API
sshDigger_test.cpp
Go to the documentation of this file.
1 //#define CATCH_CONFIG_MAIN
2 #include "../../../tests/catch2/catch.hpp"
3 
4 
5 #define SSHDIGGER_TEST_NOINDI
6 #define SSHDIGGER_TEST_NOLOG
7 #include "../sshDigger.hpp"
8 
9 using namespace MagAOX::app;
10 
11 class sshDigger_test : public sshDigger
12 {
13 public:
14  void configName(const std::string & cn)
15  {
16  m_configName = cn;
17  }
18 
19  std::string remoteHost() {return m_remoteHost;}
20  int localPort() {return m_localPort;}
21  int remotePort() {return m_remotePort;}
22  int monitorPort() { return m_monitorPort;}
23 };
24 
25 
26 SCENARIO( "sshDigger Configuration", "[sshDigger]" )
27 {
28  GIVEN("a config file with 1 tunnel")
29  {
30  WHEN("the tunnel is fully specified and matches configName")
31  {
32  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel1" },
33  {"remoteHost", "localPort", "remotePort" },
34  {"exao2", "80", "81" } );
35 
36 
37  mx::app::appConfigurator config;
38  config.readConfig("/tmp/sshDigger_test.conf");
39 
40  sshDigger_test dig;
41  dig.configName("tunnel1");
42  int rv;
43  rv = dig.loadConfigImpl(config);
44  REQUIRE( rv == 0);
45 
46  REQUIRE( dig.remoteHost() == "exao2");
47  REQUIRE( dig.localPort() == 80 );
48  REQUIRE( dig.remotePort() == 81 );
49  REQUIRE( dig.monitorPort() == 0 );
50  }
51 
52  WHEN("the tunnel is fully specified and matches configName, includes monitorPort")
53  {
54  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel1" , "tunnel1"},
55  {"remoteHost", "localPort", "remotePort", "monitorPort" },
56  {"exao2", "80", "81" , "6000"} );
57 
58 
59  mx::app::appConfigurator config;
60  config.readConfig("/tmp/sshDigger_test.conf");
61 
62  sshDigger_test dig;
63  dig.configName("tunnel1");
64  int rv;
65  rv = dig.loadConfigImpl(config);
66  REQUIRE( rv == 0);
67 
68  REQUIRE( dig.remoteHost() == "exao2");
69  REQUIRE( dig.localPort() == 80 );
70  REQUIRE( dig.remotePort() == 81 );
71  REQUIRE( dig.monitorPort() == 6000);
72 
73  }
74 
75  WHEN("no unused sections")
76  {
77  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {},
78  {},
79  {} );
80 
81  mx::app::appConfigurator config;
82  config.readConfig("/tmp/sshDigger_test.conf");
83 
84  sshDigger_test dig;
85  dig.configName("tunnel2");
86  int rv;
87  rv = dig.loadConfigImpl(config);
89 
90 
91  }
92 
93  WHEN("the tunnel does not match configName")
94  {
95  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel1" },
96  {"remoteHost", "localPort", "remotePort" },
97  {"exao2", "80", "81" } );
98 
99 
100  mx::app::appConfigurator config;
101  config.readConfig("/tmp/sshDigger_test.conf");
102 
103  sshDigger_test dig;
104  dig.configName("tunnel2");
105  int rv;
106  rv = dig.loadConfigImpl(config);
108 
109 
110  }
111 
112  WHEN("no remote host")
113  {
114  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1" },
115  {"localPort", "remotePort" },
116  {"80", "81" } );
117 
118 
119  mx::app::appConfigurator config;
120  config.readConfig("/tmp/sshDigger_test.conf");
121 
122  sshDigger_test dig;
123  dig.configName("tunnel1");
124  int rv;
125  rv = dig.loadConfigImpl(config);
127 
128  }
129 
130  WHEN("no local port")
131  {
132  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1" },
133  {"remoteHost", "remotePort" },
134  {"exao2", "81" } );
135 
136  mx::app::appConfigurator config;
137  config.readConfig("/tmp/sshDigger_test.conf");
138 
139  sshDigger_test dig;
140  dig.configName("tunnel1");
141  int rv;
142  rv = dig.loadConfigImpl(config);
144 
145  }
146 
147  WHEN("no remote port")
148  {
149  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1" },
150  {"remoteHost", "localPort" },
151  {"exao2", "80" } );
152 
153  mx::app::appConfigurator config;
154  config.readConfig("/tmp/sshDigger_test.conf");
155 
156  sshDigger_test dig;
157  dig.configName("tunnel1");
158  int rv;
159  rv = dig.loadConfigImpl(config);
161 
162  }
163 
164  }
165  GIVEN("a config file with 2 tunnels")
166  {
167  WHEN("the tunnels are fully specified and match their configNames")
168  {
169  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel1", "tunnel2", "tunnel2", "tunnel2" },
170  {"remoteHost", "localPort", "remotePort", "remoteHost", "localPort", "remotePort" },
171  {"exao2", "80", "81", "exao3", "85", "86" } );
172 
173 
174  mx::app::appConfigurator config;
175  config.readConfig("/tmp/sshDigger_test.conf");
176 
177  int rv;
178 
179  sshDigger_test dig;
180  dig.configName("tunnel1");
181 
182  rv = dig.loadConfigImpl(config);
183  REQUIRE( rv == 0);
184  REQUIRE( dig.remoteHost() == "exao2");
185  REQUIRE( dig.localPort() == 80 );
186  REQUIRE( dig.remotePort() == 81 );
187 
188  dig.configName("tunnel2");
189 
190  rv = dig.loadConfigImpl(config);
191  REQUIRE( rv == 0);
192  REQUIRE( dig.remoteHost() == "exao3");
193  REQUIRE( dig.localPort() == 85 );
194  REQUIRE( dig.remotePort() == 86 );
195 
196  }
197 
198 
199  WHEN("No tunnels match configName")
200  {
201  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel1", "tunnel2", "tunnel2", "tunnel2" },
202  {"remoteHost", "localPort", "remotePort", "remoteHost", "localPort", "remotePort" },
203  {"exao2", "80", "81", "exao3", "85", "86" } );
204 
205  mx::app::appConfigurator config;
206  config.readConfig("/tmp/sshDigger_test.conf");
207 
208  sshDigger_test dig;
209  dig.configName("tunnel3");
210  int rv;
211  rv = dig.loadConfigImpl(config);
213 
214 
215  }
216 
217  WHEN("no remote host in first tunnel")
218  {
219  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel2", "tunnel2", "tunnel2" },
220  {"localPort", "remotePort", "remoteHost", "localPort", "remotePort" },
221  {"80", "81", "exao3", "85", "86" } );
222 
223 
224 
225  mx::app::appConfigurator config;
226  config.readConfig("/tmp/sshDigger_test.conf");
227 
228  sshDigger_test dig;
229  dig.configName("tunnel1");
230  int rv;
231  rv = dig.loadConfigImpl(config);
233 
234 
235  dig.configName("tunnel2");
236 
237  rv = dig.loadConfigImpl(config);
238  REQUIRE( rv == 0);
239  REQUIRE( dig.remoteHost() == "exao3");
240  REQUIRE( dig.localPort() == 85 );
241  REQUIRE( dig.remotePort() == 86 );
242 
243 
244  }
245 
246  WHEN("no remote host in 2nd tunnel")
247  {
248  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel1", "tunnel2", "tunnel2" },
249  {"remoteHost", "localPort", "remotePort", "localPort", "remotePort" },
250  {"exao2", "80", "81", "85", "86" } );
251 
252 
253  mx::app::appConfigurator config;
254  config.readConfig("/tmp/sshDigger_test.conf");
255 
256  sshDigger_test dig;
257  dig.configName("tunnel2");
258  int rv;
259  rv = dig.loadConfigImpl(config);
261 
262 
263  dig.configName("tunnel1");
264 
265  rv = dig.loadConfigImpl(config);
266  REQUIRE( rv == 0);
267  REQUIRE( dig.remoteHost() == "exao2");
268  REQUIRE( dig.localPort() == 80 );
269  REQUIRE( dig.remotePort() == 81 );
270 
271 
272  }
273 
274  WHEN("no local port in first tunnel")
275  {
276  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel2", "tunnel2", "tunnel2" },
277  {"remoteHost", "remotePort", "remoteHost", "localPort", "remotePort" },
278  {"exao2", "81", "exao3", "85", "86" } );
279 
280 
281 
282  mx::app::appConfigurator config;
283  config.readConfig("/tmp/sshDigger_test.conf");
284 
285  sshDigger_test dig;
286  dig.configName("tunnel1");
287  int rv;
288  rv = dig.loadConfigImpl(config);
290 
291 
292  dig.configName("tunnel2");
293 
294  rv = dig.loadConfigImpl(config);
295  REQUIRE( rv == 0);
296  REQUIRE( dig.remoteHost() == "exao3");
297  REQUIRE( dig.localPort() == 85 );
298  REQUIRE( dig.remotePort() == 86 );
299 
300 
301  }
302 
303  WHEN("no local port in second tunnel")
304  {
305  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel1", "tunnel2", "tunnel2" },
306  {"remoteHost", "localPort", "remotePort", "remoteHost", "remotePort" },
307  {"exao2", "80", "81", "exao3", "86" } );
308 
309  mx::app::appConfigurator config;
310  config.readConfig("/tmp/sshDigger_test.conf");
311 
312  sshDigger_test dig;
313  dig.configName("tunnel2");
314  int rv;
315  rv = dig.loadConfigImpl(config);
317 
318 
319  dig.configName("tunnel1");
320 
321  rv = dig.loadConfigImpl(config);
322  REQUIRE( rv == 0);
323  REQUIRE( dig.remoteHost() == "exao2");
324  REQUIRE( dig.localPort() == 80 );
325  REQUIRE( dig.remotePort() == 81 );
326 
327  }
328 
329  WHEN("no remote port in first tunnel")
330  {
331  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel2", "tunnel2", "tunnel2" },
332  {"remoteHost", "localPort", "remoteHost", "localPort", "remotePort" },
333  {"exao2", "80", "exao3", "85", "86" } );
334 
335 
336 
337  mx::app::appConfigurator config;
338  config.readConfig("/tmp/sshDigger_test.conf");
339 
340  sshDigger_test dig;
341  dig.configName("tunnel1");
342  int rv;
343  rv = dig.loadConfigImpl(config);
345 
346 
347  dig.configName("tunnel2");
348 
349  rv = dig.loadConfigImpl(config);
350  REQUIRE( rv == 0);
351  REQUIRE( dig.remoteHost() == "exao3");
352  REQUIRE( dig.localPort() == 85 );
353  REQUIRE( dig.remotePort() == 86 );
354 
355  }
356 
357  WHEN("no remote port in second tunnel")
358  {
359  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel1", "tunnel2", "tunnel2" },
360  {"remoteHost", "localPort", "remotePort", "remoteHost", "localPort" },
361  {"exao2", "80", "81", "exao3", "85" } );
362 
363  mx::app::appConfigurator config;
364  config.readConfig("/tmp/sshDigger_test.conf");
365 
366  sshDigger_test dig;
367  dig.configName("tunnel2");
368  int rv;
369  rv = dig.loadConfigImpl(config);
371 
372 
373  dig.configName("tunnel1");
374 
375  rv = dig.loadConfigImpl(config);
376  REQUIRE( rv == 0);
377  REQUIRE( dig.remoteHost() == "exao2");
378  REQUIRE( dig.localPort() == 80 );
379  REQUIRE( dig.remotePort() == 81 );
380 
381  }
382  }
383 }
384 
385 SCENARIO( "sshDigger tunnel exec preparation", "[sshDigger]" )
386 {
387  GIVEN("a config file with 1 tunnel")
388  {
389  WHEN("creating the tunnelSpec")
390  {
391  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel1" },
392  {"remoteHost", "localPort", "remotePort" },
393  {"exao2", "80", "81" } );
394 
395 
396  mx::app::appConfigurator config;
397  config.readConfig("/tmp/sshDigger_test.conf");
398 
399  sshDigger_test dig;
400  dig.configName("tunnel1");
401  int rv;
402  rv = dig.loadConfigImpl(config);
403  REQUIRE( rv == 0);
404 
405  REQUIRE( dig.tunnelSpec() == "80:localhost:81");
406  }
407 
408  WHEN("creating the exec argv vector")
409  {
410  mx::app::writeConfigFile( "/tmp/sshDigger_test.conf", {"tunnel1", "tunnel1", "tunnel1" },
411  {"remoteHost", "localPort", "remotePort" },
412  {"exao2", "80", "81" } );
413 
414 
415  mx::app::appConfigurator config;
416  config.readConfig("/tmp/sshDigger_test.conf");
417 
418  sshDigger_test dig;
419  dig.configName("tunnel1");
420  int rv;
421  rv = dig.loadConfigImpl(config);
422  REQUIRE( rv == 0);
423 
424  std::vector<std::string> argsV;
425  dig.genArgsV(argsV);
426 
427  REQUIRE( argsV[0] == "autossh");
428  REQUIRE( argsV[1] == "-M0");
429  REQUIRE( argsV[2] == "");
430  REQUIRE( argsV[3] == "-nNTL");
431  REQUIRE( argsV[4] == "80:localhost:81");
432  REQUIRE( argsV[5] == "exao2");
433  }
434  }
435 }
#define GIVEN(desc)
Definition: catch.hpp:17763
#define WHEN(desc)
Definition: catch.hpp:17765
#define REQUIRE(...)
Definition: catch.hpp:17676
The MagAO-X SSH tunnel manager.
Definition: sshDigger.hpp:55
int loadConfigImpl(mx::app::appConfigurator &_config)
Implementation of loadConfig logic, separated for testing.
Definition: sshDigger.hpp:199
std::string tunnelSpec()
Create the tunnel specification string, [localPort]:localhost:[remotePort].
Definition: sshDigger.hpp:296
void genArgsV(std::vector< std::string > &argsV)
Generate the argv vector for the exec of autossh.
Definition: sshDigger.hpp:303
void configName(const std::string &cn)
std::string remoteHost()
#define SSHDIGGER_E_NOLOCALPORT
Definition: sshDigger.hpp:36
#define SSHDIGGER_E_NOTUNNELFOUND
Definition: sshDigger.hpp:34
#define SSHDIGGER_E_NOREMOTEPORT
Definition: sshDigger.hpp:37
#define SSHDIGGER_E_NOHOSTNAME
Definition: sshDigger.hpp:35
#define SSHDIGGER_E_NOTUNNELS
Definition: sshDigger.hpp:33
SCENARIO("sshDigger Configuration", "[sshDigger]")