blob: 841b507db1fd27be48b64b205ea0ee0bc2ef5fb8 [file] [log] [blame]
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070011 */
12
13#include "management/nfd-face-event-notification.hpp"
14
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070015#include "boost-test.hpp"
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070016
17namespace ndn {
18namespace nfd {
19
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070020BOOST_AUTO_TEST_SUITE(ManagementTestNfdFaceEventNotification)
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070021
22BOOST_AUTO_TEST_CASE(Flags)
23{
24 FaceEventNotification notification;
25
26 notification.setFlags(0);
27 BOOST_CHECK_EQUAL(notification.isLocal(), false);
28 BOOST_CHECK_EQUAL(notification.isOnDemand(), false);
29
30 notification.setFlags(FACE_IS_LOCAL);
31 BOOST_CHECK_EQUAL(notification.isLocal(), true);
32 BOOST_CHECK_EQUAL(notification.isOnDemand(), false);
33
34 notification.setFlags(FACE_IS_ON_DEMAND);
35 BOOST_CHECK_EQUAL(notification.isLocal(), false);
36 BOOST_CHECK_EQUAL(notification.isOnDemand(), true);
37
38 notification.setFlags(FACE_IS_LOCAL | FACE_IS_ON_DEMAND);
39 BOOST_CHECK_EQUAL(notification.isLocal(), true);
40 BOOST_CHECK_EQUAL(notification.isOnDemand(), true);
41}
42
43BOOST_AUTO_TEST_CASE(EncodeCreated)
44{
45 FaceEventNotification notification1;
46 notification1.setKind(FACE_EVENT_CREATED)
47 .setFaceId(20)
48 .setRemoteUri("tcp4://192.0.2.1:55555")
49 .setLocalUri("tcp4://192.0.2.2:6363")
50 .setFlags(FACE_IS_ON_DEMAND);
51 Block wire;
52 BOOST_REQUIRE_NO_THROW(wire = notification1.wireEncode());
53
54 // These octets are obtained by the snippet below.
55 // This check is intended to detect unexpected encoding change in the future.
56 //for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
57 // printf("0x%02x, ", *it);
58 //}
59 static const uint8_t expected[] = {
60 0xc0, 0x38, 0xc1, 0x01, 0x01, 0x69, 0x01, 0x14, 0x72, 0x16, 0x74, 0x63,
61 0x70, 0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32,
62 0x2e, 0x31, 0x3a, 0x35, 0x35, 0x35, 0x35, 0x35, 0x81, 0x15, 0x74, 0x63,
63 0x70, 0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32,
64 0x2e, 0x32, 0x3a, 0x36, 0x33, 0x36, 0x33, 0xc2, 0x01, 0x02,
65 };
66 BOOST_REQUIRE_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
67 wire.begin(), wire.end());
68
69 BOOST_REQUIRE_NO_THROW(FaceEventNotification(wire));
70 FaceEventNotification notification2(wire);
71 BOOST_CHECK_EQUAL(notification1.getKind(), notification2.getKind());
72 BOOST_CHECK_EQUAL(notification1.getFaceId(), notification2.getFaceId());
73 BOOST_CHECK_EQUAL(notification1.getRemoteUri(), notification2.getRemoteUri());
74 BOOST_CHECK_EQUAL(notification1.getLocalUri(), notification2.getLocalUri());
75 BOOST_CHECK_EQUAL(notification1.getFlags(), notification2.getFlags());
76
77 std::ostringstream os;
78 os << notification2;
79 BOOST_CHECK_EQUAL(os.str(), "FaceEventNotification("
80 "Kind: created, "
81 "FaceID: 20, "
82 "RemoteUri: tcp4://192.0.2.1:55555, "
83 "LocalUri: tcp4://192.0.2.2:6363, "
84 "Flags: 2)");
85}
86
87BOOST_AUTO_TEST_CASE(EncodeDestroyed)
88{
89 FaceEventNotification notification1;
90 notification1.setKind(FACE_EVENT_DESTROYED)
91 .setFaceId(20)
92 .setRemoteUri("tcp4://192.0.2.1:55555")
93 .setLocalUri("tcp4://192.0.2.2:6363")
94 .setFlags(FACE_IS_ON_DEMAND);
95 Block wire;
96 BOOST_REQUIRE_NO_THROW(wire = notification1.wireEncode());
97
98 // These octets are obtained by the snippet below.
99 // This check is intended to detect unexpected encoding change in the future.
100 //for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
101 // printf("0x%02x, ", *it);
102 //}
103 static const uint8_t expected[] = {
104 0xc0, 0x38, 0xc1, 0x01, 0x02, 0x69, 0x01, 0x14, 0x72, 0x16, 0x74, 0x63,
105 0x70, 0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32,
106 0x2e, 0x31, 0x3a, 0x35, 0x35, 0x35, 0x35, 0x35, 0x81, 0x15, 0x74, 0x63,
107 0x70, 0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32,
108 0x2e, 0x32, 0x3a, 0x36, 0x33, 0x36, 0x33, 0xc2, 0x01, 0x02,
109 };
110 BOOST_REQUIRE_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
111 wire.begin(), wire.end());
112
113 BOOST_REQUIRE_NO_THROW(FaceEventNotification(wire));
114 FaceEventNotification notification2(wire);
115 BOOST_CHECK_EQUAL(notification1.getKind(), notification2.getKind());
116 BOOST_CHECK_EQUAL(notification1.getFaceId(), notification2.getFaceId());
117 BOOST_CHECK_EQUAL(notification1.getRemoteUri(), notification2.getRemoteUri());
118 BOOST_CHECK_EQUAL(notification1.getLocalUri(), notification2.getLocalUri());
119 BOOST_CHECK_EQUAL(notification1.getFlags(), notification2.getFlags());
120
121 std::ostringstream os;
122 os << notification2;
123 BOOST_CHECK_EQUAL(os.str(), "FaceEventNotification("
124 "Kind: destroyed, "
125 "FaceID: 20, "
126 "RemoteUri: tcp4://192.0.2.1:55555, "
127 "LocalUri: tcp4://192.0.2.2:6363, "
128 "Flags: 2)");
129}
130
131BOOST_AUTO_TEST_SUITE_END()
132
133} // namespace nfd
134} // namespace ndn