blob: a3ca37135504a82b0dc59b475aa734cdf172d11e [file] [log] [blame]
Wentao Shang77949212014-02-01 23:42:24 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -08002/**
3 * Copyright (C) 2013 Regents of the University of California.
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -08004 * See COPYING for copyright and distribution information.
5 */
6
Alexander Afanasyeve289b532014-02-09 22:14:44 -08007#include "management/nfd-control-response.hpp"
8#include "management/nfd-fib-management-options.hpp"
Yingdi Yud3370492014-02-12 17:17:11 -08009#include "management/nfd-face-management-options.hpp"
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070010#include "management/nfd-face-event-notification.hpp"
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070011#include "management/nfd-face-status.hpp"
Alexander Afanasyev1c5a1a92014-03-21 13:32:36 -070012#include "management/nfd-status.hpp"
13#include "data.hpp"
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -080014
Wentao Shang4d5e1de2014-01-28 21:00:03 -080015#include <boost/test/unit_test.hpp>
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -080016#include <boost/test/output_test_stream.hpp>
17
18using namespace std;
19
20namespace ndn {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080021namespace nfd {
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -080022
23BOOST_AUTO_TEST_SUITE(TestNfdControl)
24
Alexander Afanasyev4b456282014-02-13 00:34:34 -080025const uint8_t TestControlResponse[] = {0x65, 0x17,
26 0x66, 0x02, 0x01, 0x94, 0x67, 0x11, 0x4e, 0x6f, 0x74,
27 0x68, 0x69, 0x6e, 0x67, 0x20, 0x6e, 0x6f, 0x74, 0x20,
28 0x66, 0x6f, 0x75, 0x6e, 0x64};
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -080029
Wentao Shang4d5e1de2014-01-28 21:00:03 -080030const uint8_t TestFibManagementOptions[] = {
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -080031 0x68, 0x49, 0x07, 0x16, 0x08, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080032 0x6f, 0x73, 0x74, 0x08, 0x03, 0x72, 0x65, 0x67, 0x08, 0x04, 0x74, 0x65,
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -080033 0x73, 0x74, 0x69, 0x01, 0x00, 0x6a, 0x01, 0x01, 0x6b, 0x29, 0x07, 0x27,
34 0x08, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x08,
35 0x03, 0x6e, 0x66, 0x64, 0x08, 0x08, 0x73, 0x74, 0x72, 0x61, 0x74, 0x65,
36 0x67, 0x79, 0x08, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2d, 0x77, 0x6f,
37 0x72, 0x6c, 0x64
Wentao Shang4d5e1de2014-01-28 21:00:03 -080038};
39
Yingdi Yud3370492014-02-12 17:17:11 -080040const uint8_t TestFaceManagementOptions[] = {
41 0x6c, 0x1e, 0x69, 0x01, 0x0a, 0x72, 0x19, 0x74, 0x63, 0x70, 0x3a, 0x2f,
42 0x2f, 0x31, 0x2e, 0x31, 0x2e, 0x31, 0x2e, 0x31, 0x2f, 0x68, 0x65, 0x6c,
43 0x6c, 0x6f, 0x2f, 0x77, 0x6f, 0x72, 0x6c, 0x64
44};
45
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070046const uint8_t TestFaceEventNotification[] = {
Alexander Afanasyev2c753312014-03-20 17:31:01 -070047 0xc0, 0x20, 0xc1, 0x01, 0x01, 0x69, 0x01, 0x64, 0x72, 0x15, 0x74, 0x63,
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070048 0x70, 0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30,
Alexander Afanasyev2c753312014-03-20 17:31:01 -070049 0x2e, 0x31, 0x3a, 0x36, 0x33, 0x36, 0x33, 0xc2, 0x01, 0x03
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070050};
51
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070052const uint8_t TestFaceStatus[] = {
53 0x80, 0x27, 0x69, 0x01, 0x64, 0x72, 0x15, 0x74, 0x63, 0x70, 0x34, 0x3a,
54 0x2f, 0x2f, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x3a,
55 0x36, 0x33, 0x36, 0x33, 0x91, 0x01, 0x0a, 0x90, 0x01, 0x14, 0x92, 0x01,
56 0x1e, 0x93, 0x02, 0x01, 0x90
57};
58
Wentao Shang4d5e1de2014-01-28 21:00:03 -080059// ControlResponse
60
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -080061BOOST_AUTO_TEST_CASE(ControlResponseEncode)
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -080062{
Alexander Afanasyeve289b532014-02-09 22:14:44 -080063 ControlResponse controlResponse(404, "Nothing not found");
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -080064 const Block &wire = controlResponse.wireEncode();
65
66 BOOST_REQUIRE_EQUAL_COLLECTIONS(TestControlResponse, TestControlResponse+sizeof(TestControlResponse),
67 wire.begin(), wire.end());
68}
69
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -080070BOOST_AUTO_TEST_CASE(ControlResponseDecode)
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -080071{
Alexander Afanasyeve289b532014-02-09 22:14:44 -080072 ControlResponse controlResponse;
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -080073
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -080074 BOOST_REQUIRE_NO_THROW(controlResponse.wireDecode(Block(TestControlResponse, sizeof(TestControlResponse))));
75
76 BOOST_REQUIRE_EQUAL(controlResponse.getCode(), 404);
77 BOOST_REQUIRE_EQUAL(controlResponse.getText(), "Nothing not found");
78}
79
Wentao Shang4d5e1de2014-01-28 21:00:03 -080080// FibManagementOptions
81
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -080082BOOST_AUTO_TEST_CASE(FibManagementOptionsEncoding)
Wentao Shang4d5e1de2014-01-28 21:00:03 -080083{
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -080084 Name n("/localhost/reg/test");
Wentao Shang4d5e1de2014-01-28 21:00:03 -080085 FibManagementOptions opt;
86
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -080087 opt.setName(n);
88 opt.setFaceId(0);
89 opt.setCost(1);
90 opt.setStrategy("/localhost/nfd/strategy/hello-world");
Wentao Shang4d5e1de2014-01-28 21:00:03 -080091
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -080092 const Block& blk = opt.wireEncode();
Wentao Shang4d5e1de2014-01-28 21:00:03 -080093
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -080094 BOOST_REQUIRE_EQUAL_COLLECTIONS(TestFibManagementOptions,
95 TestFibManagementOptions + sizeof(TestFibManagementOptions),
96 blk.begin(), blk.end());
Wentao Shang4d5e1de2014-01-28 21:00:03 -080097}
98
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -080099BOOST_AUTO_TEST_CASE(FibManagementOptionsFastEncoding)
Wentao Shang77949212014-02-01 23:42:24 -0800100{
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800101 Name n("/localhost/reg/test");
Wentao Shang77949212014-02-01 23:42:24 -0800102 FibManagementOptions opt;
103
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800104 opt.setName(n);
105 opt.setFaceId(0);
106 opt.setCost(1);
107 opt.setStrategy("/localhost/nfd/strategy/hello-world");
Wentao Shang77949212014-02-01 23:42:24 -0800108
109 EncodingBuffer blk;
110
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800111 BOOST_REQUIRE_NO_THROW(opt.wireEncode(blk));
Wentao Shang77949212014-02-01 23:42:24 -0800112
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800113 BOOST_REQUIRE_EQUAL_COLLECTIONS(TestFibManagementOptions,
114 TestFibManagementOptions + sizeof(TestFibManagementOptions),
115 blk.begin(), blk.end());
Wentao Shang77949212014-02-01 23:42:24 -0800116
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800117 EncodingBuffer blk2(4);
Wentao Shang77949212014-02-01 23:42:24 -0800118
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800119 BOOST_REQUIRE_NO_THROW(opt.wireEncode(blk2));
Wentao Shang77949212014-02-01 23:42:24 -0800120
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800121 BOOST_REQUIRE_EQUAL_COLLECTIONS(TestFibManagementOptions,
122 TestFibManagementOptions + sizeof(TestFibManagementOptions),
123 blk2.begin(), blk2.end());
Wentao Shang77949212014-02-01 23:42:24 -0800124}
125
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800126BOOST_AUTO_TEST_CASE(FibManagementOptionsDecoding)
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800127{
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800128 Block blk(TestFibManagementOptions, sizeof(TestFibManagementOptions));
129 Name n("/localhost/reg/test");
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800130 FibManagementOptions opt;
131
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800132 BOOST_REQUIRE_NO_THROW(opt.wireDecode(blk));
133
134 BOOST_CHECK_EQUAL(opt.getName(), n);
135 BOOST_CHECK_EQUAL(opt.getFaceId(), 0);
136 BOOST_CHECK_EQUAL(opt.getCost(), 1);
137 BOOST_CHECK_EQUAL(opt.getStrategy().toUri(), "/localhost/nfd/strategy/hello-world");
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800138}
139
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800140BOOST_AUTO_TEST_CASE(FaceManagementOptionsFastEncoding)
Yingdi Yud3370492014-02-12 17:17:11 -0800141{
142 FaceManagementOptions opt;
143
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800144 opt.setFaceId(10);
145 opt.setUri("tcp://1.1.1.1/hello/world");
Yingdi Yud3370492014-02-12 17:17:11 -0800146
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800147 BOOST_REQUIRE_NO_THROW(opt.wireEncode ());
Yingdi Yud3370492014-02-12 17:17:11 -0800148
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800149 BOOST_REQUIRE_EQUAL_COLLECTIONS(TestFaceManagementOptions,
150 TestFaceManagementOptions + sizeof(TestFaceManagementOptions),
151 opt.wireEncode().begin(), opt.wireEncode().end());
Yingdi Yud3370492014-02-12 17:17:11 -0800152}
153
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800154BOOST_AUTO_TEST_CASE(FaceManagementOptionsDecoding)
Yingdi Yud3370492014-02-12 17:17:11 -0800155{
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800156 Block blk(TestFaceManagementOptions, sizeof(TestFaceManagementOptions));
Yingdi Yud3370492014-02-12 17:17:11 -0800157 FaceManagementOptions opt;
158
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700159 BOOST_REQUIRE_NO_THROW(opt.wireDecode(blk));
Alexander Afanasyevc5ad1192014-02-25 14:17:47 -0800160
161 BOOST_CHECK_EQUAL(opt.getFaceId(), 10);
162 BOOST_CHECK_EQUAL(opt.getUri(), "tcp://1.1.1.1/hello/world");
Yingdi Yud3370492014-02-12 17:17:11 -0800163}
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800164
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700165
166BOOST_AUTO_TEST_CASE(FaceEventNotificationEncodingDecoding)
167{
168 FaceEventKind expectedKind = FACE_EVENT_CREATED;
169 std::string expectedUri("tcp4://127.0.0.1:6363");
170 uint64_t expectedFaceId = 100;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700171 uint64_t expectedFlags = 3;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700172
173 {
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700174 FaceEventNotification faceEvent(expectedKind, expectedFaceId, expectedUri, expectedFlags);
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700175 BOOST_REQUIRE_NO_THROW(faceEvent.wireEncode());
176
177 BOOST_REQUIRE_EQUAL_COLLECTIONS(TestFaceEventNotification,
178 TestFaceEventNotification + sizeof(TestFaceEventNotification),
179 faceEvent.wireEncode().begin(), faceEvent.wireEncode().end());
180
181 std::ostringstream os;
182 os << faceEvent;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700183 BOOST_CHECK_EQUAL(os.str(), "FaceEventNotification(Kind: created, FaceID: 100, Uri: tcp4://127.0.0.1:6363, Flags: 3)");
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700184 }
185
186 {
187 Block blk(TestFaceEventNotification, sizeof(TestFaceEventNotification));
188 FaceEventNotification faceEvent(blk);
189
190 BOOST_CHECK_EQUAL(faceEvent.getEventKind(), expectedKind);
191 BOOST_CHECK_EQUAL(faceEvent.getFaceId(), expectedFaceId);
192 BOOST_CHECK_EQUAL(faceEvent.getUri(), expectedUri);
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700193 BOOST_CHECK_EQUAL(faceEvent.getFlags(), expectedFlags);
194 BOOST_CHECK(faceEvent.isLocal());
195 BOOST_CHECK(faceEvent.isOnDemand());
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700196 }
197}
198
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700199BOOST_AUTO_TEST_CASE(FaceStatusEncodingDecoding)
200{
201 {
202 FaceStatus faceStatus(100, "tcp4://127.0.0.1:6363", 10, 20, 30, 400);
203 BOOST_REQUIRE_NO_THROW(faceStatus.wireEncode());
204
205 BOOST_CHECK_EQUAL_COLLECTIONS(TestFaceStatus,
206 TestFaceStatus + sizeof(TestFaceStatus),
207 faceStatus.wireEncode().begin(), faceStatus.wireEncode().end());
208
209 std::ostringstream os;
210 os << faceStatus;
211 BOOST_CHECK_EQUAL(os.str(), "FaceStatus(FaceID: 100, Uri: tcp4://127.0.0.1:6363, "
212 "Counters: 10|20|30|400)");
213 }
214
215 {
216 Block block(TestFaceStatus, sizeof(TestFaceStatus));
217 BOOST_REQUIRE_NO_THROW((FaceStatus(block)));
218
219 FaceStatus faceStatus(block);
220
221 BOOST_CHECK_EQUAL(faceStatus.getFaceId(), 100);
222 BOOST_CHECK_EQUAL(faceStatus.getUri(), "tcp4://127.0.0.1:6363");
223
224 BOOST_CHECK_EQUAL(faceStatus.getInInterest(), 10);
225 BOOST_CHECK_EQUAL(faceStatus.getInData(), 20);
226 BOOST_CHECK_EQUAL(faceStatus.getOutInterest(), 30);
227 BOOST_CHECK_EQUAL(faceStatus.getOutData(), 400);
228 }
229}
230
Alexander Afanasyev1c5a1a92014-03-21 13:32:36 -0700231BOOST_AUTO_TEST_CASE(NfdStatusEncode)
232{
233 Status status1;
234 status1.setNfdVersion(1014210635);
235 status1.setStartTimestamp(time::fromUnixTimestamp(time::seconds(375193249)));
236 status1.setCurrentTimestamp(time::fromUnixTimestamp(time::seconds(1886109034)));
237 status1.setNNameTreeEntries(1849943160);
238 status1.setNFibEntries(621739748);
239 status1.setNPitEntries(482129741);
240 status1.setNMeasurementsEntries(1771725298);
241 status1.setNCsEntries(1264968688);
242 status1.setNInInterests(612811615);
243 status1.setNOutInterests(952144445);
244 status1.setNInDatas(1843576050);
245 status1.setNOutDatas(138198826);
246
247 EncodingBuffer buffer;
248 status1.wireEncode(buffer);
249
250 Data data;
251 data.setContent(buffer.buf(), buffer.size());
252
253 Status status2(data.getContent());
254 BOOST_CHECK_EQUAL(status1.getNfdVersion(), status2.getNfdVersion());
255 BOOST_CHECK_EQUAL(status1.getStartTimestamp(), status2.getStartTimestamp());
256 BOOST_CHECK_EQUAL(status1.getCurrentTimestamp(), status2.getCurrentTimestamp());
257 BOOST_CHECK_EQUAL(status1.getNNameTreeEntries(), status2.getNNameTreeEntries());
258 BOOST_CHECK_EQUAL(status1.getNFibEntries(), status2.getNFibEntries());
259 BOOST_CHECK_EQUAL(status1.getNPitEntries(), status2.getNPitEntries());
260 BOOST_CHECK_EQUAL(status1.getNMeasurementsEntries(), status2.getNMeasurementsEntries());
261 BOOST_CHECK_EQUAL(status1.getNCsEntries(), status2.getNCsEntries());
262 BOOST_CHECK_EQUAL(status1.getNInInterests(), status2.getNInInterests());
263 BOOST_CHECK_EQUAL(status1.getNOutInterests(), status2.getNOutInterests());
264 BOOST_CHECK_EQUAL(status1.getNInDatas(), status2.getNInDatas());
265 BOOST_CHECK_EQUAL(status1.getNOutDatas(), status2.getNOutDatas());
266}
267
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -0800268BOOST_AUTO_TEST_SUITE_END()
269
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800270} // namespace nfd
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -0800271} // namespace ndn