summaryrefslogtreecommitdiff
path: root/cesar/maximus/sci/utest/scimsg/src/TestFunctionCall.cpp
blob: a5ede75be91f6c3c14f1931fe39c13b193489c20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Maximus project {{{
 *
 * Copyright (C) 2012 MStar Semiconductor
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    maximus/sci/utest/scimsg/src/TestFunctionCall.cpp
 * \ingroup maximus_sci_utest_scimsg
 *
 */
#include <cppunit/extensions/TestFactoryRegistry.h>

#include "inc/TestFunctionCall.h"
#include "maximus/sci/inc/FunctionSciMsg.h"
#include "maximus/sci/inc/SciServer.h"
#include <sys/types.h>
#include <string.h>
#include <ostream>

extern uint expected_msg_id;
CPPUNIT_TEST_SUITE_REGISTRATION (TestFunctionCall);

void
TestFunctionCall::setUp ()
{
    fake_server = new SciServer (fake_sta_list);
    fake_server->register_current_tick_addr (&time_base);
    time_base = 0xBEEFBEEFBEEFBEEFll;
}

void
TestFunctionCall::tearDown ()
{
    delete fake_server;
    fake_server = NULL;
}

inline void
TestFunctionCall::__build_functioncall_msg (
    unsigned char *expected,
    size_t msg_size,
    size_t data_size,
    Sci_Msg_Station_Id test_staid,
    Function_Call_Type test_type,
    Function_Call_Msg_Id test_msg_id)
{
    /* Build the expected buffer in network format. */

    /* 34 = sizeof (Sci_Msg_Header) + sizeof (Function_Call_Header). */
    CPPUNIT_ASSERT (msg_size >= 34);

    memset (expected, 0, msg_size);
    size_t test_length = data_size + sizeof (Function_Call_Header);

    /* SciMsg Header from here. */
    expected[0] = 'M';
    expected[1] = 'A';
    expected[2] = 'X';
    expected[3] = 'I';
    expected[4] = SCI_MSG_VERSION;
    expected[5] = SCI_MSG_TYPE_FUNCTION_CALL;
    expected[6] = test_length >> 24;
    expected[7] = test_length >> 16;
    expected[8] = test_length >> 8;
    expected[9] = test_length;
    expected[10] = static_cast <uint16_t> (test_staid) >> 8;
    expected[11] = static_cast <uint16_t> (test_staid);
    expected[12] = expected_msg_id >> 8;
    expected[13] = expected_msg_id;
    expected[14] = time_base >> 56;
    expected[15] = time_base >> 48;
    expected[16] = time_base >> 40;
    expected[17] = time_base >> 32;
    expected[18] = time_base >> 24;
    expected[19] = time_base >> 16;
    expected[20] = time_base >> 8;
    expected[21] = time_base;
    /* functioncall Header from here. */
    expected[26] = FUNCTION_CALL_VERSION;
    expected[27] = static_cast <uint8_t> (test_type);
    expected[28] = static_cast <uint16_t> (test_msg_id) >> 8;
    expected[29] = static_cast <uint16_t> (test_msg_id);
}

void
TestFunctionCall::test_encode ()
{
    std::cout << "TestFunctionCall_encode" << std::endl;
    const unsigned char *serial_msg;
    const size_t msg_data_size = 0;

    Sci_Msg_Station_Id staid = 30;
    Function_Call_Type type = FUNCTION_CALL_TYPE_REQ;
    Function_Call_Msg_Id msg_id = 0x17;

    const size_t msg_size = msg_data_size
        + sizeof (Sci_Msg_Header)
        + sizeof (Function_Call_Header);
    unsigned char *expected = new unsigned char [msg_size];
    ++expected_msg_id;
    __build_functioncall_msg (
        expected,
        msg_size,
        msg_data_size,
        staid,
        type,
        msg_id);

    FunctionSciMsg msg (msg_data_size);
    msg.set_sci_stationid (staid);
    msg.set_fc_msg_id (msg_id);

    msg.setup_and_send (*fake_server);
    serial_msg = msg.get_sci_alloc_addr ();
    CPPUNIT_ASSERT (serial_msg);
    CPPUNIT_ASSERT (msg.get_sci_alloc_size ()
                    != (msg_data_size + sizeof (Function_Call_Header)));

    for (uint i = 0; i < msg_size; i++)
    {
        if (expected[i] != serial_msg[i])
        {
            std::ostringstream oss;
            oss << "expected[" << i << "] = "
                << (uint) expected[i]
                << " != "
                << (uint) serial_msg[i]
                << " = serial_msg[" << i << "]";
            CPPUNIT_FAIL (oss.str ().c_str ());
        }
    }
    delete [] expected;
}

void
TestFunctionCall::test_decode ()
{
    std::cout << "TestFunctionCall_decode" << std::endl;
    const size_t msg_data_size = 15;

    Sci_Msg_Station_Id staid = 42;
    Function_Call_Type type = FUNCTION_CALL_TYPE_RSP;
    Function_Call_Msg_Id msg_id = 0x37;

    const size_t msg_size = msg_data_size
        + sizeof (Sci_Msg_Header)
        + sizeof (Function_Call_Header);
    unsigned char *expected = new unsigned char [msg_size];
    __build_functioncall_msg (
        expected,
        msg_size,
        msg_data_size,
        staid,
        type,
        msg_id);

    FunctionSciMsg msg (*((struct Sci_Msg_Header *)expected), expected);

    CPPUNIT_ASSERT (msg.get_sci_alloc_size () == msg_size);
    CPPUNIT_ASSERT (msg.get_sci_alloc_addr () == expected);
    CPPUNIT_ASSERT (msg.get_sci_data_addr () == (expected + sizeof (Sci_Msg_Header)));
    CPPUNIT_ASSERT (msg.get_sci_datalength () == (msg_size - sizeof (Sci_Msg_Header)));
    CPPUNIT_ASSERT (msg.get_sci_stationid () == staid);

    CPPUNIT_ASSERT (msg.get_fc_type () == type);
    CPPUNIT_ASSERT (msg.get_fc_msgid () == msg_id);
}