File: test_test.cpp

package info (click to toggle)
biometryd 0.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,500 kB
  • sloc: cpp: 10,221; ansic: 191; python: 42; makefile: 20
file content (84 lines) | stat: -rw-r--r-- 2,790 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (C) 2016 Canonical, Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 *
 */

#include <biometry/cmds/test.h>

#include <biometry/device_registrar.h>
#include <biometry/devices/plugin/enumerator.h>

#include <core/posix/fork.h>

#include <gtest/gtest.h>

#include "did_finish_successfully.h"

#include <fstream>

namespace cli = biometry::util::cli;

namespace
{
struct NullEnumerator : public biometry::devices::plugin::Enumerator
{
    std::size_t enumerate(const Functor& ) const override
    {
        return 0;
    }
};

struct CmdTest : public ::testing::Test
{
    biometry::DeviceRegistrar device_registrar{NullEnumerator{}};
};
}

TEST_F(CmdTest, throws_configuration_invalid_if_device_is_not_given)
{
    {std::remove("invalid.json"); std::ofstream out{"invalid.json"}; out << R"_({ "device": {}})_" << std::endl;}

    biometry::cmds::Test test;
    EXPECT_THROW(test.run(cli::Command::Context{std::cin, std::cout, {"--config=invalid.json"}}), biometry::cmds::Test::ConfigurationInvalid);
}

TEST_F(CmdTest, throws_could_not_instantiate_device_for_unknown_id)
{
    {std::remove("invalid.json"); std::ofstream out{"invalid.json"}; out << R"_({ "device": {"id": "unknown"}})_" << std::endl;}

    biometry::cmds::Test test;
    EXPECT_THROW(test.run(cli::Command::Context{std::cin, std::cout, {"--config=invalid.json"}}), biometry::cmds::Test::CouldNotInstiantiateDevice);
}

TEST_F(CmdTest, runs_successfully_for_dummy_device)
{
    auto d = []()
    {
        {std::remove("dummy.json"); std::ofstream out{"dummy.json"}; out << R"_({"device": {"id": "Dummy"}})_" << std::endl;}

        biometry::cmds::Test test;
        EXPECT_EQ(EXIT_SUCCESS, test.run(cli::Command::Context{std::cin, std::cout, {"--config=dummy.json"}}));
        return testing::Test::HasFailure() ?
                    core::posix::exit::Status::failure :
                    core::posix::exit::Status::success;
    };

    auto cp = core::posix::fork(d, core::posix::StandardStream::stdin); cp.cin() << 'y' << std::endl;
    EXPECT_TRUE(testing::did_finish_successfully(
                    cp.wait_for(
                        core::posix::wait::Flags::untraced)));
}