blob: 09ab127bf22f3098ab8b0eec39872d0e02569221 [file] [log] [blame]
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 *
5 * BSD license, See the LICENSE file for more information
6 *
7 * Author: Qiuhan Ding <qiuhanding@cs.ucla.edu>
8 */
9
10#include <boost/test/unit_test.hpp>
11
12#include "chat-message.hpp"
13#include <ndn-cxx/encoding/buffer-stream.hpp>
14
15namespace chronochat{
16namespace tests{
17
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