12
12
#include < cc/command_interpreter.h>
13
13
#include < config/http_command_mgr.h>
14
14
#include < config/command_mgr.h>
15
+ #include < dhcp/iface_mgr.h>
15
16
#include < http/response.h>
16
17
#include < http/response_parser.h>
17
18
#include < http/testutils/test_http_client.h>
@@ -80,6 +81,7 @@ class HttpCommandMgrTest : public ::testing::Test {
80
81
}
81
82
test_timer_.cancel ();
82
83
resetState ();
84
+ IfaceMgr::instance ().deleteAllExternalSockets ();
83
85
}
84
86
85
87
// / @brief Resets state.
@@ -98,6 +100,7 @@ class HttpCommandMgrTest : public ::testing::Test {
98
100
io_service_->stopAndPoll ();
99
101
HttpCommandMgr::instance ().setIOService (IOServicePtr ());
100
102
}
103
+ HttpCommandMgr::instance ().addExternalSockets (true );
101
104
}
102
105
103
106
// / @brief Constructs a complete HTTP POST given a request body.
@@ -203,7 +206,7 @@ class HttpCommandMgrTest : public ::testing::Test {
203
206
204
207
// / Verifies the configure and close of HttpCommandMgr.
205
208
TEST_F (HttpCommandMgrTest, basic) {
206
- // Make sure we can confiugure one.
209
+ // Make sure we can configure one.
207
210
ASSERT_NO_THROW_LOG (HttpCommandMgr::instance ().configure (http_config_));
208
211
auto listener = HttpCommandMgr::instance ().getHttpListener ();
209
212
ASSERT_TRUE (listener);
@@ -230,6 +233,30 @@ TEST_F(HttpCommandMgrTest, basic) {
230
233
EXPECT_FALSE (HttpCommandMgr::instance ().getHttpListener ());
231
234
}
232
235
236
+ // / Verifies the use external socket flag of HttpCommandMgr.
237
+ TEST_F (HttpCommandMgrTest, useExternal) {
238
+ // Default is to use external sockets.
239
+ ASSERT_NO_THROW_LOG (HttpCommandMgr::instance ().configure (http_config_));
240
+ auto listener = HttpCommandMgr::instance ().getHttpListener ();
241
+ ASSERT_TRUE (listener);
242
+ int fd = listener->getNative ();
243
+ EXPECT_NE (-1 , fd);
244
+ EXPECT_TRUE (IfaceMgr::instance ().isExternalSocket (fd));
245
+ ASSERT_NO_THROW_LOG (HttpCommandMgr::instance ().close ());
246
+
247
+ // Change to no external sockets.
248
+ HttpCommandMgr::instance ().addExternalSockets (false );
249
+
250
+ // Retry: now the listener is not added as an external socket
251
+ // to the interface manager.
252
+ ASSERT_NO_THROW_LOG (HttpCommandMgr::instance ().configure (http_config_));
253
+ listener = HttpCommandMgr::instance ().getHttpListener ();
254
+ ASSERT_TRUE (listener);
255
+ fd = listener->getNative ();
256
+ EXPECT_NE (-1 , fd);
257
+ EXPECT_FALSE (IfaceMgr::instance ().isExternalSocket (fd));
258
+ }
259
+
233
260
// / Verifies the configure and close of HttpCommandMgr with TLS.
234
261
TEST_F (HttpCommandMgrTest, basicTls) {
235
262
string ca_dir (string (TEST_CA_DIR));
@@ -266,6 +293,37 @@ TEST_F(HttpCommandMgrTest, basicTls) {
266
293
EXPECT_FALSE (HttpCommandMgr::instance ().getHttpListener ());
267
294
}
268
295
296
+ // / Verifies the use external socket flag of HttpCommandMgr with TLS.
297
+ TEST_F (HttpCommandMgrTest, useExternalTls) {
298
+ string ca_dir (string (TEST_CA_DIR));
299
+ // Setup TLS for the manager.
300
+ http_config_->setSocketType (" https" );
301
+ http_config_->setTrustAnchor (ca_dir + string (" /kea-ca.crt" ));
302
+ http_config_->setCertFile (ca_dir + string (" /kea-server.crt" ));
303
+ http_config_->setKeyFile (ca_dir + string (" /kea-server.key" ));
304
+
305
+ // Default is to use external sockets.
306
+ ASSERT_NO_THROW_LOG (HttpCommandMgr::instance ().configure (http_config_));
307
+ auto listener = HttpCommandMgr::instance ().getHttpListener ();
308
+ ASSERT_TRUE (listener);
309
+ int fd = listener->getNative ();
310
+ EXPECT_NE (-1 , fd);
311
+ EXPECT_TRUE (IfaceMgr::instance ().isExternalSocket (fd));
312
+ ASSERT_NO_THROW_LOG (HttpCommandMgr::instance ().close ());
313
+
314
+ // Change to no external sockets.
315
+ HttpCommandMgr::instance ().addExternalSockets (false );
316
+
317
+ // Retry: now the listener is not added as an external socket
318
+ // to the interface manager.
319
+ ASSERT_NO_THROW_LOG (HttpCommandMgr::instance ().configure (http_config_));
320
+ listener = HttpCommandMgr::instance ().getHttpListener ();
321
+ ASSERT_TRUE (listener);
322
+ fd = listener->getNative ();
323
+ EXPECT_NE (-1 , fd);
324
+ EXPECT_FALSE (IfaceMgr::instance ().isExternalSocket (fd));
325
+ }
326
+
269
327
// This test verifies that an HTTP connection can be established and used to
270
328
// transmit an HTTP request and receive the response.
271
329
TEST_F (HttpCommandMgrTest, command) {
@@ -288,4 +346,29 @@ TEST_F(HttpCommandMgrTest, command) {
288
346
EXPECT_EQ (hr->getBody (), " [ { \" arguments\" : [ \" bar\" ], \" result\" : 0 } ]" );
289
347
}
290
348
349
+ // This test verifies that an HTTP connection can be established and used to
350
+ // transmit an HTTP request and receive the response (no external sockets).
351
+ TEST_F (HttpCommandMgrTest, commandNoExternal) {
352
+ // Change to no external sockets.
353
+ HttpCommandMgr::instance ().addExternalSockets (false );
354
+
355
+ // Configure.
356
+ ASSERT_NO_THROW_LOG (HttpCommandMgr::instance ().configure (http_config_));
357
+ EXPECT_TRUE (HttpCommandMgr::instance ().getHttpListener ());
358
+
359
+ // Now let's send a "foo" command. This should create a client, connect
360
+ // to our listener, post our request and retrieve our reply.
361
+ ASSERT_NO_THROW (startRequest (" {\" command\" : \" foo\" }" ));
362
+ ASSERT_TRUE (client_);
363
+ ASSERT_NO_THROW (runIOService ());
364
+ ASSERT_TRUE (client_);
365
+
366
+ // Parse the response into an HttpResponse.
367
+ HttpResponsePtr hr;
368
+ ASSERT_NO_THROW_LOG (hr = parseResponse (client_->getResponse ()));
369
+
370
+ // We should have a response from our command handler.
371
+ EXPECT_EQ (hr->getBody (), " [ { \" arguments\" : [ \" bar\" ], \" result\" : 0 } ]" );
372
+ }
373
+
291
374
} // end of anonymous namespace
0 commit comments