summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/tests/plcd/src/event_utests.c
blob: 56fbfad7f6a5995e859ff280bd7048326e915bd2 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* Cleopatre project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    check_event.c
 * \brief   Unitary tests for plcd
 * \ingroup Cleopatre - plcd
 *
 * This file content all the unitary tests for event, part of plcd
 * (the interface between the PLC driver and the Linux filesystem).
 */

#include <check.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>           /* for select() */
#include <linux/if_ether.h>     /* for ETH_ALEN */
#include <unistd.h>
#include <setjmp.h>
#include <assert.h>
#include <errno.h>
#include "plcd.h"
#include "stub.h"
#include "libmme.h"
#include "libspid.h"

#define DEBUG   1

#ifdef DEBUG
#define TRACE(...) printf("EVENT UTESTS: " __VA_ARGS__)
#else
#define TRACE(...)
#endif

#define HPAV_CONF_TMP_PATH "hpav_tmp.conf"
#define HPAV_INFO_TMP_PATH "hpav_tmp.info"

#define HPAV_CONF_FILE_BAD_STATUS "../testfiles/hpav_bad_status.conf"
#define HPAV_CONF_FILE_BAD_CCO    "../testfiles/hpav_bad_cco.conf"
#define HPAV_CONF_FILE_3 "../testfiles/hpav_3.conf"
#define HPAV_CONF_FILE_4 "../testfiles/hpav_4.conf"

#define HPAV_INFO_FILE_1 "../testfiles/hpav_1.info"

static struct check_ctx {
    int assert;
    jmp_buf jmp_env;

} check_ctx;


/** local defines */
#define MAC_ADDR    "AA:BB:CC:12:34:56"     /* mac addr that will be used for testing */
#define PLC_ADDR    "FF:EE:DD:98:76:54"     /* mac addr that will be used for testing */

/** local variables */
static plcd_ctx_t plcd_ctx;
static plcd_ctx_t *ctx = &plcd_ctx;
static int is_info_manager_needed, is_save_conf_needed;
static mme_ctx_t mme_ctx;
static char mme_buffer[1024];

unsigned char msg_buf[ETH_FRAME_LEN];   /* local buffer to store netlink messages */

/****************************************
 * internal accomodation functions
 * **************************************/
int check_assert (struct check_ctx *ctx, int test_id)
{
    ctx->assert = 0;
    if (setjmp (ctx->jmp_env) != 0)
    {
        return test_id;
    }
    else
    {
        return 0;
    }
}

int build_file (const char *filename, const char *content)
{
    FILE *fp;
    fp = fopen (filename, "w+");
    fprintf (fp, "%s", content);
    fclose (fp);
    return 0;
}

/*
    ######################################
    #   FUNCTION STUB
    #   (redefinition of libc functions)
    ######################################
*/
#undef assert
void assert (int /*scalar*/ expr)
{
    if (!expr)
    {
        check_ctx.assert = 1;
        longjmp (check_ctx.jmp_env, check_ctx.assert);
    }
}

ssize_t sendmsg (int socket, const struct msghdr *message, int flags)
{
    struct nlmsghdr *nlh;

    nlh = (struct nlmsghdr *)(message->msg_iov->iov_base);

    /* store message to local buffer */
    memcpy(msg_buf, nlh, message->msg_iov->iov_len);

    return 1;
}

int recvmsg(int s, struct msghdr *message, int flags)
{
    struct nlmsghdr *nlh;
    MME_t *mme_hdr;

    nlh = (struct nlmsghdr *)(message->msg_iov->iov_base);
    mme_hdr = (MME_t *)NLMSG_DATA(msg_buf);

    /* change mme type to CNF */
    mme_hdr->mmtype = (mme_hdr->mmtype & ~MME_TYPE_MASK) | MME_TYPE_CNF;

    /* copy local buffer to response message */
    memcpy(nlh, msg_buf, message->msg_iov->iov_len);

    return 1;
}

int select(int nfds, fd_set *readfds, fd_set *writefds,
           fd_set *exceptfds, struct timeval *timeout)
{
    return 1;
}

int hpav_send_single_value (const plcd_ctx_t *plcd_ctx, unsigned int mmtype, const void *value, unsigned int length)
{
    return 0;
}


/* fixtures - run before and after each unit test */
void setup(void)
{
    plcd_ctx.hpav_conf_path = HPAV_CONF_TMP_PATH;
    plcd_ctx.hpav_info_path = HPAV_INFO_TMP_PATH;
    mme_init (&mme_ctx, MME_TYPE_DRV_STA_STATUS | MME_TYPE_IND, mme_buffer, sizeof(mme_buffer));
    is_info_manager_needed = LIBSPID_FALSE;
    is_save_conf_needed = LIBSPID_FALSE;
#if 0
    int i;
    char pa[] = PLC_ADDR;
    char *s, *e;

    ctx->hardware_info_path = (char *)malloc(64);
    ctx->hpav_info_path = (char *)malloc(64);
    ctx->hpav_conf_path = (char *)malloc(64);

    /* initialize plcd context */
    strcpy(ctx->hardware_info_path, LIBSPID_HARDWARE_INFO_PATH);
    strcpy(ctx->hpav_info_path, LIBSPID_HPAV_INFO_PATH);
    strcpy(ctx->hpav_conf_path, LIBSPID_HPAV_CONF_PATH);
    ctx->plc_sock = 0;
    ctx->nvram = (spc300_nvram_t *) malloc(sizeof(spc300_nvram_t));
    s = pa;
    for (i = 0; i < 6; ++i)
    {
		ctx->nvram->plc_address[i] = s ? strtoul (s, &e, 16) : 0;
		if (s)
			s = (*e) ? e + 1 : e;
	}
#endif
}

void teardown(void)
{
}

/* --- TEST PROCEDURES --- */

/* check when plcd_ctx = NULL */
START_TEST (test_refresh_status_param_1)
{
    int result;

    if(0 == (result = check_assert (&check_ctx, 1)))
    {
        refresh_status (NULL, &mme_ctx, &is_save_conf_needed, &is_info_manager_needed);
        fail ("refresh_status plcd_ctx = NULL (1)");
    }
    fail_unless ((1 == result)
                 && (check_ctx.assert != 0),
                 "refresh status plcd_ctx = NULL (2)");
}
END_TEST

/* check when mme_ctx = NULL */
START_TEST (test_refresh_status_param_2)
{
    int result;

    if(0 == (result = check_assert (&check_ctx, 2)))
    {
        refresh_status (&plcd_ctx, NULL, &is_save_conf_needed, &is_info_manager_needed);
        fail ("refresh_status mme_ctx = NULL (1)");
    }
    fail_unless ((2 == result)
                 && (check_ctx.assert != 0),
                 "refresh status mme_ctx = NULL (2)");
}
END_TEST

/* check when mme_ctx.mmtype has wrong value */
START_TEST (test_refresh_status_param_3)
{
    int result;

    mme_ctx.mmtype = MME_TYPE_DRV_STA_STATUS | MME_TYPE_CNF;
    if(0 == (result = check_assert (&check_ctx, 3)))
    {
        refresh_status (&plcd_ctx, &mme_ctx, &is_save_conf_needed, &is_info_manager_needed);
        fail ("refresh_status mme_ctx.mmtype (1)");
    }
    fail_unless ((3 == result)
                 && (check_ctx.assert != 0),
                 "refresh status mme_ctx.mmtype (2)");
}
END_TEST

/* check when is_save_conf_needed = NULL */
START_TEST (test_refresh_status_param_4)
{
    int result;

    if(0 == (result = check_assert (&check_ctx, 4)))
    {
        refresh_status (&plcd_ctx, &mme_ctx, NULL, &is_info_manager_needed);
        fail ("refresh_status is_save_conf_needed (1)");
    }
    fail_unless ((4 == result)
                 && (check_ctx.assert != 0),
                 "refresh status is_save_conf_needed (2)");
}
END_TEST

/* check when is_info_manager_needed = NULL */
START_TEST (test_refresh_status_param_5)
{
    int result;

    if(0 == (result = check_assert (&check_ctx, 5)))
    {
        refresh_status (&plcd_ctx, &mme_ctx, &is_save_conf_needed, NULL);
        fail ("refresh_status is_info_manager_needed (1)");
    }
    fail_unless ((5 == result)
                 && (check_ctx.assert != 0),
                 "refresh status is_info_manager_needed (2)");
}
END_TEST

TCase *event_refresh_status_param_tcase (void)
{
    TCase *tc = tcase_create ("refresh_status param");
    // check params plcd_ctx = NULL
    tcase_add_test (tc, test_refresh_status_param_1);
    // check param mme_ctx = NULL
    tcase_add_test (tc, test_refresh_status_param_2);
    // check param mme_ctx.mmtype has wrong value
    tcase_add_test (tc, test_refresh_status_param_3);
    // check param is_save_conf_needed = NULL
    tcase_add_test (tc, test_refresh_status_param_4);
    // check param is_info_manager_needed = NULL
    tcase_add_test (tc, test_refresh_status_param_5);
    return tc;
}

/* check assoc status switch to unassociated */
START_TEST (test_refresh_status_assoc_1)
{
    int result, length;
    char buffer[1024];

    mme_push (&mme_ctx, "\x00\x00\x00\x00\x00", 5, &length);
    build_file (HPAV_INFO_TMP_PATH, "STATUS = associated\n");

    refresh_status (&plcd_ctx, &mme_ctx, &is_save_conf_needed, &is_info_manager_needed);
    libspid_config_read_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_STATUS, buffer, sizeof(buffer));

    fail_unless (!strcmp (buffer, "unassociated")
                 && (LIBSPID_FALSE == is_info_manager_needed),
                 "STATUS = unassociated");
}
END_TEST

/* check assoc status switch to associated */
START_TEST (test_refresh_status_assoc_2)
{
    int result, length;
    char buffer[1024];

    mme_push (&mme_ctx, "\x01\x00\x00\x00\x00", 5, &length);
    build_file (HPAV_INFO_TMP_PATH, "STATUS = unassociated\n");

    refresh_status (&plcd_ctx, &mme_ctx, &is_save_conf_needed, &is_info_manager_needed);
    libspid_config_read_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_STATUS, buffer, sizeof(buffer));

    fail_unless (!strcmp (buffer, "associated")
                 && (LIBSPID_FALSE == is_info_manager_needed),
                 "STATUS = associated");
}
END_TEST

/* check assoc status switch to associated */
START_TEST (test_refresh_status_assoc_3)
{
    int result, length;
    char buffer[1024];

    mme_push (&mme_ctx, "\x02\x00\x00\x00\x00", 5, &length);
    build_file (HPAV_INFO_TMP_PATH, "STATUS = unassociated\n");

    refresh_status (&plcd_ctx, &mme_ctx, &is_save_conf_needed, &is_info_manager_needed);
    libspid_config_read_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_STATUS, buffer, sizeof(buffer));

    fail_unless (!strcmp (buffer, "authenticated")
                 && (LIBSPID_FALSE == is_info_manager_needed),
                 "STATUS = authenticated");
}
END_TEST

/* check assoc status wrong value */
START_TEST (test_refresh_status_assoc_4)
{
    int result, length;
    char buffer[1024];

    mme_push (&mme_ctx, "\x03\x00\x00\x00\x00", 5, &length);
    build_file (HPAV_INFO_TMP_PATH, "STATUS = associated\n");

    refresh_status (&plcd_ctx, &mme_ctx, &is_save_conf_needed, &is_info_manager_needed);
    libspid_config_read_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_STATUS, buffer, sizeof(buffer));

    fail_unless (!strcmp (buffer, "associated")
                 && (LIBSPID_FALSE == is_info_manager_needed),
                 "wrong STATUS value");
}
END_TEST

TCase *event_refresh_status_assoc_tcase (void)
{
    TCase *tc = tcase_create ("refresh_status assoc");
    // check STATUS field
    tcase_add_test (tc, test_refresh_status_assoc_1);
    tcase_add_test (tc, test_refresh_status_assoc_2);
    tcase_add_test (tc, test_refresh_status_assoc_3);
    tcase_add_test (tc, test_refresh_status_assoc_4);

    return tc;
}

extern Suite* event_suite(void)
{
    Suite *s = suite_create("event");
    suite_add_tcase (s, event_refresh_status_param_tcase);
    suite_add_tcase (s, event_refresh_status_assoc_tcase);
    return s;
}

int main(void)
{
    Suite *s = event_suite;
    int number_failed = 0;
    SRunner *sr = srunner_create(s);
    srunner_add_suite (sr, event_suite);
    //srunner_set_fork_status (sr, CK_NOFORK);
    srunner_set_fork_status (sr, CK_FORK);
    srunner_run_all(sr, CK_NORMAL);
    number_failed = srunner_ntests_failed(sr);
    srunner_free(sr);

    return (number_failed == 0) ? 0 : -1;
}