blob: 37ea13a1df53b4f6bdbd936c18519be6f2c08c74 [file] [log] [blame]
Yingdi Yube4aeed2015-03-23 13:28:58 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California
4 *
5 * This file is part of NSL (NDN Signature Logger).
6 * See AUTHORS.md for complete list of NSL authors and contributors.
7 *
8 * NSL is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NSL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NSL, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of nsl authors and contributors.
20 */
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