Statistics
| Branch: | Revision:

peerstreamer-src / Test / pschannel_bucket_test.c @ cea8d274

History | View | Annotate | Download (5.74 KB)

1 4a486a01 Luca Baldesi
/*
2
 *
3
 *  copyright (c) 2017 luca baldesi
4
 *
5
 */
6
7
#include<assert.h>
8
#include<stdlib.h>
9
#include<stdio.h>
10
#include<string.h>
11
12
#include"pschannel.h"
13 1e7c49cb Luca Baldesi
#include"pstreamer.h"
14 4a486a01 Luca Baldesi
15
16
void pschannel_bucket_destroy_test()
17
{
18
        struct pschannel_bucket * pb = NULL;
19
20
        pschannel_bucket_destroy(NULL);
21
        pschannel_bucket_destroy(&pb);
22
23 1e7c49cb Luca Baldesi
        pb = pschannel_bucket_new(NULL, NULL);
24 4a486a01 Luca Baldesi
        pschannel_bucket_destroy(&pb);
25
        fprintf(stderr,"%s successfully passed!\n",__func__);
26
}
27
28
void pschannel_bucket_insert_test()
29
{
30
        struct pschannel_bucket * pb = NULL;
31
        uint8_t res;
32
33 c039490c Luca Baldesi
        res = pschannel_bucket_insert(NULL, NULL, NULL, NULL, NULL, NULL);
34 4a486a01 Luca Baldesi
        assert(res);
35
36 1e7c49cb Luca Baldesi
        pb = pschannel_bucket_new(NULL, NULL);
37 c039490c Luca Baldesi
        res = pschannel_bucket_insert(pb, NULL, NULL, NULL, NULL, NULL);
38 4a486a01 Luca Baldesi
        assert(res);
39 c039490c Luca Baldesi
        res = pschannel_bucket_insert(pb, NULL, "10.0.0.1", "8000", NULL, NULL);
40 4a486a01 Luca Baldesi
        assert(res);
41
42 c039490c Luca Baldesi
        res = pschannel_bucket_insert(pb, "channel1", "10.0.0.1", "8000", "1Mbps", "localhost/channel.sdp");
43 4a486a01 Luca Baldesi
        assert(res == 0);
44
45
        pschannel_bucket_destroy(&pb);
46
        fprintf(stderr,"%s successfully passed!\n",__func__);
47
}
48
49
void pschannel_bucket_iter_test()
50
{
51
        struct pschannel_bucket * pb = NULL;
52
        const struct pschannel * iter = NULL;
53
54
        iter = pschannel_bucket_iter(NULL, NULL);
55
        assert(iter == NULL);
56
57 1e7c49cb Luca Baldesi
        pb = pschannel_bucket_new(NULL, NULL);
58 4a486a01 Luca Baldesi
        iter = pschannel_bucket_iter(pb, iter);
59
        assert(iter == NULL);
60
61 c039490c Luca Baldesi
        pschannel_bucket_insert(pb, "channel1", "10.0.0.1", "8000", "1Mbps", "localhost/channel.sdp");
62 4a486a01 Luca Baldesi
        iter = pschannel_bucket_iter(pb, iter);
63
        assert(iter);
64
        iter = pschannel_bucket_iter(pb, iter);
65
        assert(iter == NULL);
66
67
        pschannel_bucket_destroy(&pb);
68
        fprintf(stderr,"%s successfully passed!\n",__func__);
69
}
70
71
void pschannel_bucket_to_json_test()
72
{
73
        struct pschannel_bucket * pb = NULL;
74
        char * s = NULL;
75
76
        s = pschannel_bucket_to_json(pb);
77
        assert(s == NULL);
78
79 1e7c49cb Luca Baldesi
        pb = pschannel_bucket_new(NULL, NULL);
80 4a486a01 Luca Baldesi
81
        s = pschannel_bucket_to_json(pb);
82
        assert(strcmp(s, "[]") == 0);
83
        free(s);
84
85 c039490c Luca Baldesi
        pschannel_bucket_insert(pb, "channel1", "10.0.0.1", "8000", "1Mbps", "localhost/channel.sdp");
86 4a486a01 Luca Baldesi
        s = pschannel_bucket_to_json(pb);
87 f6084213 Luca Baldesi
        assert(strcmp(s, "[{\"name\":\"channel1\",\"ipaddr\":\"10.0.0.1\",\"port\":\"8000\",\"quality\":\"1Mbps\"}]") == 0);
88
        free(s);
89
90 c039490c Luca Baldesi
        pschannel_bucket_insert(pb, "channel2", "10.0.0.1", "8001", "1Mbps", "localhost/channel.sdp");
91 f6084213 Luca Baldesi
        s = pschannel_bucket_to_json(pb);
92
        assert(strcmp(s, "[{\"name\":\"channel1\",\"ipaddr\":\"10.0.0.1\",\"port\":\"8000\",\"quality\":\"1Mbps\"},{\"name\":\"channel2\",\"ipaddr\":\"10.0.0.1\",\"port\":\"8001\",\"quality\":\"1Mbps\"}]") == 0);
93 4a486a01 Luca Baldesi
        free(s);
94
95
        pschannel_bucket_destroy(&pb);
96
        fprintf(stderr,"%s successfully passed!\n",__func__);
97
}
98
99 c2bc5145 Luca Baldesi
void pschannel_bucket_loadfile_test()
100
{
101
        const char * testfile = "/tmp/channel_test_file.csv";
102
        struct pschannel_bucket * psb = NULL;
103
        const struct pschannel * ch;
104
        int8_t res = 0;
105
        FILE* fd;
106
107
        res = pschannel_bucket_loadfile(psb);
108
        assert(res == -1);
109
110 1e7c49cb Luca Baldesi
        psb = pschannel_bucket_new(NULL, NULL);
111 c2bc5145 Luca Baldesi
        res = pschannel_bucket_loadfile(psb);
112
        assert(res == -1);
113
        pschannel_bucket_destroy(&psb);
114
115 1e7c49cb Luca Baldesi
        psb = pschannel_bucket_new("nonexisting", NULL);
116 c2bc5145 Luca Baldesi
        res = pschannel_bucket_loadfile(psb);
117
        assert(res == -1);
118
        pschannel_bucket_destroy(&psb);
119
120
        fd = fopen(testfile, "w");
121
        fprintf(fd, "ch1,ip1,port1,q1,addr1\n");
122
        fclose(fd);
123
124 1e7c49cb Luca Baldesi
        psb = pschannel_bucket_new(testfile, NULL);
125 c2bc5145 Luca Baldesi
        res = pschannel_bucket_loadfile(psb);
126
        assert(res == 0);
127
        ch = pschannel_bucket_find(psb, "ip1", "port1");
128
        assert(ch);
129
        pschannel_bucket_destroy(&psb);
130
131
        fprintf(stderr,"%s successfully passed!\n",__func__);
132
}
133
134 1e7c49cb Luca Baldesi
void pschannel_load_local_streams_test()
135
{
136
        struct pstreamer_manager * psm;
137
        struct pschannel_bucket * pb = NULL;
138
        const struct pschannel * iter = NULL;
139
        struct pstreamer * source;
140
141
        assert(pschannel_bucket_load_local_streams(pb));
142
143
        psm = pstreamer_manager_new(6000, NULL);
144
        pb = pschannel_bucket_new(NULL, psm);
145
146
        // empty bucket        
147
        assert(pschannel_bucket_load_local_streams(pb) == 0);
148
        iter = pschannel_bucket_iter(pb, NULL);
149
        assert(iter == NULL);
150
151
        // one local source, without display name
152
        pstreamer_manager_create_streamer(psm, "10.0.0.1", "6000", "42", "127.0.0.1", NULL);
153
        source = (struct pstreamer *) pstreamer_manager_create_source_streamer(psm, "room1", "127.0.0.1", NULL);
154
        assert(pschannel_bucket_load_local_streams(pb) == 0);
155
        iter = pschannel_bucket_iter(pb, NULL);
156
        assert(iter == NULL);
157
158
        pstreamer_set_display_name(source, "Channel1");
159
        assert(pschannel_bucket_load_local_streams(pb) == 0);
160
        iter = pschannel_bucket_iter(pb, NULL);
161
        assert(iter != NULL);
162
        assert(strcmp(((struct pschannel *)iter)->name, "Channel1") == 0);
163
        iter = pschannel_bucket_iter(pb, iter);
164
        assert(iter == NULL);
165
166
        pstreamer_manager_destroy(&psm);
167
        pschannel_bucket_destroy(&pb);
168
        fprintf(stderr,"%s successfully passed!\n",__func__);
169
}
170
171 cea8d274 Luca Baldesi
void pschannel_bucket_save2file_test()
172
{
173
        struct pschannel_bucket * psb = NULL;
174
        const struct pschannel * ch;
175
        int8_t res = 0;
176
177
        res = pschannel_bucket_save2file(psb);
178
        assert(res);
179
180
        psb = pschannel_bucket_new(NULL, NULL);
181
        res = pschannel_bucket_save2file(psb);
182
        assert(res);
183
        pschannel_bucket_destroy(&psb);
184
185
        psb = pschannel_bucket_new("/tmp/channel_test_file.csv", NULL);
186
        pschannel_bucket_insert(psb, "local_channel", "10.0.0.1", "8000", "1Mbps", "localhost/channel.sdp");
187
        res = pschannel_bucket_save2file(psb);
188
        assert(res == 0);
189
        pschannel_bucket_destroy(&psb);
190
191
        psb = pschannel_bucket_new("/tmp/channel_test_file_out.csv", NULL);
192
        res = pschannel_bucket_loadfile(psb);
193
        assert(res == 0);
194
        ch = pschannel_bucket_find(psb, "10.0.0.1", "8000");
195
        assert(ch);
196
        assert(res == 0);
197
        pschannel_bucket_destroy(&psb);
198
199
        fprintf(stderr,"%s successfully passed!\n",__func__);
200
}
201
202 4a486a01 Luca Baldesi
int main(int argv, char ** argc)
203
{
204
        pschannel_bucket_destroy_test();
205
        pschannel_bucket_insert_test();
206
        pschannel_bucket_iter_test();
207
        pschannel_bucket_to_json_test();
208 c2bc5145 Luca Baldesi
        pschannel_bucket_loadfile_test();
209 1e7c49cb Luca Baldesi
        pschannel_load_local_streams_test();
210 cea8d274 Luca Baldesi
        pschannel_bucket_save2file_test();
211 4a486a01 Luca Baldesi
        return 0;
212
}