Statistics
| Branch: | Revision:

pstreamer / test / net_helper_test.c @ 662a3ab9

History | View | Annotate | Download (4.07 KB)

1
#include<malloc.h>
2
#include<assert.h>
3
#include<string.h>
4
#include<net_helper.h>
5

    
6
void create_node_test()
7
{
8
        struct nodeID * s;
9

    
10
        s = create_node(NULL, -1);
11
        assert(s == NULL);
12
        s = create_node(NULL, 6000);
13
        assert(s == NULL);
14
        s = create_node("192.168.0.1", 0);
15
        assert(s == NULL);
16

    
17
        s = create_node("192.168.0.1", 6000);
18
        assert(s);
19
        nodeid_free(s);
20

    
21
        fprintf(stderr,"%s successfully passed!\n",__func__);
22
}
23

    
24
void net_helper_init_test()
25
{
26
        struct nodeID * node;
27

    
28
        node = net_helper_init(NULL, 0, NULL);
29
        assert(node == NULL);
30
        node = net_helper_init("10.0.0.1", 0, NULL);
31
        assert(node == NULL);
32

    
33
        node = net_helper_init("10.0.0.1", 6000, NULL);
34
        assert(node == NULL);
35

    
36
        node = net_helper_init("127.0.0.1", 6000, NULL);
37
        assert(node);
38
        nodeid_free(node);
39

    
40
        node = net_helper_init("::1", 6000, NULL);
41
        assert(node);
42
        nodeid_free(node);
43

    
44
        fprintf(stderr,"%s successfully passed!\n",__func__);
45
}
46

    
47
void nodeid_dup_test()
48
{
49
        struct nodeID * n1, * n2;
50

    
51
        n2 = nodeid_dup(NULL);
52
        assert(n2 == NULL);
53

    
54
        n1 = create_node("127.0.0.1", 6000);
55
        n2 = nodeid_dup(n1);
56
        assert(n2);
57

    
58
        nodeid_free(n1);
59
        nodeid_free(n2);
60
        fprintf(stderr,"%s successfully passed!\n",__func__);
61
}
62

    
63
void nodeid_equal_test()
64
{
65
        struct nodeID * n1, * n2;
66

    
67
        assert(nodeid_equal(NULL, NULL) == 0);
68

    
69
        n1 = create_node("127.0.0.1", 6000);
70
        assert(nodeid_equal(n1, NULL) == 0);
71

    
72
        assert(nodeid_equal(n1, n1));
73
        
74
        n2 = create_node("127.0.0.1", 6000);
75
        assert(nodeid_equal(n1, n2));
76

    
77
        nodeid_free(n2);
78
        n2 = create_node("127.0.0.1", 6001);
79
        assert(nodeid_equal(n1, n2) == 0);
80

    
81
        nodeid_free(n2);
82
        n2 = create_node("127.0.0.2", 6000);
83
        assert(nodeid_equal(n1, n2) == 0);
84

    
85
        nodeid_free(n1);
86
        nodeid_free(n2);
87
        fprintf(stderr,"%s successfully passed!\n",__func__);
88
}
89

    
90
void nodeid_cmp_test()
91
{
92
        struct nodeID * n1, * n2;
93

    
94
        assert(nodeid_cmp(NULL, NULL) == 0);
95

    
96
        n1 = create_node("127.0.0.1", 6000);
97
        assert(nodeid_cmp(n1, NULL) > 0);
98

    
99
        assert(nodeid_cmp(n1, n1) == 0);
100
        
101
        n2 = create_node("127.0.0.1", 6001);
102
        assert(nodeid_cmp(n1, n2) < 0);
103

    
104
        nodeid_free(n2);
105
        n2 = create_node("127.0.0.2", 6000);
106
        assert(nodeid_cmp(n1, n2) < 0);
107

    
108
        nodeid_free(n1);
109
        nodeid_free(n2);
110
        fprintf(stderr,"%s successfully passed!\n",__func__);
111
}
112

    
113
void node_addr_test()
114
{
115
        struct nodeID * s = NULL;
116
        char buff[80];
117
        int res;
118

    
119
        res = node_addr(s, NULL, 0);
120
        assert(res < 0);
121
        res = node_addr(s, buff, 0);
122
        assert(res < 0);
123

    
124
        s = create_node("10.0.0.1", 6000);
125
        res = node_addr(s, buff, 0);
126
        assert(res < 0);
127
        nodeid_free(s);
128

    
129
        s = create_node("10.0.0.1", 6000);
130
        res = node_addr(s, buff, 2);
131
        assert(res < 0);
132
        nodeid_free(s);
133

    
134
        s = create_node("10.0.0.1", 6000);
135
        res = node_addr(s, buff, 80);
136
        assert(res > 0);
137
        assert(strcmp(buff, "10.0.0.1:6000") == 0);
138
        nodeid_free(s);
139

    
140
        s = create_node("::1", 6000);
141
        res = node_addr(s, buff, 80);
142
        assert(res > 0);
143
        assert(strcmp(buff, "::1:6000") == 0);
144
        nodeid_free(s);
145

    
146
        fprintf(stderr,"%s successfully passed!\n",__func__);
147
}
148

    
149
void nodeid_dump_test()
150
{
151
        struct nodeID *s = NULL, *s2 = NULL;
152
        uint8_t buff[80];
153
        int res, len;
154

    
155
        res = nodeid_dump(NULL, s, 0);
156
        assert(res < 0);
157

    
158
        res = nodeid_dump(buff, s, 0);
159
        assert(res < 0);
160

    
161
        res = nodeid_dump(buff, s, 80);
162
        assert(res < 0);
163

    
164
        s = create_node("10.0.0.1", 6000);
165
        res = nodeid_dump(buff, s, 80);
166
        assert(res > 0);
167

    
168
        s2 = nodeid_undump(NULL, NULL);
169
        assert(s2 == NULL);
170

    
171
        s2 = nodeid_undump(buff, NULL);
172
        assert(s2 == NULL);
173

    
174
        s2 = nodeid_undump(buff, &len);
175
        assert(s2);
176
        assert(nodeid_equal(s, s2));
177

    
178
        nodeid_free(s);
179
        nodeid_free(s2);
180

    
181
        fprintf(stderr,"%s successfully passed!\n",__func__);
182
}
183

    
184
void send_recv_test()
185
{
186
        struct nodeID * n1, *n2, *r;
187
        char buff[80];
188
        char msg[] = "ciao";
189

    
190
        n1 = net_helper_init("127.0.0.1", 6000, NULL);
191
        n2 = net_helper_init("127.0.0.1", 6001, NULL);
192
        send_to_peer(n1, n2, (uint8_t *)msg, 5);
193
        recv_from_peer(n2, &r, (uint8_t *)buff, 80);
194
        assert(strcmp(msg, buff) == 0);
195
        assert(nodeid_equal(r, n1));
196

    
197
        nodeid_free(n1);
198
        nodeid_free(n2);
199
        nodeid_free(r);
200
        fprintf(stderr,"%s successfully passed!\n",__func__);
201
}
202

    
203
int main()
204
{
205
        create_node_test();
206
        net_helper_init_test();
207
        nodeid_dup_test();
208
        nodeid_equal_test();
209
        nodeid_cmp_test();
210
        node_addr_test();
211
        nodeid_dump_test();
212
        send_recv_test();
213
        return 0;
214
}