blob: 848dfded0b5451f4564f14f9568fe3994037d26f [file] [log] [blame]
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08004 *
5 * BSD license, See the LICENSE file for more information
6 *
7 * Author: Qiuhan Ding <qiuhanding@cs.ucla.edu>
8 */
9
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080010#include "chat-message.hpp"
Varun Patila24bd3e2020-11-24 10:08:33 +053011
12#include <boost/test/unit_test.hpp>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080013#include <ndn-cxx/encoding/buffer-stream.hpp>
14
Varun Patila24bd3e2020-11-24 10:08:33 +053015namespace chronochat {
16namespace tests {
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080017
18using std::string;
19
20BOOST_AUTO_TEST_SUITE(TestChatMessage)
21
22BOOST_AUTO_TEST_CASE(EncodeDecode)
23{
24 string nick("qiuhan");
25 string chatroomName("test");
26 int32_t seconds =
27 static_cast<int32_t>(time::toUnixTimestamp(time::system_clock::now()).count() / 1000);
28 ChatMessage helloMsg;
29 helloMsg.setNick(nick);
30 helloMsg.setChatroomName(chatroomName);
31 helloMsg.setTimestamp(seconds);
32 helloMsg.setMsgType(ChatMessage::ChatMessageType::HELLO);
33
34 Block helloWire;
35 BOOST_REQUIRE_NO_THROW(helloWire = helloMsg.wireEncode());
36 ChatMessage decodedHelloMsg;
37 BOOST_REQUIRE_NO_THROW(decodedHelloMsg.wireDecode(helloWire));
38
39 BOOST_CHECK_EQUAL(decodedHelloMsg.getNick(), nick);
40 BOOST_CHECK_EQUAL(decodedHelloMsg.getChatroomName(), chatroomName);
41 BOOST_CHECK_EQUAL(decodedHelloMsg.getTimestamp(), seconds);
42 BOOST_CHECK_EQUAL(decodedHelloMsg.getMsgType(), ChatMessage::ChatMessageType::HELLO);
43
44 ChatMessage chatMsg;
45 string data = "This is for testing";
46 chatMsg.setNick(nick);
47 chatMsg.setChatroomName(chatroomName);
48 chatMsg.setTimestamp(seconds);
49 chatMsg.setData(data);
50 chatMsg.setMsgType(ChatMessage::ChatMessageType::CHAT);
51
52 Block chatWire;
53 BOOST_REQUIRE_NO_THROW(chatWire = chatMsg.wireEncode());
54 ChatMessage decodedChatMsg;
55 BOOST_REQUIRE_NO_THROW(decodedChatMsg.wireDecode(chatWire));
56
57 BOOST_CHECK_EQUAL(decodedChatMsg.getNick(), nick);
58 BOOST_CHECK_EQUAL(decodedChatMsg.getChatroomName(), chatroomName);
59 BOOST_CHECK_EQUAL(decodedChatMsg.getTimestamp(), seconds);
60 BOOST_CHECK_EQUAL(decodedChatMsg.getData(), data);
61 BOOST_CHECK_EQUAL(decodedChatMsg.getMsgType(), ChatMessage::ChatMessageType::CHAT);
62
63}
64
65BOOST_AUTO_TEST_SUITE_END()
66
67} // namespace tests
68} // namespace chronochat