File: plugin.cc

package info (click to toggle)
ignition-common 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,856 kB
  • sloc: cpp: 22,616; python: 1,595; ansic: 484; sh: 125; makefile: 18
file content (148 lines) | stat: -rw-r--r-- 5,441 bytes parent folder | download
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
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (C) 2017 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

#include <string>
#include <vector>
#include <gtest/gtest.h>
#include <iostream>
#include "ignition/common/Console.hh"
#include "ignition/common/Filesystem.hh"
#include "ignition/common/PluginLoader.hh"
#include "ignition/common/SystemPaths.hh"
#include "ignition/common/Plugin.hh"

#include "test_config.h"
#include "DummyPluginsPath.h"
#include "util/DummyPlugins.hh"

/////////////////////////////////////////////////
TEST(PluginLoader, LoadBadPlugins)
{
  std::string dummyPath =
    ignition::common::copyFromUnixPath(IGN_DUMMY_PLUGIN_PATH);

  ignition::common::SystemPaths sp;
  sp.AddPluginPaths(dummyPath);

  std::vector<std::string> libraryNames = {
    "IGNBadPluginAPIVersionOld",
    "IGNBadPluginAPIVersionNew",
    "IGNBadPluginAlign",
    "IGNBadPluginSize"};
  for (auto const & libraryName : libraryNames)
  {
    std::string path = sp.FindSharedLibrary(libraryName);
    ASSERT_FALSE(path.empty());

    ignition::common::PluginLoader pl;

    // Make sure the expected plugins were loaded.
    ignition::common::Console::SetVerbosity(2);
    std::unordered_set<std::string> pluginNames = pl.LoadLibrary(path);
    EXPECT_TRUE(pluginNames.empty());
  }
}

/////////////////////////////////////////////////
TEST(PluginLoader, LoadExistingLibrary)
{
  std::string dummyPath =
    ignition::common::copyFromUnixPath(IGN_DUMMY_PLUGIN_PATH);

  ignition::common::SystemPaths sp;
  sp.AddPluginPaths(dummyPath);

  std::string path = sp.FindSharedLibrary("IGNDummyPlugins");
  ASSERT_FALSE(path.empty());

  ignition::common::PluginLoader pl;

  // Make sure the expected plugins were loaded.
  std::unordered_set<std::string> pluginNames = pl.LoadLibrary(path);
  EXPECT_EQ(1u, pluginNames.count("::test::util::DummySinglePlugin"));
  EXPECT_EQ(1u, pluginNames.count("::test::util::DummyMultiPlugin"));

  std::cout << pl.PrettyStr();

  // Make sure the expected interfaces were loaded.
  EXPECT_EQ(5u, pl.InterfacesImplemented().size());
  EXPECT_EQ(1u, pl.InterfacesImplemented()
            .count("::test::util::DummyNameBase"));
  EXPECT_EQ(2u, pl.PluginsImplementing("::test::util::DummyNameBase").size());
  EXPECT_EQ(1u, pl.PluginsImplementing("::test::util::DummyDoubleBase").size());


  ignition::common::PluginPtr firstPlugin =
      pl.Instantiate("test::util::DummySinglePlugin");
  EXPECT_TRUE(firstPlugin->HasInterface("test::util::DummyNameBase"));
  EXPECT_FALSE(firstPlugin->HasInterface("test::util::DummyDoubleBase"));
  EXPECT_FALSE(firstPlugin->HasInterface("test::util::DummyIntBase"));
  EXPECT_FALSE(firstPlugin->HasInterface("test::util::DummySetterBase"));

  ignition::common::PluginPtr secondPlugin =
      pl.Instantiate("test::util::DummyMultiPlugin");
  EXPECT_TRUE(secondPlugin->HasInterface("test::util::DummyNameBase"));
  EXPECT_TRUE(secondPlugin->HasInterface("test::util::DummyDoubleBase"));
  EXPECT_TRUE(secondPlugin->HasInterface("test::util::DummyIntBase"));
  EXPECT_TRUE(secondPlugin->HasInterface("test::util::DummySetterBase"));

  // Check that the DummyNameBase interface exists and that it returns the
  // correct value.
  test::util::DummyNameBase* nameBase =
      firstPlugin->QueryInterface<test::util::DummyNameBase>(
        "test::util::DummyNameBase");
  ASSERT_NE(nullptr, nameBase);
  EXPECT_EQ(std::string("DummySinglePlugin"), nameBase->MyNameIs());

  // Check that DummyDoubleBase does not exist for this plugin
  test::util::DummyDoubleBase* doubleBase =
      firstPlugin->QueryInterface<test::util::DummyDoubleBase>(
        "test::util::DummyDoubleBase");
  EXPECT_EQ(nullptr, doubleBase);

  // Check that DummyDoubleBase does exist for this function and that it returns
  // the correct value.
  doubleBase = secondPlugin->QueryInterface<test::util::DummyDoubleBase>(
        "test::util::DummyDoubleBase");
  ASSERT_NE(nullptr, doubleBase);
  EXPECT_NEAR(3.14159, doubleBase->MyDoubleValueIs(), 1e-8);

  // Check that the DummyNameBase interface exists for this plugin and that it
  // returns the correct value.
  nameBase = secondPlugin->QueryInterface<test::util::DummyNameBase>(
        "test::util::DummyNameBase");
  ASSERT_NE(nullptr, nameBase);
  EXPECT_EQ(std::string("DummyMultiPlugin"), nameBase->MyNameIs());

  test::util::DummyGetSomeObjectBase *objectBase =
    secondPlugin->QueryInterface<test::util::DummyGetSomeObjectBase>();
  ASSERT_NE(nullptr, objectBase);

  std::unique_ptr<test::util::SomeObject> object =
    objectBase->GetSomeObject();
  EXPECT_EQ(secondPlugin->QueryInterface<test::util::DummyIntBase>()
                ->MyIntegerValueIs(),
            object->someInt);
  EXPECT_NEAR(doubleBase->MyDoubleValueIs(), object->someDouble, 1e-8);
}

/////////////////////////////////////////////////
int main(int argc, char **argv)
{
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}