Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 1 | #include "chatroom-info.hpp" |
| 2 | #include <boost/test/unit_test.hpp> |
| 3 | #include <ndn-cxx/encoding/block.hpp> |
| 4 | |
| 5 | namespace chronos { |
| 6 | |
| 7 | namespace test { |
| 8 | |
| 9 | using std::string; |
| 10 | |
| 11 | BOOST_AUTO_TEST_SUITE(TestChatroomInfo) |
| 12 | |
| 13 | const uint8_t chatroomInfo[] = { |
| 14 | 0x81, 0x2d, // ChatroomInfo |
| 15 | 0x82, 0x01, // TrustModel |
| 16 | 0x01, |
| 17 | 0x80, 0x14,// Participant1 |
| 18 | 0x07, 0x12, |
| 19 | 0x08, 0x03, |
| 20 | 0x6e, 0x64, 0x6e, |
| 21 | 0x08, 0x04, |
| 22 | 0x75, 0x63, 0x6c, 0x61, |
| 23 | 0x08, 0x05, |
| 24 | 0x61, 0x6c, 0x69, 0x63, 0x65, |
| 25 | 0x80, 0x12, // Participant2 |
| 26 | 0x07, 0x10, |
| 27 | 0x08, 0x03, |
| 28 | 0x6e, 0x64, 0x6e, |
| 29 | 0x08, 0x04, |
| 30 | 0x75, 0x63, 0x6c, 0x61, |
| 31 | 0x08, 0x03, |
| 32 | 0x79, 0x6d, 0x6a |
| 33 | }; |
| 34 | |
| 35 | BOOST_AUTO_TEST_CASE(EncodeChatroom) |
| 36 | { |
| 37 | //Chatroom := CHATROOM-TYPE TLV-LENGTH |
| 38 | // TrustModel |
| 39 | // Participant+ |
| 40 | |
| 41 | ChatroomInfo chatroom; |
| 42 | chatroom.setName(ndn::Name::Component("lunch-talk")); |
| 43 | chatroom.addParticipant(Name("/ndn/ucla/alice")); |
| 44 | chatroom.addParticipant(Name("/ndn/ucla/ymj")); |
| 45 | chatroom.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST); |
| 46 | |
| 47 | const Block& encoded = chatroom.wireEncode(); |
| 48 | Block chatroomInfoBlock(chatroomInfo, sizeof(chatroomInfo)); |
| 49 | |
| 50 | BOOST_CHECK_EQUAL_COLLECTIONS(chatroomInfoBlock.wire(), |
| 51 | chatroomInfoBlock.wire() + chatroomInfoBlock.size(), |
| 52 | encoded.wire(), |
| 53 | encoded.wire() + encoded.size()); |
| 54 | } |
| 55 | |
| 56 | BOOST_AUTO_TEST_CASE(DecodeChatroomCorrect) |
| 57 | { |
| 58 | ChatroomInfo chatroom; |
| 59 | chatroom.setName(ndn::Name::Component("lunch-talk")); |
| 60 | chatroom.addParticipant(Name("/ndn/ucla/alice")); |
| 61 | chatroom.addParticipant(Name("/ndn/ucla/ymj")); |
| 62 | chatroom.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST); |
| 63 | |
| 64 | Block chatroomInfoBlock(chatroomInfo, sizeof(chatroomInfo)); |
| 65 | ChatroomInfo dechatroom; |
| 66 | dechatroom.wireDecode(chatroomInfoBlock); |
| 67 | dechatroom.setName(ndn::Name::Component("lunch-talk")); |
| 68 | |
| 69 | BOOST_CHECK_EQUAL(chatroom.getName(), dechatroom.getName()); |
| 70 | BOOST_CHECK_EQUAL(chatroom.getParticipants().size(), dechatroom.getParticipants().size()); |
| 71 | BOOST_CHECK_EQUAL(chatroom.getParticipants()[0].toUri(), dechatroom.getParticipants()[0].toUri()); |
| 72 | BOOST_CHECK_EQUAL(chatroom.getParticipants()[1].toUri(), dechatroom.getParticipants()[1].toUri()); |
| 73 | BOOST_CHECK_EQUAL(chatroom.getTrustModel(), dechatroom.getTrustModel()); |
| 74 | } |
| 75 | |
| 76 | BOOST_AUTO_TEST_CASE(DecodeChatroomError) |
| 77 | { |
| 78 | const uint8_t error1[] = { |
| 79 | 0x80, 0x2d, // Wrong ChatroomInfo Type (0x81, 0x2d) |
| 80 | 0x82, 0x01, // TrustModel |
| 81 | 0x01, |
| 82 | 0x80, 0x14,// Participant1 |
| 83 | 0x07, 0x12, |
| 84 | 0x08, 0x03, |
| 85 | 0x6e, 0x64, 0x6e, |
| 86 | 0x08, 0x04, |
| 87 | 0x75, 0x63, 0x6c, 0x61, |
| 88 | 0x08, 0x05, |
| 89 | 0x61, 0x6c, 0x69, 0x63, 0x65, |
| 90 | 0x80, 0x12, // Participant2 |
| 91 | 0x07, 0x10, |
| 92 | 0x08, 0x03, |
| 93 | 0x6e, 0x64, 0x6e, |
| 94 | 0x08, 0x04, |
| 95 | 0x75, 0x63, 0x6c, 0x61, |
| 96 | 0x08, 0x03, |
| 97 | 0x79, 0x6d, 0x6a |
| 98 | }; |
| 99 | |
| 100 | Block errorBlock1(error1, sizeof(error1)); |
| 101 | BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock1), ChatroomInfo::Error); |
| 102 | |
| 103 | const uint8_t error2[] = { |
| 104 | 0x81, 0x2d, // ChatroomInfo |
| 105 | 0x81, 0x01, // Wrong TrustModel Type (0x82, 0x01) |
| 106 | 0x01, |
| 107 | 0x80, 0x14,// Participant1 |
| 108 | 0x07, 0x12, |
| 109 | 0x08, 0x03, |
| 110 | 0x6e, 0x64, 0x6e, |
| 111 | 0x08, 0x04, |
| 112 | 0x75, 0x63, 0x6c, 0x61, |
| 113 | 0x08, 0x05, |
| 114 | 0x61, 0x6c, 0x69, 0x63, 0x65, |
| 115 | 0x80, 0x12, // Participant2 |
| 116 | 0x07, 0x10, |
| 117 | 0x08, 0x03, |
| 118 | 0x6e, 0x64, 0x6e, |
| 119 | 0x08, 0x04, |
| 120 | 0x75, 0x63, 0x6c, 0x61, |
| 121 | 0x08, 0x03, |
| 122 | 0x79, 0x6d, 0x6a |
| 123 | }; |
| 124 | |
| 125 | Block errorBlock2(error2, sizeof(error2)); |
| 126 | BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock2), ChatroomInfo::Error); |
| 127 | |
| 128 | const uint8_t error3[] = { |
| 129 | 0x81, 0x2d, // ChatroomInfo |
| 130 | 0x82, 0x01, // TrustModel |
| 131 | 0x01, |
| 132 | 0x80, 0x14,// Participant1 |
| 133 | 0x07, 0x12, |
| 134 | 0x08, 0x03, |
| 135 | 0x6e, 0x64, 0x6e, |
| 136 | 0x08, 0x04, |
| 137 | 0x75, 0x63, 0x6c, 0x61, |
| 138 | 0x08, 0x05, |
| 139 | 0x61, 0x6c, 0x69, 0x63, 0x65, |
| 140 | 0x81, 0x12, // Wrong Participant Type (0x80, 0x12) |
| 141 | 0x07, 0x10, |
| 142 | 0x08, 0x03, |
| 143 | 0x6e, 0x64, 0x6e, |
| 144 | 0x08, 0x04, |
| 145 | 0x75, 0x63, 0x6c, 0x61, |
| 146 | 0x08, 0x03, |
| 147 | 0x79, 0x6d, 0x6a |
| 148 | }; |
| 149 | |
| 150 | Block errorBlock3(error3, sizeof(error3)); |
| 151 | BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock3), ChatroomInfo::Error); |
| 152 | |
| 153 | const uint8_t error4[] = { |
| 154 | 0x81, 0x00 // Empty ChatroomInfo |
| 155 | }; |
| 156 | |
| 157 | Block errorBlock4(error4, sizeof(error4)); |
| 158 | BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock4), ChatroomInfo::Error); |
| 159 | |
| 160 | const uint8_t error5[] = { |
| 161 | 0x81, 0x03, // ChatroomInfo |
| 162 | 0x82, 0x01, // TrustModel |
| 163 | 0x01 |
| 164 | //zero Participant |
| 165 | }; |
| 166 | |
| 167 | Block errorBlock5(error5, sizeof(error5)); |
| 168 | BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock5), ChatroomInfo::Error); |
| 169 | } |
| 170 | |
| 171 | BOOST_AUTO_TEST_SUITE_END() |
| 172 | |
| 173 | } //namespace test |
| 174 | |
| 175 | } //namespace chronos |