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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
// Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <asiolink/asio_wrapper.h>
#include <asiolink/interval_timer.h>
#include <asiolink/io_service.h>
#include <http/listener.h>
#include <http/post_request_json.h>
#include <http/response_creator.h>
#include <http/response_creator_factory.h>
#include <http/response_json.h>
#include <http/tests/response_test.h>
#include <netconf/http_control_socket.h>
#include <netconf/netconf_config.h>
#include <netconf/stdout_control_socket.h>
#include <netconf/unix_control_socket.h>
#include <testutils/sandbox.h>
#include <testutils/threaded_test.h>
#include <yang/tests/sysrepo_setup.h>

#include <iostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <thread><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc;
using namespace isc::netconf;
using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::http;
using namespace isc::http::test;
using namespace isc::test;

using isc::yang::test::SysrepoSetup;

namespace {

/// @brief Type definition for the pointer to Thread objects.
using ThreadPtr = shared_ptr<thread>;

//////////////////////////////// STDOUT ////////////////////////////////

/// @brief Test derived StdoutControlSocket class.
///
/// This class exposes the constructor taking the output stream.
class TestStdoutControlSocket : public StdoutControlSocket {
public:
    /// @brief Constructor.
    ///
    /// @param ctrl_sock The control socket configuration.
    /// @param output The output stream.
    TestStdoutControlSocket(CfgControlSocketPtr ctrl_sock, ostream& output)
        : StdoutControlSocket(ctrl_sock, output) {
    }
};  // TestStdoutControlSocket

/// @brief Type definition for the pointer to the @c TestStdoutControlSocket.
using TestStdoutControlSocketPtr = shared_ptr<TestStdoutControlSocket>;

// Verifies that the createControlSocket template can create a stdout
// control socket.
TEST(StdoutControlSocketTest, createControlSocket) {<--- syntax error
    CfgControlSocketPtr cfg(new
                    CfgControlSocket(CfgControlSocket::Type::STDOUT,
                                     "",
                                     Url("http://127.0.0.1:8000/")));
    ASSERT_TRUE(cfg);
    ControlSocketBasePtr cs = controlSocketFactory(cfg);
    ASSERT_TRUE(cs);
    StdoutControlSocketPtr scs =
        dynamic_pointer_cast<StdoutControlSocket>(cs);
    EXPECT_TRUE(scs);
}

// Verifies that a stdout control socket does not implement configGet.
TEST(StdoutControlSocketTest, configGet) {
    CfgControlSocketPtr cfg(new
                    CfgControlSocket(CfgControlSocket::Type::STDOUT,
                                     "",
                                     Url("http://127.0.0.1:8000/")));
    ASSERT_TRUE(cfg);
    StdoutControlSocketPtr scs(new StdoutControlSocket(cfg));
    ASSERT_TRUE(scs);
    EXPECT_THROW_MSG(scs->configGet("foo"), NotImplemented,
                     "No config-get for stdout control socket");
}

// Verifies that a stdout control socket does not nothing for configTest.
TEST(StdoutControlSocketTest, configTest) {
    CfgControlSocketPtr cfg(new
                            CfgControlSocket(CfgControlSocket::Type::STDOUT,
                                             "",
                                             Url("http://127.0.0.1:8000/")));
    ASSERT_TRUE(cfg);
    StdoutControlSocketPtr scs(new StdoutControlSocket(cfg));
    ASSERT_TRUE(scs);
    ConstElementPtr answer;
    ASSERT_NO_THROW_LOG(answer = scs->configTest(ElementPtr(), "foo"));

    // Check answer.
    ASSERT_TRUE(answer);
    EXPECT_EQ("{ \"result\": 0 }", answer->str());
}

// Verifies that a stdout control socket outputs configSet argument.
TEST(StdoutControlSocketTest, configSet) {
    CfgControlSocketPtr cfg(new
                            CfgControlSocket(CfgControlSocket::Type::STDOUT,
                                             "",
                                             Url("http://127.0.0.1:8000/")));
    ASSERT_TRUE(cfg);
    ostringstream os;
    TestStdoutControlSocketPtr tscs(new TestStdoutControlSocket(cfg, os));
    ASSERT_TRUE(tscs);
    ElementPtr json = Element::fromJSON("{ \"bar\": 1 }");
    ConstElementPtr answer;
    ASSERT_NO_THROW_LOG(answer = tscs->configSet(json, "foo"));

    // Check answer.
    ASSERT_TRUE(answer);
    EXPECT_EQ("{ \"result\": 0 }", answer->str());

    // Check output.
    string expected = "{\n  \"bar\": 1\n}\n";
    EXPECT_EQ(expected, os.str());
}

//////////////////////////////// UNIX ////////////////////////////////

/// @brief Test timeout in ms.
const long TEST_TIMEOUT = 1500;

/// @brief Test fixture class for unix control sockets.
class UnixControlSocketTest : public ThreadedTest {
public:
    isc::test::Sandbox sandbox;

    /// @brief Constructor.
    UnixControlSocketTest()
        : ThreadedTest(), io_service_(new IOService()) {
    }

    void SetUp() override {
        SysrepoSetup::cleanSharedMemory();
        removeUnixSocketFile();
    }

    void TearDown() override {
        if (thread_) {
            thread_->join();
            thread_.reset();
        }
        removeUnixSocketFile();
        io_service_->stopAndPoll();
    }

    /// @brief Returns socket file path.
    ///
    /// If the KEA_SOCKET_TEST_DIR environment variable is specified, the
    /// socket file is created in the location pointed to by this variable.
    /// Otherwise, it is created in the build directory.
    string unixSocketFilePath() {
        string socket_path;
        const char* env = getenv("KEA_SOCKET_TEST_DIR");
        if (env) {
            socket_path = string(env) + "/test-socket";
        } else {
            socket_path = sandbox.join("test-socket");
        }
        return (socket_path);
    }

    /// @brief Removes unix socket descriptor.
    void removeUnixSocketFile() {
        static_cast<void>(remove(unixSocketFilePath().c_str()));
    }

    /// @brief Create configuration of the control socket.
    ///
    /// @return a pointer to a control socket configuration.
    CfgControlSocketPtr createCfgControlSocket() {
        CfgControlSocketPtr cfg;
        cfg.reset(new CfgControlSocket(CfgControlSocket::Type::UNIX,
                                       unixSocketFilePath(),
                                       Url("http://127.0.0.1:8000/")));
        return (cfg);
    }

    /// @brief Thread reflecting server function.
    void reflectServer();

    /// @brief IOService object.
    IOServicePtr io_service_;
};  // UnixControlSocketTest

/// @brief Server method running in a thread reflecting the command.
///
/// It creates the server socket, accepts client connection, read
/// the command and send it back in a received JSON map.
void
UnixControlSocketTest::reflectServer() {
    // Acceptor.
    boost::asio::local::stream_protocol::acceptor
        acceptor(io_service_->getInternalIOService());
    EXPECT_NO_THROW_LOG(acceptor.open());
    boost::asio::local::stream_protocol::endpoint
        endpoint(unixSocketFilePath());
    EXPECT_NO_THROW_LOG(acceptor.bind(endpoint));
    EXPECT_NO_THROW_LOG(acceptor.listen());
    boost::asio::local::stream_protocol::socket
        socket(io_service_->getInternalIOService());

    // Ready.
    signalReady();

    // Timeout.
    IntervalTimer timer(io_service_);
    bool timeout = false;
    timer.setup([&timeout]() {
            timeout = true;
            FAIL() << "timeout";
        }, TEST_TIMEOUT, IntervalTimer::ONE_SHOT);

    // Accept.
    bool accepted = false;
    boost::system::error_code ec;
    acceptor.async_accept(socket,
                          [&ec, &accepted]
                          (const boost::system::error_code& error) {
                              ec = error;
                              accepted = true;
                          });
    while (!accepted && !timeout) {
        io_service_->runOne();
    }
    ASSERT_FALSE(ec);

    // Receive command.
    string rbuf(1024, ' ');
    size_t received = 0;
    socket.async_receive(boost::asio::buffer(&rbuf[0], rbuf.size()),
                         [&ec, &received]
                         (const boost::system::error_code& error, size_t cnt) {
                             ec = error;
                             received = cnt;
                         });
    while (!received && !timeout) {
        io_service_->runOne();
    }
    ASSERT_FALSE(ec);
    rbuf.resize(received);

    // Reflect.
    ElementPtr map = Element::createMap();
    map->set("received", Element::create(rbuf));
    string sbuf = map->str();

    // Send back.
    size_t sent = 0;
    socket.async_send(boost::asio::buffer(&sbuf[0], sbuf.size()),
                      [&ec, &sent]
                      (const boost::system::error_code& error, size_t cnt) {
                          ec = error;
                          sent = cnt;
                      });
    while (!sent && !timeout) {
        io_service_->runOne();
    }
    ASSERT_FALSE(ec);

    // Stop timer.
    timer.cancel();

    // Close socket.
    if (socket.is_open()) {
        EXPECT_NO_THROW_LOG(socket.close());
    }

    EXPECT_FALSE(timeout);
    EXPECT_TRUE(accepted);
    EXPECT_TRUE(received);
    EXPECT_TRUE(sent);
    EXPECT_EQ(sent, sbuf.size());
}

// Verifies that the createControlSocket template can create an unix
// control socket.
TEST_F(UnixControlSocketTest, createControlSocket) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    ControlSocketBasePtr cs = controlSocketFactory(cfg);
    ASSERT_TRUE(cs);
    UnixControlSocketPtr ucs =
        dynamic_pointer_cast<UnixControlSocket>(cs);
    EXPECT_TRUE(ucs);
}

// Verifies that unix control sockets handle configGet() as expected.
TEST_F(UnixControlSocketTest, configGet) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    UnixControlSocketPtr ucs(new UnixControlSocket(cfg));
    ASSERT_TRUE(ucs);

    // Run a reflecting server in a thread.
    thread_.reset(new thread([this]() { reflectServer(); }));

    waitReady();

    // Try configGet.
    ConstElementPtr reflected;
    EXPECT_NO_THROW_LOG(reflected = ucs->configGet("foo"));
    ASSERT_TRUE(reflected);
    ASSERT_EQ(Element::map, reflected->getType());
    ConstElementPtr command = reflected->get("received");
    ASSERT_TRUE(command);
    ASSERT_EQ(Element::string, command->getType());
    string expected = "{ \"command\": \"config-get\" }";
    EXPECT_EQ(expected, command->stringValue());
}

// Verifies that unix control sockets handle configTest() as expected.
TEST_F(UnixControlSocketTest, configTest) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    UnixControlSocketPtr ucs(new UnixControlSocket(cfg));
    ASSERT_TRUE(ucs);

    // Run a reflecting server in a thread.
    thread_.reset(new thread([this]() { reflectServer(); }));

    waitReady();

    // Prepare a config to test.
    ElementPtr json = Element::fromJSON("{ \"bar\": 1 }");

    ConstElementPtr reflected;
    EXPECT_NO_THROW_LOG(reflected = ucs->configTest(json, "foo"));
    ASSERT_TRUE(reflected);
    ASSERT_EQ(Element::map, reflected->getType());
    ConstElementPtr command = reflected->get("received");
    ASSERT_TRUE(command);
    ASSERT_EQ(Element::string, command->getType());
    string expected = "{ \"arguments\": { \"bar\": 1 }, "
        "\"command\": \"config-test\" }";
    EXPECT_EQ(expected, command->stringValue());
}

// Verifies that unix control sockets handle configSet() as expected.
TEST_F(UnixControlSocketTest, configSet) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    UnixControlSocketPtr ucs(new UnixControlSocket(cfg));
    ASSERT_TRUE(ucs);

    // Run a reflecting server in a thread.
    thread_.reset(new thread([this]() { reflectServer(); }));

    waitReady();

    // Prepare a config to set.
    ElementPtr json = Element::fromJSON("{ \"bar\": 1 }");

    ConstElementPtr reflected;
    EXPECT_NO_THROW_LOG(reflected = ucs->configSet(json, "foo"));
    ASSERT_TRUE(reflected);
    ASSERT_EQ(Element::map, reflected->getType());
    ConstElementPtr command = reflected->get("received");
    ASSERT_TRUE(command);
    ASSERT_EQ(Element::string, command->getType());
    string expected = "{ \"arguments\": { \"bar\": 1 }, "
        "\"command\": \"config-set\" }";
    EXPECT_EQ(expected, command->stringValue());
}

// Verifies that unix control sockets handle timeouts.
TEST_F(UnixControlSocketTest, timeout) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    UnixControlSocketPtr ucs(new UnixControlSocket(cfg));
    ASSERT_TRUE(ucs);

    // Run a timeout server in a thread.
    thread_.reset(new thread([this]() { waitReady(); }));

    // Try configGet: it should get a communication error,
    EXPECT_THROW_MSG(ucs->configGet("foo"), ControlSocketError,
                     "communication error: No such file or directory");
    signalReady();
}

//////////////////////////////// HTTP ////////////////////////////////

/// @brief IP address to which HTTP service is bound.
const string SERVER_ADDRESS = "127.0.0.1";

/// @brief Port number to which HTTP service is bound.
const uint16_t SERVER_PORT = 18123;

/// @brief Test HTTP JSON response.
using Response = TestHttpResponseBase<HttpResponseJson>;

/// @brief Pointer to test HTTP JSON response.
using ResponsePtr = boost::shared_ptr<Response>;

/// @brief Generic test HTTP response.
using GenericResponse = TestHttpResponseBase<HttpResponse>;

/// @brief Pointer to generic test HTTP response.
using GenericResponsePtr = boost::shared_ptr<GenericResponse>;

/// @brief Implementation of the HttpResponseCreator.
///
/// Send back the request in a received JSON map.
class TestHttpResponseCreator : public HttpResponseCreator {
public:
    /// @brief Create a new request.
    ///
    /// @return Pointer to the new instance of the HttpRequest.
    HttpRequestPtr createNewHttpRequest() const override final {
        return (HttpRequestPtr(new PostHttpRequestJson()));
    }

protected:
    /// @brief Creates HTTP response.
    ///
    /// @param request Pointer to the HTTP request.
    /// @return Pointer to the generated HTTP response.
    HttpResponsePtr
    createStockHttpResponse(const HttpRequestPtr& request,
                            const HttpStatusCode& status_code) const override final {
        // Data is in the request context.
        HttpVersion http_version(request->context()->http_version_major_,
                                 request->context()->http_version_minor_);
        ResponsePtr response(new Response(http_version, status_code));
        response->finalize();
        return (response);
    }

    /// @brief Creates HTTP response.
    ///
    /// Build a response with reflected request in a received JSON map.
    /// It can be told to respond with a partial JSON.
    ///
    /// @param request Pointer to the HTTP request.
    /// @return Pointer to an object representing HTTP response.
    virtual HttpResponsePtr
    createDynamicHttpResponse(HttpRequestPtr request) override {
        // Request must always be JSON.
        PostHttpRequestJsonPtr request_json =
            boost::dynamic_pointer_cast<PostHttpRequestJson>(request);
        if (!request_json) {
            isc_throw(Unexpected, "request is not JSON");
        }
        ConstElementPtr body = request_json->getBodyAsJson();
        if (!body) {
            isc_throw(Unexpected, "can't get JSON from request");
        }

        // Check for the special partial JSON.
        ConstElementPtr arguments = body->get("arguments");
        if (arguments && (arguments->contains("want-partial"))) {
            // Use a generic response.
            GenericResponsePtr
                response(new GenericResponse(request->getHttpVersion(),
                                             HttpStatusCode::OK));
            HttpResponseContextPtr ctx = response->context();
            // Generate JSON response.
            ctx->headers_.push_back(HttpHeaderContext("Content-Type",
                                                      "application/json"));
            // Not closed JSON map so it will fail.
            ctx->body_ = "{";
            response->finalize();
            // Take into account the missing '}'.
            response->setContentLength(2);
            return (response);
        }

        // Reflect.
        ResponsePtr response(new Response(request->getHttpVersion(),
                                          HttpStatusCode::OK));
        ElementPtr map = Element::createMap();
        map->set("received", Element::create(body->str()));
        response->setBodyAsJson(map);
        response->finalize();
        return (response);
    }
};  // TestHttpResponseCreator

/// @brief Implementation of the test HttpResponseCreatorFactory.
class TestHttpResponseCreatorFactory : public HttpResponseCreatorFactory {
public:

    /// @brief Creates @ref TestHttpResponseCreator instance.
    HttpResponseCreatorPtr create() const override final {
        HttpResponseCreatorPtr response_creator(new TestHttpResponseCreator());
        return (response_creator);
    }
};  // TestHttpResponseCreatorFactory

/// @brief Test fixture class for http control sockets.
class HttpControlSocketTest : public ThreadedTest {
public:
    /// @brief Constructor.
    HttpControlSocketTest() : io_service_(new IOService()) {
    }

    void SetUp() override {
        SysrepoSetup::cleanSharedMemory();
    }

    void TearDown() override {
        SysrepoSetup::cleanSharedMemory();
        if (thread_) {
            thread_->join();
            thread_.reset();
        }
        if (listener_) {
            listener_->stop();
        }
        io_service_->stopAndPoll();
        listener_.reset();
    }

    /// @brief Returns socket URL.
    static Url httpSocketUrl() {
        ostringstream s;
        s << "http://" << SERVER_ADDRESS << ":" << SERVER_PORT << "/";
        return (Url(s.str()));
    }

    /// @brief Create configuration of the control socket.
    ///
    /// @return a pointer to a control socket configuration.
    CfgControlSocketPtr createCfgControlSocket() {
        CfgControlSocketPtr cfg;
        cfg.reset(new CfgControlSocket(CfgControlSocket::Type::HTTP,
                                       "", httpSocketUrl()));
        return (cfg);
    }

    /// @brief Create the reflecting listener.
    void createReflectListener();

    /// @brief Start listener.
    ///
    /// Run IO in a thread.
    void start() {
        // If the thread is ready to go, start the listener.
        if (listener_) {
            ASSERT_NO_THROW_LOG(listener_->start());
        }

        thread_.reset(new thread([this]() {
            // The thread is ready to go. Signal it to the main
            // thread so it can start the actual test.
            signalReady();
            // Until stop() is called run IO service.
            while (!isStopping()) {
                io_service_->runOne();
            }
            // Main thread signaled that the thread should
            // terminate. Launch any outstanding IO service
            // handlers.
            io_service_->poll();
            // Nothing more to do. Signal that the thread is
            // done so as the main thread can close HTTP
            // listener and clean up after the test.
            signalStopped();
        }));

        // Main thread waits here for the thread to start.
        waitReady();
    }

    /// @brief Stop listener.
    ///
    /// Post an empty action to finish current runOne.
    void stop() {
        // Notify the thread that it should terminate.
        signalStopping();
        // If the thread is blocked on running the IO
        // service, post the empty handler to cause
        // runOne to return.
        io_service_->post([]() { return; });
        // We asked that the thread stops. Let's wait
        // for it to signal that it has stopped.
        waitStopped();

        // Thread has terminated. We can stop the HTTP
        // listener safely.
        if (listener_) {
            ASSERT_NO_THROW_LOG(listener_->stop());
        }
    }

    /// @brief IOService object.
    IOServicePtr io_service_;

    /// @brief Pointer to listener.
    HttpListenerPtr listener_;
};  // HttpControlSocketTest

/// @brief Create the reflecting listener.
void
HttpControlSocketTest::createReflectListener() {
    HttpResponseCreatorFactoryPtr
        factory(new TestHttpResponseCreatorFactory());
    listener_.reset(new HttpListener(io_service_,
                                     IOAddress(SERVER_ADDRESS), SERVER_PORT,
                                     TlsContextPtr(), factory,
                                     HttpListener::RequestTimeout(2000),
                                     HttpListener::IdleTimeout(2000)));
}

// Verifies that the createControlSocket template can create a http
// control socket.
TEST_F(HttpControlSocketTest, createControlSocket) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    ControlSocketBasePtr cs = controlSocketFactory(cfg);
    ASSERT_TRUE(cs);
    HttpControlSocketPtr hcs =
        dynamic_pointer_cast<HttpControlSocket>(cs);
    EXPECT_TRUE(hcs);
}

// Verifies that http control sockets handle configGet() as expected.
TEST_F(HttpControlSocketTest, configGet) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    HttpControlSocketPtr hcs(new HttpControlSocket(cfg));
    ASSERT_TRUE(hcs);

    // Run a reflecting server in a thread.
    createReflectListener();
    start();

    // Try configGet.
    ConstElementPtr reflected;
    EXPECT_NO_THROW_LOG(reflected = hcs->configGet("foo"));
    stop();

    // Check result.
    ASSERT_TRUE(reflected);
    ASSERT_EQ(Element::map, reflected->getType());
    ConstElementPtr command = reflected->get("received");
    ASSERT_TRUE(command);
    ASSERT_EQ(Element::string, command->getType());
    string expected = "{ \"command\": \"config-get\", "
        "\"remote-address\": \"127.0.0.1\", \"service\": [ \"foo\" ] }";
    EXPECT_EQ(expected, command->stringValue());
}

// Verifies that http control sockets handle configGet() for a control agent
// as expected.
TEST_F(HttpControlSocketTest, configGetCA) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    HttpControlSocketPtr hcs(new HttpControlSocket(cfg));
    ASSERT_TRUE(hcs);

    // Run a reflecting server in a thread.
    createReflectListener();
    start();

    // Try configGet.
    ConstElementPtr reflected;
    EXPECT_NO_THROW_LOG(reflected = hcs->configGet("ca"));
    stop();

    // Check result.
    ASSERT_TRUE(reflected);
    ASSERT_EQ(Element::map, reflected->getType());
    ConstElementPtr command = reflected->get("received");
    ASSERT_TRUE(command);
    ASSERT_EQ(Element::string, command->getType());
    string expected = "{ \"command\": \"config-get\", \"remote-address\": \"127.0.0.1\" }";
    EXPECT_EQ(expected, command->stringValue());
}

// Verifies that http control sockets handle configTest() as expected.
TEST_F(HttpControlSocketTest, configTest) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    HttpControlSocketPtr hcs(new HttpControlSocket(cfg));
    ASSERT_TRUE(hcs);

    // Run a reflecting server in a thread.
    createReflectListener();
    start();

    // Prepare a config to test.
    ElementPtr json = Element::fromJSON("{ \"bar\": 1 }");

    // Try configTest.
    ConstElementPtr reflected;
    EXPECT_NO_THROW_LOG(reflected = hcs->configTest(json, "foo"));
    stop();

    // Check result.
    ASSERT_TRUE(reflected);
    ASSERT_EQ(Element::map, reflected->getType());
    ConstElementPtr command = reflected->get("received");
    ASSERT_TRUE(command);
    ASSERT_EQ(Element::string, command->getType());
    string expected = "{ \"arguments\": { \"bar\": 1 }, "
        "\"command\": \"config-test\", "
        "\"remote-address\": \"127.0.0.1\", \"service\": [ \"foo\" ] }";
    EXPECT_EQ(expected, command->stringValue());
}

// Verifies that http control sockets handle configTest() for a control agent
// as expected.
TEST_F(HttpControlSocketTest, configTestCA) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    HttpControlSocketPtr hcs(new HttpControlSocket(cfg));
    ASSERT_TRUE(hcs);

    // Run a reflecting server in a thread.
    createReflectListener();
    start();

    // Prepare a config to test.
    ElementPtr json = Element::fromJSON("{ \"bar\": 1 }");

    // Try configTest.
    ConstElementPtr reflected;
    EXPECT_NO_THROW_LOG(reflected = hcs->configTest(json, "ca"));
    stop();

    // Check result.
    ASSERT_TRUE(reflected);
    ASSERT_EQ(Element::map, reflected->getType());
    ConstElementPtr command = reflected->get("received");
    ASSERT_TRUE(command);
    ASSERT_EQ(Element::string, command->getType());
    string expected = "{ \"arguments\": { \"bar\": 1 }, "
        "\"command\": \"config-test\", \"remote-address\": \"127.0.0.1\" }";
    EXPECT_EQ(expected, command->stringValue());
}

// Verifies that http control sockets handle configSet() as expected.
TEST_F(HttpControlSocketTest, configSet) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    HttpControlSocketPtr hcs(new HttpControlSocket(cfg));
    ASSERT_TRUE(hcs);

    // Run a reflecting server in a thread.
    createReflectListener();
    start();

    // Prepare a config to set.
    ElementPtr json = Element::fromJSON("{ \"bar\": 1 }");

    // Try configSet.
    ConstElementPtr reflected;
    EXPECT_NO_THROW_LOG(reflected = hcs->configSet(json, "foo"));
    stop();

    // Check result.
    ASSERT_TRUE(reflected);
    ASSERT_EQ(Element::map, reflected->getType());
    ConstElementPtr command = reflected->get("received");
    ASSERT_TRUE(command);
    ASSERT_EQ(Element::string, command->getType());
    string expected = "{ \"arguments\": { \"bar\": 1 }, "
        "\"command\": \"config-set\", "
        "\"remote-address\": \"127.0.0.1\", \"service\": [ \"foo\" ] }";
    EXPECT_EQ(expected, command->stringValue());
}

// Verifies that http control sockets handle configSet() for a control agent
// as expected.
TEST_F(HttpControlSocketTest, configSetCA) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    HttpControlSocketPtr hcs(new HttpControlSocket(cfg));
    ASSERT_TRUE(hcs);

    // Run a reflecting server in a thread.
    createReflectListener();
    start();

    // Prepare a config to set.
    ElementPtr json = Element::fromJSON("{ \"bar\": 1 }");

    // Try configSet.
    ConstElementPtr reflected;
    EXPECT_NO_THROW_LOG(reflected = hcs->configSet(json, "ca"));
    stop();

    // Check result.
    ASSERT_TRUE(reflected);
    ASSERT_EQ(Element::map, reflected->getType());
    ConstElementPtr command = reflected->get("received");
    ASSERT_TRUE(command);
    ASSERT_EQ(Element::string, command->getType());
    string expected = "{ \"arguments\": { \"bar\": 1 }, "
        "\"command\": \"config-set\", \"remote-address\": \"127.0.0.1\" }";
    EXPECT_EQ(expected, command->stringValue());
}

// Verifies that http control sockets handle can't connect errors.
TEST_F(HttpControlSocketTest, connectionRefused) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    HttpControlSocketPtr hcs(new HttpControlSocket(cfg));
    ASSERT_TRUE(hcs);

    // Try configGet: it should get a communication error,
    try {
        hcs->configGet("foo");
    } catch (const ControlSocketError& ex) {
        EXPECT_EQ("communication error (code): Connection refused",
                  string(ex.what()));
    } catch (exception const& ex) {
        FAIL() << "unexpected exception: " << ex.what();
    } catch (...) {
        FAIL() << "unexpected exception";
    }
}

// Verifies that http control sockets handle timeout errors.
TEST_F(HttpControlSocketTest, partial) {
    CfgControlSocketPtr cfg = createCfgControlSocket();
    ASSERT_TRUE(cfg);
    HttpControlSocketPtr hcs(new HttpControlSocket(cfg));
    ASSERT_TRUE(hcs);

    // Create the server but do not start it.
    createReflectListener();
    start();

    // Prepare a special config to set.
    ElementPtr json = Element::fromJSON("{ \"want-partial\": true }");

    // Warn this makes time.
    cout << "Waiting 2s..." << endl;

    // Try configSet: it should get a communication error,
    try {
        hcs->configSet(json, "foo");
    } catch (const ControlSocketError& ex) {
        EXPECT_EQ("communication error (code): End of file",
                  string(ex.what()));
    } catch (exception const& ex) {
        FAIL() << "unexpected exception: " << ex.what();
    } catch (...) {
        FAIL() << "unexpected exception";
    }
    stop();
}

}  // namespace