blob: e352c57392ced1c6b77c573db97f4467eecb3dfb [file] [log] [blame]
Yingdi Yube4aeed2015-03-23 13:28:58 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07003 * Copyright (c) 2014-2017, Regents of the University of California
Yingdi Yube4aeed2015-03-23 13:28:58 -07004 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07005 * This file is part of NDN DeLorean, An Authentication System for Data Archives in
6 * Named Data Networking. See AUTHORS.md for complete list of NDN DeLorean authors
7 * and contributors.
Yingdi Yube4aeed2015-03-23 13:28:58 -07008 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07009 * NDN DeLorean is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
Yingdi Yube4aeed2015-03-23 13:28:58 -070013 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070014 * NDN DeLorean is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
16 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Yingdi Yube4aeed2015-03-23 13:28:58 -070017 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070018 * You should have received a copy of the GNU General Public License along with NDN
19 * DeLorean, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yingdi Yube4aeed2015-03-23 13:28:58 -070020 */
21
22#include "logger-response.hpp"
23#include "cryptopp.hpp"
24
25#include "boost-test.hpp"
26
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070027namespace ndn {
28namespace delorean {
Yingdi Yube4aeed2015-03-23 13:28:58 -070029namespace tests {
30
31BOOST_AUTO_TEST_SUITE(TestLoggerResponse)
32
33void
34printByte(const uint8_t* buf, size_t size)
35{
36 std::stringstream ss;
37 using namespace CryptoPP;
38 StringSource is(buf, size, true, new HexEncoder(new FileSink(ss), false));
39
40 std::string output = ss.str();
41 for (size_t i = 0; i < output.size(); i++) {
42 std::cerr << "0x" << output.at(i);
43 std::cerr << output.at(++i) << ", ";
44 if ((i + 1) % 32 == 0)
45 std::cerr << std::endl;
46 }
47}
48
49BOOST_AUTO_TEST_CASE(Basic)
50{
51 LoggerResponse response1(5);
52 BOOST_CHECK_EQUAL(response1.getCode(), 0);
53 BOOST_CHECK_EQUAL(response1.getDataSeqNo(), 5);
54
55 LoggerResponse response2(1, "error");
56 BOOST_CHECK_EQUAL(response2.getCode(), 1);
57 BOOST_CHECK_EQUAL(response2.getMsg(), "error");
58}
59
60uint8_t RESPONSE1[] = {
61 0x90, 0x06,
62 0x91, 0x01, 0x00,
63 0x82, 0x01, 0x05
64};
65
66uint8_t RESPONSE2[] = {
67 0x90, 0x0a,
68 0x91, 0x01, 0x01,
69 0x92, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72
70};
71
72BOOST_AUTO_TEST_CASE(Encoding)
73{
74 LoggerResponse response1(5);
75 BOOST_CHECK_EQUAL_COLLECTIONS(response1.wireEncode().wire(),
76 response1.wireEncode().wire() + response1.wireEncode().size(),
77 RESPONSE1, RESPONSE1 + sizeof(RESPONSE1));
78
79 LoggerResponse response2(1, "error");
80 BOOST_CHECK_EQUAL_COLLECTIONS(response2.wireEncode().wire(),
81 response2.wireEncode().wire() + response2.wireEncode().size(),
82 RESPONSE2, RESPONSE2 + sizeof(RESPONSE2));
83}
84
85BOOST_AUTO_TEST_CASE(Decoding)
86{
87 LoggerResponse response1;
88 Block block1(RESPONSE1, sizeof(RESPONSE1));
89 BOOST_REQUIRE_NO_THROW(response1.wireDecode(block1));
90 BOOST_CHECK_EQUAL(response1.getCode(), 0);
91 BOOST_CHECK_EQUAL(response1.getDataSeqNo(), 5);
92
93 LoggerResponse response2;
94 Block block2(RESPONSE2, sizeof(RESPONSE2));
95 BOOST_REQUIRE_NO_THROW(response2.wireDecode(block2));
96 BOOST_CHECK_EQUAL(response2.getCode(), 1);
97 BOOST_CHECK_EQUAL(response2.getMsg(), "error");
98}
99
100
101BOOST_AUTO_TEST_SUITE_END()
102
103} // namespace tests
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -0700104} // namespace delorean
105} // namespace ndn