summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/tests/spidlib/utests/src/eoc_utests.c
blob: 71d969e2e46553969e05514be5bee0dd60b10d98 (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
/* Cleopatre project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    eoc_utests.c
 * \brief   Unitary tests for spidlib
 * \ingroup Cleopatre - spidlib
 *
 * This file content all the unitary tests for EoC SPiDLib.
 */

#include <check.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <net/if.h>
#include <libgen.h>    /* for basename() */

#include "eoc_utests.h"

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

#define ONLINE_INFO "testfiles/online.info"

/** local variables */
typedef char mac_t[6];

/* fixtures - run before and after each unit test */
void setup(void)
{
    int ret;
    struct stat st;
	char buf[256];

    /* initialize - delete testing dir if it exists */
    if ( (stat (UTESTS_TMP_DIR, &st) == 0) )
    {
        system ("rm -r " UTESTS_TMP_DIR);
    }
    ret = mkdir(UTESTS_TMP_DIR, 0770);
    if (ret < 0 && errno != EEXIST)
    {
        perror("mkdir(UTESTS_TMP_DIR, 0770)");
        exit(EXIT_FAILURE);
    }

    /* create other subdirs */
    sprintf(buf, "%s/etc", UTESTS_TMP_DIR);
    ret = mkdir(buf, 0770);
    if (ret < 0 && errno != EEXIST)
    {
        perror("mkdir(UTESTS_TMP_DIR/etc, 0770)");
        exit(EXIT_FAILURE);
    }
}

void teardown(void)
{
}

/* --- TEST PROCEDURES --- */
/* TESTING FALSE PARAMS */
START_TEST (test_eoc_get_topo)
{
	int ret = SPIDLIB_SUCCESS;
    char mac_address_list;
	int mac_address_count = 0;

    system("cp " ONLINE_INFO " " UTESTS_TMP_DIR "/etc");

    /* mac_address_list = NULL */
    ret = spidlib_eoc_get_topo( NULL, &mac_address_count );
    fail_unless( ret == SPIDLIB_ERROR_PARAM, "mac_address_list = NULL");

    /* mac_address_count = NULL */
    ret = spidlib_eoc_get_topo ( &mac_address_list, NULL );
    fail_unless( ret == SPIDLIB_ERROR_PARAM, "mac_address_count = NULL");
}
END_TEST

TCase *
tc_param (void)
{
    TCase *tc_param = tcase_create ("param");
    tcase_add_test (tc_param, test_eoc_get_topo);
    return tc_param;
}

/* TEST THAT SHOULD PASS */
/** test procedures */
START_TEST (test_spidlib_eoc_get_topo)
{
    int ret;
    mac_t mac_address_list[5];
	int mac_address_count = 0;

    system("cp " ONLINE_INFO " " UTESTS_TMP_DIR "/etc");
    ret = spidlib_eoc_get_topo (mac_address_list[0], &mac_address_count);
    fail_if(ret != SPIDLIB_SUCCESS, "spidlib_eoc_get_topo fail");
    fail_if( strcmp(mac_address_list[0], "\x00\xd7\x20\x00\x00\x01") != 0, "spidlib_eoc_get_topo gets wrong mac address 1");
    fail_if( strcmp(mac_address_list[1], "\x00\xd7\x20\x00\x00\x02") != 0, "spidlib_eoc_get_topo gets wrong mac address 2");
    fail_if( strcmp(mac_address_list[2], "\x00\xd7\x20\x00\x00\x03") != 0, "spidlib_eoc_get_topo gets wrong mac address 3");
    fail_if( strcmp(mac_address_list[3], "\x00\xd7\x20\x00\x00\x04") != 0, "spidlib_eoc_get_topo gets wrong mac address 4");
    fail_if( strcmp(mac_address_list[4], "\x00\xd7\x20\x00\x00\x05") != 0, "spidlib_eoc_get_topo gets wrong mac address 5");
    fail_if(mac_address_count != 5, "spidlib_eoc_get_topo gets wrong mac address count");
}
END_TEST

extern Suite* spidlib_eoc_suite(void)
{
    Suite *s = suite_create("SPIDLIB_EOC");
    TCase *tc_core = tcase_create("Core");
    tcase_add_checked_fixture (tc_core, setup, teardown);

    //Test eoc_get_topo
    tcase_add_test(tc_core, test_spidlib_eoc_get_topo);

    suite_add_tcase(s, tc_core);
    suite_add_tcase(s, tc_param());
    return s;
}

int main(void)
{
    int number_failed = 0;
    Suite *s;

    //Run EoC tests
    s = spidlib_eoc_suite();

    SRunner *sr = srunner_create(s);
    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;
}