blob: 13a552660200660ae44c7eb9c33eb97e858a2266 [file] [log] [blame]
Vince Lehman27f1add2014-10-16 17:14:46 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 **/
21
22#include "tests/test-common.hpp"
23#include "tests/control-commands.hpp"
24#include "tests/dummy-face.hpp"
25
26#include "utility/face-controller.hpp"
27
Junxiao Shiee29a032014-11-12 00:48:59 -070028#include <ndn-cxx/security/key-chain.hpp>
Vince Lehman27f1add2014-10-16 17:14:46 -050029#include <ndn-cxx/management/nfd-controller.hpp>
30
31namespace nlsr {
32namespace test {
33
34using ndn::bind;
35using ndn::shared_ptr;
36using ndn::Interest;
37
38class FaceControllerFixture : public BaseFixture
39{
40public:
41 FaceControllerFixture()
42 : face(ndn::makeDummyFace())
43 , interests(face->m_sentInterests)
Junxiao Shiee29a032014-11-12 00:48:59 -070044 , controller(*face, keyChain)
Vince Lehman27f1add2014-10-16 17:14:46 -050045 , faceManager(g_ioService, controller)
46 {
47 }
48
49 void
50 expectCanonizeSuccess(const std::string& request, const std::string expectedUri)
51 {
52 faceManager.createFace(request, 0, bind(&FaceControllerFixture::onFailure, this, _1, _2));
53 expectedUris.push_back(expectedUri);
54 }
55
56 void
57 expectCanonizeFailure(const std::string& request)
58 {
59 faceManager.createFace(request, 0, bind(&FaceControllerFixture::onFailure, this, _1, _2));
60 }
61
62 void
63 onFailure(uint32_t code, const std::string& reason)
64 {
65 BOOST_CHECK_EQUAL(code, 408);
66 }
67
68public:
69 shared_ptr<ndn::DummyFace> face;
Junxiao Shiee29a032014-11-12 00:48:59 -070070 ndn::KeyChain keyChain;
Vince Lehman27f1add2014-10-16 17:14:46 -050071 std::vector<Interest>& interests;
72 ndn::nfd::Controller controller;
73 util::FaceController faceManager;
74
75 std::list<std::string> expectedUris;
76};
77
78BOOST_FIXTURE_TEST_SUITE(TestFaceController, FaceControllerFixture)
79
80BOOST_AUTO_TEST_CASE(FaceCreateCanonize)
81{
82 expectCanonizeSuccess("udp4://192.0.2.1:6363", "udp4://192.0.2.1:6363");
83 expectCanonizeSuccess("tcp4://192.0.2.1:6363", "tcp4://192.0.2.1:6363");
84
85 expectCanonizeSuccess("udp://192.0.2.1", "udp4://192.0.2.1:6363");
86 expectCanonizeSuccess("udp://203.0.113.1:6363", "udp4://203.0.113.1:6363");
87 expectCanonizeSuccess("udp4://google-public-dns-a.google.com", "udp4://8.8.8.8:6363");
88
89 expectCanonizeSuccess("tcp://192.0.2.1", "tcp4://192.0.2.1:6363");
90 expectCanonizeSuccess("tcp://203.0.113.1:6363", "tcp4://203.0.113.1:6363");
91 expectCanonizeSuccess("tcp4://google-public-dns-a.google.com", "tcp4://8.8.8.8:6363");
92
93 expectCanonizeSuccess("udp6://[2001:4860:4860::8888]:6363", "udp6://[2001:4860:4860::8888]:6363");
94 expectCanonizeSuccess("tcp6://[2001:4860:4860::8888]:6363", "tcp6://[2001:4860:4860::8888]:6363");
95
96 expectCanonizeSuccess("udp://[2001:4860:4860::8888]", "udp6://[2001:4860:4860::8888]:6363");
97 expectCanonizeSuccess("udp://[2001:4860:4860::8888]:6363", "udp6://[2001:4860:4860::8888]:6363");
98 expectCanonizeSuccess("udp6://google-public-dns-a.google.com",
99 "udp6://[2001:4860:4860::8888]:6363");
100
101 expectCanonizeSuccess("tcp://[2001:4860:4860::8888]", "tcp6://[2001:4860:4860::8888]:6363");
102 expectCanonizeSuccess("tcp://[2001:4860:4860::8888]:6363", "tcp6://[2001:4860:4860::8888]:6363");
103 expectCanonizeSuccess("tcp6://google-public-dns-a.google.com",
104 "tcp6://[2001:4860:4860::8888]:6363");
105
106 g_ioService.run();
107 face->processEvents(ndn::time::milliseconds(1));
108
109 BOOST_CHECK_EQUAL(interests.size(), expectedUris.size());
110
111 for (std::vector<ndn::Interest>::iterator it = interests.begin(); it != interests.end(); ++it) {
112
113 ndn::nfd::ControlParameters extractedParameters;
114 ndn::Name::Component verb;
115
116 extractFaceCommandParameters(*it, verb, extractedParameters);
117
118 BOOST_CHECK_EQUAL(verb, ndn::Name::Component("create"));
119
120 std::list<std::string>::iterator uri = std::find(expectedUris.begin(),
121 expectedUris.end(),
122 extractedParameters.getUri());
123
124 BOOST_REQUIRE(uri != expectedUris.end());
125 expectedUris.erase(uri);
126 }
127}
128
129BOOST_AUTO_TEST_CASE(FaceCreateFailure)
130{
131 expectCanonizeFailure("udp4://not-a-valid.uri");
132 expectCanonizeFailure("udp4://256.0.0.1:6363");
133
134 g_ioService.run();
135 face->processEvents(ndn::time::milliseconds(1));
136
137 BOOST_CHECK_EQUAL(interests.size(), 0);
138}
139
140BOOST_AUTO_TEST_SUITE_END()
141
142} //namespace test
143} //namespace nlsr