summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/tests/plcd/src/event_utests.c
blob: 48b0dccf58899d8bbe06ce02b881967ccdb08c07 (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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/* 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_id;
    int test_id;
    jmp_buf jmp_env;

} check_ctx;

#define CHECK_ASSERT(result, id, jmpbuf) if(0 == (result = setjmp (jmpbuf)))

/** 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 */
#define MAX_ITEM_NB 8
#define CONF_TABLE_INDEX 0
#define INFO_TABLE_INDEX 1

/** 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[256];
unsigned char label_table[2][MAX_ITEM_NB][64];
unsigned char value_table[2][MAX_ITEM_NB][64];
unsigned char msg_buf[ETH_FRAME_LEN];   /* local buffer to store netlink messages */

extern int check_sc_start (plcd_ctx_t *ctx);
extern int refresh_status (plcd_ctx_t *ctx, mme_ctx_t *status_ctx, int *is_save_conf_needed, int *is_info_manager_needed);


/*
    ######################################
    #   FUNCTION STUB
    #   (redefinition of libc functions)
    ######################################
*/
void __assert_fail (__const char *__assertion, __const char *__file,
                           unsigned int __line, __const char *__function)
{
    check_ctx.assert_id = check_ctx.test_id;
    longjmp (check_ctx.jmp_env, check_ctx.assert_id);
}

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;
}

void syslog(int priority, const char *format, ...)
{
    return;
}

int hpav_send_single_value (const plcd_ctx_t *plcd_ctx, unsigned int mmtype, const void *value, unsigned int length)
{
    int len;
    mme_init (&mme_ctx, mmtype, mme_buffer,  sizeof(mme_buffer));
    mme_put (&mme_ctx, value, length, &len);
    return 0;
}

mme_error_t mme_init (mme_ctx_t *ctx, const mme_type_t mmtype, unsigned char *buffer, const unsigned int length)
{
    ctx->buffer     =   buffer;
    ctx->mmtype     =   mmtype;
    ctx->length     =   length;
    ctx->head       =   0;
    ctx->tail       =   0;
    return MME_SUCCESS;
}

mme_error_t mme_push (mme_ctx_t *ctx, const void *data, unsigned int length, unsigned int *result_length)
{
    return MME_SUCCESS;
}

mme_error_t mme_pull (mme_ctx_t *ctx, void *data, unsigned int length, unsigned int *result_length)
{
    *result_length = length;
    memcpy(data, (unsigned char *)(ctx->buffer + ctx->head), length);
    ctx->head += length;
    //printf ("head=%d, data=%02x\n", ctx->head, *((unsigned char *)data));
    return MME_SUCCESS;
}

mme_error_t mme_put (mme_ctx_t *ctx, const void *data, unsigned int length, unsigned int *result_length)
{
    *result_length = length;
    memcpy( (unsigned char *)(ctx->buffer + ctx->tail), (unsigned char *)data, length );
    ctx->tail += length;
    return MME_SUCCESS;
}

void dump_table (unsigned char table[MAX_ITEM_NB][64])
{
    int i;
    for (i = 0; i < MAX_ITEM_NB; i++)
        printf ("%s-", table[i]);
    printf ("\n");
}

libspid_error_t libspid_config_read_item (const char *filename, const char *label, char *value, int buffer_len)
{
    int i, conf;
    conf = (!strcmp (filename, "conf") ? CONF_TABLE_INDEX : INFO_TABLE_INDEX);
    for(i = 0; i < MAX_ITEM_NB; i++)
    {
        if(!strcmp (label, label_table[conf][i]))
        {
            strcpy (value, value_table[conf][i]);
            return LIBSPID_SUCCESS;
        }
    }
    return LIBSPID_ERROR_NOT_FOUND;
}

libspid_error_t libspid_config_write_item(const char *filename, const char *label, const char *value)
{
    int i, conf;
    conf = (!strcmp (filename, "conf") ? CONF_TABLE_INDEX : INFO_TABLE_INDEX);
    for (i = 0; i < MAX_ITEM_NB; i++)
    {
        if(label_table[conf][i][0] == '\0')
            break;
        if(!strcmp (label_table[conf][i], label))
        {
            strcpy (value_table[conf][i], value);
            return LIBSPID_SUCCESS;
        }
    }
    if(i >= MAX_ITEM_NB)
        return LIBSPID_ERROR_NO_SPACE;
    strcpy (label_table[conf][i], label);
    strcpy (value_table[conf][i], value);
    return LIBSPID_SUCCESS;
}

extern libspid_error_t libspid_system_save_file(const char *filename)
{
    return LIBSPID_SUCCESS;
}

/* 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;
    check_ctx.test_id = 0;
    check_ctx.assert_id = 0;
    memset (label_table, '\0', sizeof(label_table));
    memset (value_table, '\0', sizeof(value_table));
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_STATUS, "unassociated");
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_CCO, "station");
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_BACKUP_CCO, "no");
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_SC, "no");
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_SC_BUTTON, "no");

}

void teardown(void)
{
}

/***********************************************************
 *
 * check_sc_start() test cases
 *
 *******************************************************/

/* check when plcd_ctx = NULL */
START_TEST (test_check_sc_start_param_1)
{
    check_ctx.test_id = 1;
    if(0 == setjmp (check_ctx.jmp_env))
    {
        check_sc_start (NULL);
        fail ("check_sc_start plcd_ctx = NULL (1)");
    }
    fail_unless ((check_ctx.test_id == check_ctx.assert_id),
                 "check_sc_start plcd_ctx = NULL (2)");
}
END_TEST

/* check if no MME is sent if SC_BUTTON!=yes or SC!=no */
START_TEST (test_check_sc_start_cond_1)
{
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_SC_BUTTON, "yes");
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_SC, "yes");
    check_sc_start (&plcd_ctx);
    fail_unless ((mme_ctx.head == mme_ctx.tail),
                 "check_sc_start SC_BUTTON=yes, SC=yes");

}
END_TEST

/* check if no MME is sent if SC_BUTTON!=no or SC!=no */
START_TEST (test_check_sc_start_cond_2)
{
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_SC_BUTTON, "no");
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_SC, "no");
    check_sc_start (&plcd_ctx);
    fail_unless ((mme_ctx.head == mme_ctx.tail),
                 "check_sc_start SC_BUTTON=no, SC=no");

}
END_TEST

/* check if MME is sent with SC_JOIN */
START_TEST (test_check_sc_start_assoc_1)
{
    unsigned char sc;
    unsigned int length;
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_SC_BUTTON, "yes");
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_SC, "no");
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_STATUS, "unassociated");

    check_sc_start (&plcd_ctx);
    mme_pull (&mme_ctx, &sc, 1, &length);
    fail_unless ((MME_DRV_SC_JOIN == sc)
                 && (1 == length),
                 "check_sc_start SC_JOIN");
}
END_TEST

/* check if MME is sent with SC_ADD */
START_TEST (test_check_sc_start_assoc_2)
{
    unsigned char sc;
    unsigned int length;
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_SC_BUTTON, "yes");
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_SC, "no");
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_STATUS, "authenticated");

    check_sc_start (&plcd_ctx);
    mme_pull (&mme_ctx, &sc, 1, &length);
    fail_unless ((MME_DRV_SC_ADD == sc)
                 && (1 == length),
                 "check_sc_start SC_ADD");
}
END_TEST

TCase *event_refresh_check_sc_start_tcase (void)
{
    TCase *tc = tcase_create ("check_sc_start");
    tcase_add_checked_fixture (tc, setup, teardown);
    // check STATUS field
    tcase_add_test (tc, test_check_sc_start_param_1);
    tcase_add_test (tc, test_check_sc_start_cond_1);
    tcase_add_test (tc, test_check_sc_start_cond_2);
    tcase_add_test (tc, test_check_sc_start_assoc_1);
    tcase_add_test (tc, test_check_sc_start_assoc_2);
    return (tc);
}

/***********************************************************
 *
 * refresh_status() test cases
 *
 *******************************************************/

/* check when plcd_ctx = NULL */
START_TEST (test_refresh_status_param_1)
{
    check_ctx.test_id = 1;
    if(0 == setjmp (check_ctx.jmp_env))
    {
        refresh_status (NULL, &mme_ctx, &is_save_conf_needed, &is_info_manager_needed);
        fail ("refresh_status plcd_ctx = NULL (1)");
    }
    fail_unless ((check_ctx.test_id == check_ctx.assert_id),
                 "refresh status plcd_ctx = NULL (2)");
}
END_TEST

/* check when mme_ctx = NULL */
START_TEST (test_refresh_status_param_2)
{
    check_ctx.test_id = 2;
    if(0 == setjmp (check_ctx.jmp_env))
    {
        refresh_status (&plcd_ctx, NULL, &is_save_conf_needed, &is_info_manager_needed);
        fail ("refresh_status mme_ctx = NULL (1)");
    }
    fail_unless ((check_ctx.test_id == check_ctx.assert_id),
                 "refresh status mme_ctx = NULL (2)");
}
END_TEST

/* check when mme_ctx.mmtype has wrong value */
START_TEST (test_refresh_status_param_3)
{
    mme_ctx.mmtype = MME_TYPE_DRV_STA_STATUS | MME_TYPE_CNF;
    check_ctx.test_id = 3;
    if(0 == setjmp (check_ctx.jmp_env))
    {
        refresh_status (&plcd_ctx, &mme_ctx, &is_save_conf_needed, &is_info_manager_needed);
        fail ("refresh_status mme_ctx.mmtype (1)");
    }
    fail_unless ((check_ctx.test_id == check_ctx.assert_id),
                 "refresh status mme_ctx.mmtype (2)");
}
END_TEST

/* check when is_save_conf_needed = NULL */
START_TEST (test_refresh_status_param_4)
{
    check_ctx.test_id = 4;
    if(0 == setjmp (check_ctx.jmp_env))
    {
        refresh_status (&plcd_ctx, &mme_ctx, NULL, &is_info_manager_needed);
        fail ("refresh_status is_save_conf_needed (1)");
    }
    fail_unless ((check_ctx.test_id == check_ctx.assert_id),
                 "refresh status is_save_conf_needed (2)");
}
END_TEST

/* check when is_info_manager_needed = NULL */
START_TEST (test_refresh_status_param_5)
{
    check_ctx.test_id = 5;
    if(0 == setjmp (check_ctx.jmp_env))
    {
        refresh_status (&plcd_ctx, &mme_ctx, &is_save_conf_needed, NULL);
        fail ("refresh_status is_info_manager_needed (1)");
    }
    fail_unless ((check_ctx.test_id == check_ctx.assert_id),
                 "refresh status is_info_manager_needed (2)");
}
END_TEST

TCase *event_refresh_status_param_tcase (void)
{
    TCase *tc = tcase_create ("refresh_status_param");
    tcase_add_checked_fixture (tc, setup, teardown);
    // 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)
{
    unsigned int length;
    char buffer[1024];

    mme_put (&mme_ctx, "\x00\x00\x00\x00\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_STATUS, "unassociated");

    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)
{
    unsigned int length;
    char buffer[1024];

    mme_put (&mme_ctx, "\x01\x00\x00\x00\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_STATUS, "unassociated");

//    dump_table (label_table[INFO_TABLE_INDEX]);
//    dump_table (value_table[INFO_TABLE_INDEX]);

    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));

//    dump_table (label_table[INFO_TABLE_INDEX]);
//    dump_table (value_table[INFO_TABLE_INDEX]);

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

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

    mme_put (&mme_ctx, "\x02\x00\x00\x00\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_STATUS, "unassociated");

    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_TRUE == is_info_manager_needed),
                 "STATUS = authenticated");
}
END_TEST

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

    mme_put (&mme_ctx, "\x03\x00\x00\x00\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_STATUS, "associated");

    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");
    tcase_add_checked_fixture (tc, setup, teardown);
    // 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;
}

/* check cco switch to proxy */
START_TEST (test_refresh_status_cco_1)
{
    unsigned int length;
    char buffer[1024];

    mme_put (&mme_ctx, "\x00\x01\x00\x00\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_CCO, "station");

    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_CCO, buffer, sizeof(buffer));

    fail_unless (!strcmp (buffer, "proxy")
                 && (LIBSPID_TRUE == is_info_manager_needed),
                 "CCO = proxy");
}
END_TEST

/* check cco switch to station */
START_TEST (test_refresh_status_cco_2)
{
    unsigned int length;
    char buffer[1024];

    mme_put (&mme_ctx, "\x00\x00\x00\x00\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_CCO, "proxy");

    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_CCO, buffer, sizeof(buffer));

    fail_unless (!strcmp (buffer, "station")
                 && (LIBSPID_TRUE == is_info_manager_needed),
                 "CCO = station");
}
END_TEST

/* check cco switch to main with WAS_CCO=no update */
START_TEST (test_refresh_status_cco_3)
{
    unsigned int length;
    char buffer1[256], buffer2[256];

    mme_put (&mme_ctx, "\x02\x02\x00\x00\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_CCO, "station");
    libspid_config_write_item (plcd_ctx.hpav_conf_path, LIBSPID_HPAV_CONF_LABEL_WAS_CCO, "no");

    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_CCO, buffer1, sizeof(buffer1));
    libspid_config_read_item (plcd_ctx.hpav_conf_path, LIBSPID_HPAV_CONF_LABEL_WAS_CCO, buffer2, sizeof(buffer2));

    fail_unless (!strcmp (buffer1, "main")
                 && !strcmp (buffer2, "yes")
                 && (LIBSPID_TRUE == is_info_manager_needed)
                 && (LIBSPID_TRUE == is_save_conf_needed),
                 "CCO = main with WAS_CCO = no (status=%s, was_cco=%s)", buffer1, buffer2);
}
END_TEST

/* check cco switch to station with WAS_CCO=yes update */
START_TEST (test_refresh_status_cco_4)
{
    unsigned int length;
    char buffer1[256], buffer2[256];

    mme_put (&mme_ctx, "\x02\x00\x00\x00\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_CCO, "main");
    libspid_config_write_item (plcd_ctx.hpav_conf_path, LIBSPID_HPAV_CONF_LABEL_WAS_CCO, "yes");

    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_CCO, buffer1, sizeof(buffer1));
    libspid_config_read_item (plcd_ctx.hpav_conf_path, LIBSPID_HPAV_CONF_LABEL_WAS_CCO, buffer2, sizeof(buffer2));

    fail_unless (!strcmp (buffer1, "station")
                 && !strcmp (buffer2, "no")
                 && (LIBSPID_TRUE == is_info_manager_needed)
                 && (LIBSPID_TRUE == is_save_conf_needed),
                 "CCO = station with WAS_CCO = yes (status=%s, was_cco=%s)", buffer1, buffer2);
}
END_TEST

TCase *event_refresh_status_cco_tcase (void)
{
    TCase *tc = tcase_create ("refresh_status_cco");
    tcase_add_checked_fixture (tc, setup, teardown);
    // check STATUS field
    tcase_add_test (tc, test_refresh_status_cco_1);
    tcase_add_test (tc, test_refresh_status_cco_2);
    tcase_add_test (tc, test_refresh_status_cco_3);
    tcase_add_test (tc, test_refresh_status_cco_4);
    return tc;
}

/* check cco preferred from no to yes*/
START_TEST (test_refresh_status_cco_preferred_1)
{
    unsigned int length;
    char buffer[256];

    mme_put (&mme_ctx, "\x00\x00\x01\x00\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_conf_path, LIBSPID_HPAV_CONF_LABEL_CCO_PREFERRED, "no");

    refresh_status (&plcd_ctx, &mme_ctx, &is_save_conf_needed, &is_info_manager_needed);
    libspid_config_read_item (plcd_ctx.hpav_conf_path, LIBSPID_HPAV_CONF_LABEL_CCO_PREFERRED, buffer, sizeof(buffer));

    fail_unless (!strcmp (buffer, "yes")
                 && (LIBSPID_TRUE == is_save_conf_needed),
                 "CCO_PREFERRED = yes");
}
END_TEST

/* check cco preferred from yes to no */
START_TEST (test_refresh_status_cco_preferred_2)
{
    unsigned int length;
    char buffer[256];

    mme_put (&mme_ctx, "\x00\x00\x00\x00\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_conf_path, LIBSPID_HPAV_CONF_LABEL_CCO_PREFERRED, "yes");

    refresh_status (&plcd_ctx, &mme_ctx, &is_save_conf_needed, &is_info_manager_needed);
    libspid_config_read_item (plcd_ctx.hpav_conf_path, LIBSPID_HPAV_CONF_LABEL_CCO_PREFERRED, buffer, sizeof(buffer));

    fail_unless (!strcmp (buffer, "no")
                 && (LIBSPID_TRUE == is_save_conf_needed),
                 "CCO_PREFERRED = no");
}
END_TEST

TCase *event_refresh_status_cco_preferred_tcase (void)
{
    TCase *tc = tcase_create ("refresh_status_cco_preferred");
    tcase_add_checked_fixture (tc, setup, teardown);
    tcase_add_test (tc, test_refresh_status_cco_preferred_1);
    tcase_add_test (tc, test_refresh_status_cco_preferred_2);
    return tc;
}

/* check cco backup from no to yes */
START_TEST (test_refresh_status_cco_backup_1)
{
    unsigned int length;
    char buffer[256];

    mme_put (&mme_ctx, "\x00\x00\x00\x01\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_BACKUP_CCO, "no");

    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_BACKUP_CCO, buffer, sizeof(buffer));

    fail_unless (!strcmp (buffer, "yes")
                 && (LIBSPID_TRUE == is_info_manager_needed),
                 "BACKUP_CCO = yes");
}
END_TEST

/* check cco backup from yes to no */
START_TEST (test_refresh_status_cco_backup_2)
{
    unsigned int length;
    char buffer[256];

    mme_put (&mme_ctx, "\x00\x00\x00\x00\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_BACKUP_CCO, "yes");

    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_BACKUP_CCO, buffer, sizeof(buffer));

    fail_unless (!strcmp (buffer, "no")
                 && (LIBSPID_TRUE == is_info_manager_needed),
                 "BACKUP_CCO = no");
}
END_TEST

TCase *event_refresh_status_cco_backup_tcase (void)
{
    TCase *tc = tcase_create ("refresh_status_cco_backup");
    tcase_add_checked_fixture (tc, setup, teardown);
    tcase_add_test (tc, test_refresh_status_cco_backup_1);
    tcase_add_test (tc, test_refresh_status_cco_backup_2);
    return tc;
}

/* check SC from no to yes */
START_TEST (test_refresh_status_sc_1)
{
    unsigned int length;
    char buffer[256];

    mme_put (&mme_ctx, "\x00\x00\x00\x00\x01", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_SC, "no");

    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_SC, buffer, sizeof(buffer));

    fail_unless (!strcmp (buffer, "yes")
                 && (LIBSPID_TRUE == is_info_manager_needed),
                 "SC = yes");
}
END_TEST

/* check sc from yes to no */
START_TEST (test_refresh_status_sc_2)
{
    unsigned int length;
    char buffer[256];

    mme_put (&mme_ctx, "\x00\x00\x00\x00\x00", 5, &length);
    libspid_config_write_item (plcd_ctx.hpav_info_path, LIBSPID_HPAV_INFO_LABEL_SC, "yes");

    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_SC, buffer, sizeof(buffer));

    fail_unless (!strcmp (buffer, "no")
                 && (LIBSPID_TRUE == is_info_manager_needed),
                 "BACKUP_SC = no");
}
END_TEST

TCase *event_refresh_status_sc_tcase (void)
{
    TCase *tc = tcase_create ("refresh_status_sc");
    tcase_add_checked_fixture (tc, setup, teardown);
    tcase_add_test (tc, test_refresh_status_sc_1);
    tcase_add_test (tc, test_refresh_status_sc_2);
    return tc;
}


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());
    suite_add_tcase (s, event_refresh_status_cco_tcase());
    suite_add_tcase (s, event_refresh_status_cco_preferred_tcase());
    suite_add_tcase (s, event_refresh_status_cco_backup_tcase());
    suite_add_tcase (s, event_refresh_status_sc_tcase());
    suite_add_tcase (s, event_refresh_check_sc_start_tcase ());
    return s;
}

int main(void)
{
    Suite *s = suite_create ("event");
    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;
}