File: test_plugin_device.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 (138 lines) | stat: -rw-r--r-- 5,332 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * 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/devices/plugin/device.h>
#include <biometry/devices/plugin/enumerator.h>
#include <biometry/devices/plugin/verifier.h>

#include <biometry/util/configuration.h>
#include <biometry/util/dynamic_library.h>

#include <biometry/device_registry.h>

#include <gmock/gmock.h>

#include <fstream>
#include <system_error>

#include "config.h"

namespace
{
struct MockDynamicLibraryApi : public biometry::util::DynamicLibrary::Api
{
    MOCK_CONST_METHOD1(open, biometry::util::DynamicLibrary::Handle(const boost::filesystem::path& path));
    MOCK_CONST_METHOD1(close, void(const biometry::util::DynamicLibrary::Handle&));
    MOCK_CONST_METHOD2(sym, biometry::util::DynamicLibrary::Symbol(const biometry::util::DynamicLibrary::Handle&, const std::string& symbol));
    MOCK_CONST_METHOD0(error, std::string());
};

struct MockPluginLoader : public biometry::devices::plugin::Loader
{
    MOCK_CONST_METHOD2(
            verify_and_load,
            std::shared_ptr<biometry::Device> (const std::shared_ptr<biometry::util::DynamicLibrary::Api>&, const boost::filesystem::path&));
};
}

TEST(PluginDeviceLoad, calls_into_loader)
{
    using namespace testing;

    std::shared_ptr<biometry::util::DynamicLibrary::Api> api = std::make_shared<NiceMock<MockDynamicLibraryApi>>();
    const boost::filesystem::path path{"/tmp/does/not/exist/module.so"};

    MockPluginLoader loader;
    EXPECT_CALL(loader, verify_and_load(api, path)).Times(1).WillOnce(Return(std::shared_ptr<biometry::Device>{}));

    biometry::devices::plugin::load(api, path, loader);
}

TEST(NonVerifyingLoader, can_load_plugin)
{
    const auto p = testing::runtime_dir() / "libbiometryd_devices_plugin_dl.so";

    biometry::devices::plugin::NonVerifyingLoader loader;
    EXPECT_NO_THROW(loader.verify_and_load(biometry::util::glibc::dl_api(), p));
}

TEST(ElfDescriptorLoader, can_load_from_plugin)
{
    const auto p = testing::runtime_dir() / "libbiometryd_devices_plugin_dl.so";
    biometry::devices::plugin::ElfDescriptorLoader loader;
    auto desc = loader.load_with_name(p, BIOMETRYD_DEVICES_PLUGIN_DESCRIPTOR_SECTION);

    EXPECT_STREQ("TestPlugin", desc.name);
    EXPECT_STREQ("Thomas Voß <thomas.voss@canonical.com>", desc.author);
    EXPECT_STREQ("Just a plugin for testing purposes", desc.description);
    EXPECT_EQ(0, desc.version.plugin.major);
    EXPECT_EQ(0, desc.version.plugin.minor);
    EXPECT_EQ(0, desc.version.plugin.patch);
}

TEST(ElfDescriptorLoader, throws_for_section_not_being_found)
{
    const auto p = testing::runtime_dir() / "libbiometryd_devices_plugin_dl.so";
    biometry::devices::plugin::ElfDescriptorLoader loader;
    EXPECT_THROW(loader.load_with_name(p, "DoesNotExist"), biometry::devices::plugin::ElfDescriptorLoader::NoSuchSection);
}

TEST(ElfDescriptorLoader, throws_when_trying_to_load_non_existing_file)
{
    std::remove("test.txt");
    biometry::devices::plugin::ElfDescriptorLoader loader;
    EXPECT_THROW(loader.load_with_name("test.txt", "DoesNotExist"), std::system_error);
}

TEST(ElfDescriptorLoader, throws_when_trying_to_load_non_elf_object)
{
    std::remove("test.txt");
    {std::ofstream out("test.txt"); out << "test";}
    biometry::devices::plugin::ElfDescriptorLoader loader;
    EXPECT_THROW(loader.load_with_name("test.txt", "DoesNotExist"), std::runtime_error);
}

TEST(MajorVersionVerifier, throws_when_verifying_plugin_with_major_host_version_mismatch)
{
    const auto p = testing::runtime_dir() / "libbiometryd_devices_plugin_dl_version_mismatch.so";
    biometry::devices::plugin::ElfDescriptorLoader loader;

    biometry::devices::plugin::MajorVersionVerifier verifier;
    EXPECT_THROW(verifier.verify(loader.load_with_name(p, BIOMETRYD_DEVICES_PLUGIN_DESCRIPTOR_SECTION)),
                 biometry::devices::plugin::MajorVersionVerifier::MajorVersionMismatch);
}

TEST(MajorVersionVerifier, does_not_throw_when_verifying_plugin_with_major_host_version_match)
{
    const auto p = testing::runtime_dir() / "libbiometryd_devices_plugin_dl.so";
    biometry::devices::plugin::ElfDescriptorLoader loader;

    biometry::devices::plugin::MajorVersionVerifier verifier;
    EXPECT_NO_THROW(verifier.verify(loader.load_with_name(p, BIOMETRYD_DEVICES_PLUGIN_DESCRIPTOR_SECTION)));
}

TEST(DirectoryEnumerator, finds_biometryd_plugins)
{
    biometry::devices::plugin::DirectoryEnumerator enumerator{{testing::runtime_dir()}};
    EXPECT_GE(1, enumerator.enumerate([](const biometry::Device::Descriptor::Ptr& ptr)
    {
        static const biometry::util::Configuration the_empty_config;
        EXPECT_NO_THROW(ptr->create(the_empty_config));
    }));
}