Yingdi Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, Regents of the University of California |
Yingdi Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 5 | * 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 Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 8 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 9 | * 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 Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 13 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 14 | * 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 Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 17 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 18 | * 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 Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "logger-response.hpp" |
| 23 | #include "cryptopp.hpp" |
| 24 | |
| 25 | #include "boost-test.hpp" |
| 26 | |
| 27 | namespace nsl { |
| 28 | namespace tests { |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestLoggerResponse) |
| 31 | |
| 32 | void |
| 33 | printByte(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 | |
| 48 | BOOST_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 | |
| 59 | uint8_t RESPONSE1[] = { |
| 60 | 0x90, 0x06, |
| 61 | 0x91, 0x01, 0x00, |
| 62 | 0x82, 0x01, 0x05 |
| 63 | }; |
| 64 | |
| 65 | uint8_t RESPONSE2[] = { |
| 66 | 0x90, 0x0a, |
| 67 | 0x91, 0x01, 0x01, |
| 68 | 0x92, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72 |
| 69 | }; |
| 70 | |
| 71 | BOOST_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 | |
| 84 | BOOST_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 | |
| 100 | BOOST_AUTO_TEST_SUITE_END() |
| 101 | |
| 102 | } // namespace tests |
| 103 | } // namespace nsl |