blob: 1dff8f987d5d31f7ce606a3b7adc0f277ce7d302 [file] [log] [blame]
Junxiao Shi8127d1a2018-08-24 15:29:21 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "prefix-announcement.hpp"
23#include "encoding/tlv-nfd.hpp"
24
25#include "boost-test.hpp"
26#include "identity-management-fixture.hpp"
27#include "make-interest-data.hpp"
28
29namespace ndn {
30namespace tests {
31
32BOOST_AUTO_TEST_SUITE(TestPrefixAnnouncement)
33
34static Data
35makePrefixAnnData()
36{
37 return Data(
38 "067A 071A announced-name=/net/example 08036E6574 08076578616D706C65"
39 " keyword-prefix-ann=20025041 version=0802FD01 segment=08020000"
40 " 1403 content-type=prefix-ann 180105"
41 " 1530 expire in one hour 6D040036EE80"
42 " validity FD00FD26 FD00FE0F323031383130333054303030303030"
43 " FD00FF0F323031383131323454323335393539"
44 " 1603 1B0100 signature"
45 " 1720 0000000000000000000000000000000000000000000000000000000000000000"_block);
46}
47
48BOOST_FIXTURE_TEST_CASE(Encode, IdentityManagementFixture)
49{
50 PrefixAnnouncement pa;
51 BOOST_CHECK(!pa.getData());
52 const Data& data0 = pa.toData(m_keyChain, signingWithSha256(), 5);
53 BOOST_CHECK_EQUAL(data0.getName(), "/32=PA/%FD%05/%00%00");
54 BOOST_REQUIRE(pa.getData());
55 BOOST_CHECK_EQUAL(*pa.getData(), data0);
56
57 pa.setAnnouncedName("/net/example");
58 BOOST_CHECK_THROW(pa.setExpiration(-1_ms), std::invalid_argument);
59 pa.setExpiration(1_h);
60 const Data& data1 = pa.toData(m_keyChain, signingWithSha256(), 1);
61 BOOST_CHECK_EQUAL(data1.getName(), "/net/example/32=PA/%FD%01/%00%00");
62 const Block& payload1 = data1.getContent();
63 payload1.parse();
64 BOOST_CHECK_EQUAL(readNonNegativeInteger(payload1.get(tlv::nfd::ExpirationPeriod)), 3600000);
65 BOOST_CHECK(payload1.find(tlv::ValidityPeriod) == payload1.elements_end());
66
67 pa.setValidityPeriod(security::ValidityPeriod(time::fromIsoString("20181030T000000"),
68 time::fromIsoString("20181124T235959")));
69 const Data& data2 = pa.toData(m_keyChain);
70 const Block& payload2 = data2.getContent();
71 payload2.parse();
72 BOOST_CHECK_EQUAL(readNonNegativeInteger(payload2.get(tlv::nfd::ExpirationPeriod)), 3600000);
73 BOOST_CHECK_EQUAL(payload2.get(tlv::ValidityPeriod),
74 "FD00FD26 FD00FE0F323031383130333054303030303030"
75 " FD00FF0F323031383131323454323335393539"_block);
76}
77
78BOOST_AUTO_TEST_CASE(DecodeGood)
79{
80 Data data0 = makePrefixAnnData();
81 PrefixAnnouncement pa0(data0);
82 BOOST_CHECK_EQUAL(pa0.getAnnouncedName(), "/net/example");
83 BOOST_CHECK_EQUAL(pa0.getExpiration(), 1_h);
84 BOOST_CHECK(pa0.getValidityPeriod());
85 BOOST_CHECK_EQUAL(*pa0.getValidityPeriod(),
86 security::ValidityPeriod(time::fromIsoString("20181030T000000"),
87 time::fromIsoString("20181124T235959")));
88
89 // reorder ExpirationPeriod and ValidityPeriod, unrecognized non-critical element
90 Data data1 = makePrefixAnnData();
91 Block payload1 = data1.getContent();
92 payload1.parse();
93 Block expirationElement = payload1.get(tlv::nfd::ExpirationPeriod);
94 payload1.remove(tlv::nfd::ExpirationPeriod);
95 payload1.push_back(expirationElement);
96 payload1.push_back("2000"_block);
97 payload1.encode();
98 data1.setContent(payload1);
99 PrefixAnnouncement pa1(data1);
100 BOOST_CHECK_EQUAL(pa1.getAnnouncedName(), "/net/example");
101 BOOST_CHECK_EQUAL(pa1.getExpiration(), 1_h);
102 BOOST_CHECK(pa1.getValidityPeriod());
103 BOOST_CHECK_EQUAL(*pa1.getValidityPeriod(),
104 security::ValidityPeriod(time::fromIsoString("20181030T000000"),
105 time::fromIsoString("20181124T235959")));
106
107 // no ValidityPeriod
108 Data data2 = makePrefixAnnData();
109 Block payload2 = data2.getContent();
110 payload2.parse();
111 payload2.remove(tlv::ValidityPeriod);
112 payload2.encode();
113 data2.setContent(payload2);
114 PrefixAnnouncement pa2(data2);
115 BOOST_CHECK_EQUAL(pa2.getAnnouncedName(), "/net/example");
116 BOOST_CHECK_EQUAL(pa2.getExpiration(), 1_h);
117 BOOST_CHECK(!pa2.getValidityPeriod());
118}
119
120BOOST_AUTO_TEST_CASE(DecodeBad)
121{
122 // wrong ContentType
123 Data data0 = makePrefixAnnData();
124 data0.setContentType(tlv::ContentType_Blob);
125 BOOST_CHECK_THROW(PrefixAnnouncement pa0(data0), tlv::Error);
126
127 // Name has no "32=PA" keyword
128 Data data1 = makePrefixAnnData();
129 setNameComponent(data1, -3, name::Component::fromEscapedString("32=not-PA"));
130 BOOST_CHECK_THROW(PrefixAnnouncement pa1(data1), tlv::Error);
131
132 // Name has no version component
133 Data data2 = makePrefixAnnData();
134 setNameComponent(data2, -2, "not-version");
135 BOOST_CHECK_THROW(PrefixAnnouncement pa2(data2), tlv::Error);
136
137 // Name has no segment number component
138 Data data3 = makePrefixAnnData();
139 setNameComponent(data3, -2, "not-segment");
140 BOOST_CHECK_THROW(PrefixAnnouncement pa3(data3), tlv::Error);
141
142 // Content has no ExpirationPeriod element
143 Data data4 = makePrefixAnnData();
144 Block payload4 = data4.getContent();
145 payload4.parse();
146 payload4.remove(tlv::nfd::ExpirationPeriod);
147 payload4.encode();
148 data4.setContent(payload4);
149 BOOST_CHECK_THROW(PrefixAnnouncement pa4(data4), tlv::Error);
150
151 // ExpirationPeriod is malformed
152 Data data5 = makePrefixAnnData();
153 Block payload5 = data5.getContent();
154 payload5.parse();
155 payload5.remove(tlv::nfd::ExpirationPeriod);
156 payload5.push_back("6D03010101"_block);
157 payload5.encode();
158 data5.setContent(payload5);
159 BOOST_CHECK_THROW(PrefixAnnouncement pa5(data5), tlv::Error);
160
161 // ValidityPeriod is malformed
162 Data data6 = makePrefixAnnData();
163 Block payload6 = data6.getContent();
164 payload6.parse();
165 payload6.remove(tlv::ValidityPeriod);
166 payload6.push_back("FD00FD00"_block);
167 payload6.encode();
168 data6.setContent(payload6);
169 BOOST_CHECK_THROW(PrefixAnnouncement pa6(data6), tlv::Error);
170
171 // Content has unrecognized critical element
172 Data data7 = makePrefixAnnData();
173 Block payload7 = data7.getContent();
174 payload7.parse();
175 payload7.push_back("0200"_block);
176 payload7.encode();
177 data7.setContent(payload7);
178 BOOST_CHECK_THROW(PrefixAnnouncement pa7(data7), tlv::Error);
179}
180
181BOOST_AUTO_TEST_CASE(Modify)
182{
183 PrefixAnnouncement pa0(makePrefixAnnData());
184 BOOST_REQUIRE(pa0.getData());
185 BOOST_CHECK_EQUAL(*pa0.getData(), makePrefixAnnData());
186 pa0.setAnnouncedName("/com/example");
187 BOOST_CHECK(!pa0.getData());
188
189 PrefixAnnouncement pa1(makePrefixAnnData());
190 pa1.setExpiration(5_min);
191 BOOST_CHECK(!pa1.getData());
192
193 PrefixAnnouncement pa2(makePrefixAnnData());
194 pa2.setValidityPeriod(security::ValidityPeriod(time::fromIsoString("20180118T000000"),
195 time::fromIsoString("20180212T235959")));
196 BOOST_CHECK(!pa2.getData());
197}
198
199BOOST_AUTO_TEST_SUITE_END() // TestPrefixAnnouncement
200
201} // namespace tests
202} // namespace ndn