~thomas-voss/biometryd/verify-incoming-requests

« back to all changes in this revision

Viewing changes to src/biometry/cmds/test.cpp

Merge lp:~thomas-voss/biometryd/test-against-service-if-no-config-given

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include <biometry/device_registry.h>
24
24
#include <biometry/identifier.h>
25
25
#include <biometry/reason.h>
 
26
#include <biometry/service.h>
26
27
#include <biometry/template_store.h>
27
28
#include <biometry/tracing_operation_observer.h>
28
29
#include <biometry/user.h>
29
30
 
 
31
#include <biometry/dbus/service.h>
 
32
 
30
33
#include <biometry/util/benchmark.h>
31
34
#include <biometry/util/configuration.h>
32
35
#include <biometry/util/json_configuration_builder.h>
36
39
 
37
40
namespace cli = biometry::util::cli;
38
41
 
 
42
namespace
 
43
{
 
44
std::shared_ptr<biometry::Device> device_from_config_file(const boost::filesystem::path& file)
 
45
{
 
46
    using StreamingJsonConfigurationBuilder = biometry::util::StreamingConfigurationBuilder<biometry::util::JsonConfigurationBuilder>;
 
47
    StreamingJsonConfigurationBuilder builder{StreamingJsonConfigurationBuilder::make_streamer(file)};
 
48
 
 
49
    const auto configuration = builder.build_configuration();
 
50
 
 
51
    static const auto throw_configuration_invalid = [](){ std::throw_with_nested(biometry::cmds::Test::ConfigurationInvalid{});};
 
52
 
 
53
    const auto id = configuration
 
54
            ("device",  throw_configuration_invalid)
 
55
            ("id",      throw_configuration_invalid);
 
56
    biometry::util::Configuration config; config["config"] = configuration
 
57
            ("device",  throw_configuration_invalid)
 
58
            ["config"];
 
59
 
 
60
    try
 
61
    {
 
62
        return biometry::device_registry().at(id.value().string())->create(config);
 
63
    } catch(...) { std::throw_with_nested(biometry::cmds::Test::CouldNotInstiantiateDevice{});}
 
64
}
 
65
}
 
66
 
39
67
biometry::cmds::Test::ConfigurationInvalid::ConfigurationInvalid()
40
68
    : std::runtime_error{"Configuration is invalid"}
41
69
{
56
84
 
57
85
    action([this](const cli::Command::Context& ctxt)
58
86
    {
59
 
        if (not config) throw cli::Command::FlagsMissing{};
60
 
 
61
 
        using StreamingJsonConfigurationBuilder = util::StreamingConfigurationBuilder<util::JsonConfigurationBuilder>;
62
 
        StreamingJsonConfigurationBuilder builder
63
 
        {
64
 
            config ?
65
 
                StreamingJsonConfigurationBuilder::make_streamer(config.get()) :
66
 
                StreamingJsonConfigurationBuilder::make_streamer(std::cin)
67
 
        };
68
 
 
69
 
        const auto configuration = builder.build_configuration();
70
 
 
71
 
        static const auto throw_configuration_invalid = [](){ std::throw_with_nested(ConfigurationInvalid{});};
72
 
 
73
 
        const auto id = configuration
74
 
                ("device",  throw_configuration_invalid)
75
 
                ("id",      throw_configuration_invalid);
76
 
        util::Configuration config; config["config"] = configuration
77
 
                ("device",  throw_configuration_invalid)
78
 
                ["config"];
79
 
 
80
 
        if (id)
81
 
        {
82
 
            Device::Descriptor::Ptr desc;
83
 
            std::shared_ptr<Device> device;
84
 
            try
85
 
            {
86
 
                device = (desc = device_registry().at(id.value().string()))->create(config);
87
 
            } catch(...) { std::throw_with_nested(CouldNotInstiantiateDevice{});}
88
 
 
89
 
            ctxt.cout
90
 
                    << "We are about to execute a test run for a biometric device." << std::endl
91
 
                    << "Please note that we are executing the test in a production" << std::endl
92
 
                    << "environment and you should consider the test to be harmful to the" << std::endl
93
 
                    << "device configuration for device with:" << std::endl
94
 
                    << "  Name:        " << desc->name() << std::endl
95
 
                    << "  Description: " << desc->description() << std::endl
96
 
                    << "  Author:      " << desc->author() << std::endl
97
 
                    << "and configuration:" << std::endl
98
 
                    << "  User:        " << user << std::endl
99
 
                    << "  Config:      " << *Test::config << std::endl
100
 
                    << "Would you really like to proceed (y/n)?";
101
 
 
102
 
            static constexpr const char yes{'y'}; char answer; ctxt.cin >> answer;
103
 
            return answer == yes ? test_device(user, ctxt, device) : EXIT_FAILURE;
104
 
        } else {throw std::runtime_error{"Invalid configuration."};}
 
87
        auto device = config ? device_from_config_file(*config) : biometry::dbus::Service::create_stub()->default_device();
 
88
 
 
89
        ctxt.cout
 
90
                << "We are about to execute a test run for a biometric device."                         << std::endl
 
91
                << "Please note that we are executing the test in a production"                         << std::endl
 
92
                << "environment and you should consider the test to be harmful to the"                  << std::endl
 
93
                << "device configuration:"                                                              << std::endl
 
94
                << "  User:        " << user                                                            << std::endl
 
95
                << "  Config:      " << (Test::config ? (*Test::config).string() : "Default device")    << std::endl
 
96
                << "Would you really like to proceed (y/n)?";
 
97
 
 
98
        static constexpr const char yes{'y'}; char answer; ctxt.cin >> answer;
 
99
        return answer == yes ? test_device(user, ctxt, device) : EXIT_FAILURE;
105
100
 
106
101
        return EXIT_FAILURE;
107
102
    });