blob: 9a6f0c0f1ebb2032741b0f20817fab4ef1da58a3 [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
27namespace nsl {
28namespace tests {
29
30BOOST_AUTO_TEST_SUITE(TestLoggerResponse)
31
32void
33printByte(const uint8_t* buf, size_t size)
34{
35 std::stringstream ss;
36 using namespace CryptoPP;
37 StringSource is(buf, size, true, new HexEncoder(new FileSink(ss), false));
38
39 std::string output = ss.str();
40 for (size_t i = 0; i < output.size(); i++) {
41 std::cerr << "0x" << output.at(i);
42 std::cerr << output.at(++i) << ", ";
43 if ((i + 1) % 32 == 0)
44 std::cerr << std::endl;
45 }
46}
47
48BOOST_AUTO_TEST_CASE(Basic)
49{
50 LoggerResponse response1(5);
51 BOOST_CHECK_EQUAL(response1.getCode(), 0);
52 BOOST_CHECK_EQUAL(response1.getDataSeqNo(), 5);
53
54 LoggerResponse response2(1, "error");
55 BOOST_CHECK_EQUAL(response2.getCode(), 1);
56 BOOST_CHECK_EQUAL(response2.getMsg(), "error");
57}
58
59uint8_t RESPONSE1[] = {
60 0x90, 0x06,
61 0x91, 0x01, 0x00,
62 0x82, 0x01, 0x05
63};
64
65uint8_t RESPONSE2[] = {
66 0x90, 0x0a,
67 0x91, 0x01, 0x01,
68 0x92, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72
69};
70
71BOOST_AUTO_TEST_CASE(Encoding)
72{
73 LoggerResponse response1(5);
74 BOOST_CHECK_EQUAL_COLLECTIONS(response1.wireEncode().wire(),
75 response1.wireEncode().wire() + response1.wireEncode().size(),
76 RESPONSE1, RESPONSE1 + sizeof(RESPONSE1));
77
78 LoggerResponse response2(1, "error");
79 BOOST_CHECK_EQUAL_COLLECTIONS(response2.wireEncode().wire(),
80 response2.wireEncode().wire() + response2.wireEncode().size(),
81 RESPONSE2, RESPONSE2 + sizeof(RESPONSE2));
82}
83
84BOOST_AUTO_TEST_CASE(Decoding)
85{
86 LoggerResponse response1;
87 Block block1(RESPONSE1, sizeof(RESPONSE1));
88 BOOST_REQUIRE_NO_THROW(response1.wireDecode(block1));
89 BOOST_CHECK_EQUAL(response1.getCode(), 0);
90 BOOST_CHECK_EQUAL(response1.getDataSeqNo(), 5);
91
92 LoggerResponse response2;
93 Block block2(RESPONSE2, sizeof(RESPONSE2));
94 BOOST_REQUIRE_NO_THROW(response2.wireDecode(block2));
95 BOOST_CHECK_EQUAL(response2.getCode(), 1);
96 BOOST_CHECK_EQUAL(response2.getMsg(), "error");
97}
98
99
100BOOST_AUTO_TEST_SUITE_END()
101
102} // namespace tests
103} // namespace nsl