forked from gazebosim/gz-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemLoader_TEST.cc
179 lines (156 loc) · 5.44 KB
/
SystemLoader_TEST.cc
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (C) 2018 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 <gtest/gtest.h>
#include <sdf/Root.hh>
#include <sdf/World.hh>
#include <gz/common/Filesystem.hh>
#include "gz/sim/System.hh"
#include "gz/sim/SystemLoader.hh"
#include "test_config.hh" // NOLINT(build/include)
using namespace gz;
using namespace sim;
/////////////////////////////////////////////////
TEST(SystemLoader, Constructor)
{
sim::SystemLoader sm;
// Add test plugin to path (referenced in config)
auto testBuildPath = gz::common::joinPaths(
std::string(PROJECT_BINARY_PATH), "lib");
sm.AddSystemPluginPath(testBuildPath);
sdf::Root root;
root.LoadSdfString(std::string("<?xml version='1.0'?><sdf version='1.6'>"
"<world name='default'>"
"<plugin filename='libgz-sim")
GZ_SIM_MAJOR_VERSION_STR "-physics-system.so' "
"name='gz::sim::systems::Physics'></plugin>"
"</world></sdf>");
auto worldElem = root.WorldByIndex(0)->Element();
if (worldElem->HasElement("plugin")) {
sdf::ElementPtr pluginElem = worldElem->GetElement("plugin");
while (pluginElem)
{
sdf::Plugin plugin;
plugin.Load(pluginElem);
auto system = sm.LoadPlugin(plugin);
ASSERT_TRUE(system.has_value());
pluginElem = pluginElem->GetNextElement("plugin");
}
}
}
/////////////////////////////////////////////////
TEST(SystemLoader, EmptyNames)
{
sim::SystemLoader sm;
sdf::Plugin plugin;
::testing::internal::CaptureStderr();
auto system = sm.LoadPlugin(plugin);
ASSERT_FALSE(system.has_value());
auto output = ::testing::internal::GetCapturedStderr();
EXPECT_NE(std::string::npos, output.find("empty argument"));
}
/////////////////////////////////////////////////
TEST(SystemLoader, PluginPaths)
{
SystemLoader sm;
// verify that there should exist some default paths
std::list<std::string> paths = sm.PluginPaths();
unsigned int pathCount = paths.size();
EXPECT_LT(0u, pathCount);
// Add test path and verify that the loader now contains this path
auto testBuildPath = common::joinPaths(
std::string(PROJECT_BINARY_PATH), "lib/");
sm.AddSystemPluginPath(testBuildPath);
paths = sm.PluginPaths();
// Number of paths should increase by 1
EXPECT_EQ(pathCount 1, paths.size());
// verify newly added paths exists
bool hasPath = false;
for (const auto &s : paths)
{
// the returned path string may not be exact match due to extra '/'
// appended at the end of the string. So use absPath to compare paths
if (common::absPath(s) == common::absPath(testBuildPath))
{
hasPath = true;
break;
}
}
EXPECT_TRUE(hasPath);
}
/////////////////////////////////////////////////
TEST(SystemLoader, BadLibraryPath)
{
sim::SystemLoader sm;
// Add test plugin to path (referenced in config)
auto testBuildPath = gz::common::joinPaths(
std::string(PROJECT_BINARY_PATH), "lib");
sm.AddSystemPluginPath(testBuildPath);
sdf::Root root;
root.LoadSdfString(std::string("<?xml version='1.0'?><sdf version='1.6'>"
"<world name='default'>"
"<plugin filename='foo.so'"
"name='gz::sim::systems::Physics'></plugin>"
"</world></sdf>"));
auto worldElem = root.WorldByIndex(0)->Element();
if (worldElem->HasElement("plugin")) {
sdf::ElementPtr pluginElem = worldElem->GetElement("plugin");
while (pluginElem)
{
::testing::internal::CaptureStderr();
sdf::Plugin plugin;
plugin.Load(pluginElem);
auto system = sm.LoadPlugin(plugin);
ASSERT_FALSE(system.has_value());
auto output = ::testing::internal::GetCapturedStderr();
EXPECT_NE(std::string::npos,
output.find("Could not find shared library")) << output.c_str();
pluginElem = pluginElem->GetNextElement("plugin");
}
}
}
/////////////////////////////////////////////////
TEST(SystemLoader, BadPluginName)
{
sim::SystemLoader sm;
// Add test plugin to path (referenced in config)
auto testBuildPath = gz::common::joinPaths(
std::string(PROJECT_BINARY_PATH), "lib");
sm.AddSystemPluginPath(testBuildPath);
sdf::Root root;
root.LoadSdfString(std::string("<?xml version='1.0'?><sdf version='1.6'>"
"<world name='default'>"
"<plugin filename='libgz-sim")
GZ_SIM_MAJOR_VERSION_STR "-physics-system.so' "
"name='gz::sim::systems::Foo'></plugin>"
"</world></sdf>");
auto worldElem = root.WorldByIndex(0)->Element();
if (worldElem->HasElement("plugin")) {
sdf::ElementPtr pluginElem = worldElem->GetElement("plugin");
while (pluginElem)
{
::testing::internal::CaptureStderr();
sdf::Plugin plugin;
plugin.Load(pluginElem);
auto system = sm.LoadPlugin(plugin);
ASSERT_FALSE(system.has_value());
auto output = ::testing::internal::GetCapturedStderr();
EXPECT_NE(std::string::npos,
output.find("library does not contain")) << output.c_str();
pluginElem = pluginElem->GetNextElement("plugin");
}
}
}