blob: af223d4ab39e932fc28d8490bdfb0977feb72b9f [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2020, Regents of the University of California
*
* BSD license, See the LICENSE file for more information
*/
#include "chatroom-info.hpp"
#include <boost/test/unit_test.hpp>
#include <ndn-cxx/encoding/block.hpp>
namespace chronochat {
namespace tests {
using std::string;
BOOST_AUTO_TEST_SUITE(TestChatroomInfo)
const uint8_t chatroomInfo[] = {
0x80, 0x5d, // ChatroomInfo
0x81, 0x06, // ChatroomName
0x08, 0x04,
0x06e, 0x64, 0x6e, 0x64,
0x82, 0x01, // TrustModel
0x01,
0x83, 0x12, // ChatroomPrefix
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x09,
0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
0x84, 0x14, // ManagerPrefix
0x07, 0x12,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x05,
0x61, 0x6c, 0x69, 0x63, 0x65,
0x85, 0x26,// Participants
0x07, 0x12,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x05,
0x61, 0x6c, 0x69, 0x63, 0x65,
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x03,
0x79, 0x6d, 0x6a
};
BOOST_AUTO_TEST_CASE(EncodeChatroom)
{
// ChatroomInfo := CHATROOM-INFO-TYPE TLV-LENGTH
// ChatroomName
// TrustModel
// ChatroomPrefix
// ManagerPrefix
// Participants
//
// ChatroomName := CHATROOM-NAME-TYPE TLV-LENGTH
// NameComponent
//
// TrustModel := TRUST-MODEL-TYPE TLV-LENGTH
// nonNegativeInteger
//
// ChatroomPrefix := CHATROOM-PREFIX-TYPE TLV-LENGTH
// Name
//
// ManagerPrefix := MANAGER-PREFIX-TYPE TLV-LENGTH
// Name
//
// Participants := PARTICIPANTS-TYPE TLV-LENGTH
// Name+
ChatroomInfo chatroom;
chatroom.setName(ndn::Name::Component("ndnd"));
chatroom.setManager("/ndn/ucla/alice");
chatroom.setSyncPrefix("/ndn/broadcast");
chatroom.addParticipant(Name("/ndn/ucla/alice"));
chatroom.addParticipant(Name("/ndn/ucla/ymj"));
chatroom.addParticipant(Name("/ndn/ucla"));
chatroom.removeParticipant(Name("/ndn/ucla"));
chatroom.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST);
const Block& encoded = chatroom.wireEncode();
Block chatroomInfoBlock(chatroomInfo, sizeof(chatroomInfo));
BOOST_CHECK_EQUAL_COLLECTIONS(chatroomInfoBlock.wire(),
chatroomInfoBlock.wire() + chatroomInfoBlock.size(),
encoded.wire(),
encoded.wire() + encoded.size());
}
BOOST_AUTO_TEST_CASE(DecodeChatroomCorrect)
{
ChatroomInfo chatroom;
chatroom.setName(ndn::Name::Component("ndnd"));
chatroom.setManager("/ndn/ucla/alice");
chatroom.setSyncPrefix("/ndn/broadcast");
chatroom.addParticipant(Name("/ndn/ucla/alice"));
chatroom.addParticipant(Name("/ndn/ucla/ymj"));
chatroom.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST);
Block chatroomInfoBlock(chatroomInfo, sizeof(chatroomInfo));
ChatroomInfo dechatroom;
dechatroom.wireDecode(chatroomInfoBlock);
BOOST_CHECK_EQUAL(chatroom.getName(), dechatroom.getName());
BOOST_CHECK_EQUAL(chatroom.getSyncPrefix().toUri(), dechatroom.getSyncPrefix().toUri());
BOOST_CHECK_EQUAL(chatroom.getManagerPrefix().toUri(), dechatroom.getManagerPrefix().toUri());
BOOST_CHECK_EQUAL(chatroom.getParticipants().size(), dechatroom.getParticipants().size());
BOOST_CHECK_EQUAL(chatroom.getParticipants().begin()->toUri(),
dechatroom.getParticipants().begin()->toUri());
BOOST_CHECK_EQUAL(chatroom.getParticipants().begin()->toUri(),
dechatroom.getParticipants().begin()->toUri());
}
BOOST_AUTO_TEST_CASE(DecodeChatroomError)
{
const uint8_t error1[] = {
0x81, 0x5d, // ChatroomInfo Type Error
0x81, 0x06, // ChatroomName
0x08, 0x04,
0x06e, 0x64, 0x6e, 0x64,
0x82, 0x01, // TrustModel
0x01,
0x83, 0x12, // ChatroomPrefix
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x09,
0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
0x84, 0x14, // ManagerPrefix
0x07, 0x12,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x05,
0x61, 0x6c, 0x69, 0x63, 0x65,
0x85, 0x26,// Participants
0x07, 0x12,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x05,
0x61, 0x6c, 0x69, 0x63, 0x65,
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x03,
0x79, 0x6d, 0x6a
};
Block errorBlock1(error1, sizeof(error1));
BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock1), ChatroomInfo::Error);
const uint8_t error2[] = {
0x80, 0x5d, // ChatroomInfo
0x81, 0x06, // ChatroomName
0x08, 0x04,
0x06e, 0x64, 0x6e, 0x64,
0x83, 0x01, // TrustModel Type Error
0x01,
0x83, 0x12, // ChatroomPrefix
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x09,
0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
0x84, 0x14, // ManagerPrefix
0x07, 0x12,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x05,
0x61, 0x6c, 0x69, 0x63, 0x65,
0x85, 0x26,// Participants
0x07, 0x12,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x05,
0x61, 0x6c, 0x69, 0x63, 0x65,
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x03,
0x79, 0x6d, 0x6a
};
Block errorBlock2(error2, sizeof(error2));
BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock2), ChatroomInfo::Error);
const uint8_t error3[] = {
0x80, 0x5d, // ChatroomInfo
0x81, 0x06, // ChatroomName
0x08, 0x04,
0x06e, 0x64, 0x6e, 0x64,
0x82, 0x01, // TrustModel
0x01,
0x80, 0x12, // ChatroomPrefix Type Error
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x09,
0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
0x84, 0x14, // ManagerPrefix
0x07, 0x12,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x05,
0x61, 0x6c, 0x69, 0x63, 0x65,
0x85, 0x26,// Participants
0x07, 0x12,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x05,
0x61, 0x6c, 0x69, 0x63, 0x65,
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x03,
0x79, 0x6d, 0x6a
};
Block errorBlock3(error3, sizeof(error3));
BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock3), ChatroomInfo::Error);
const uint8_t error4[] = {
0x80, 0x5d, // ChatroomInfo
0x81, 0x06, // ChatroomName
0x08, 0x04,
0x06e, 0x64, 0x6e, 0x64,
0x82, 0x01, // TrustModel
0x01,
0x83, 0x12, // ChatroomPrefix
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x09,
0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
0x80, 0x14, // ManagerPrefix Error Type
0x07, 0x12,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x05,
0x61, 0x6c, 0x69, 0x63, 0x65,
0x85, 0x26,// Participants
0x07, 0x12,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x05,
0x61, 0x6c, 0x69, 0x63, 0x65,
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x03,
0x79, 0x6d, 0x6a
};
Block errorBlock4(error4, sizeof(error4));
BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock4), ChatroomInfo::Error);
const uint8_t error5[] = {
0x80, 0x5d, // ChatroomInfo
0x81, 0x06, // ChatroomName
0x08, 0x04,
0x06e, 0x64, 0x6e, 0x64,
0x82, 0x01, // TrustModel
0x01,
0x83, 0x12, // ChatroomPrefix
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x09,
0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
0x84, 0x14, // ManagerPrefix
0x07, 0x12,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x05,
0x61, 0x6c, 0x69, 0x63, 0x65,
0x80, 0x26,// Participants Error Type
0x07, 0x12,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x05,
0x61, 0x6c, 0x69, 0x63, 0x65,
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x04,
0x75, 0x63, 0x6c, 0x61,
0x08, 0x03,
0x79, 0x6d, 0x6a
};
Block errorBlock5(error5, sizeof(error5));
BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock5), ChatroomInfo::Error);
const uint8_t error6[] = {
0x80, 0x00 // Empty ChatroomInfo
};
Block errorBlock6(error6, sizeof(error6));
BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock6), ChatroomInfo::Error);
const uint8_t error7[] = {
0x80, 0x1f, // ChatroomInfo
0x81, 0x06, // ChatroomName
0x08, 0x04,
0x06e, 0x64, 0x6e, 0x64,
0x82, 0x01, // TrustModel
0x01,
0x83, 0x12, // ChatroomPrefix
0x07, 0x10,
0x08, 0x03,
0x6e, 0x64, 0x6e,
0x08, 0x09,
0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
// no Participant
};
Block errorBlock7(error7, sizeof(error7));
BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock7), ChatroomInfo::Error);
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace tests
} // namespace chronochat