Statistics
| Branch: | Revision:

peerstreamer-src / Test / pschannel_bucket_test.c @ cea8d274

History | View | Annotate | Download (5.74 KB)

1
/*
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
#include"pstreamer.h"
14

    
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
        pb = pschannel_bucket_new(NULL, NULL);
24
        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
        res = pschannel_bucket_insert(NULL, NULL, NULL, NULL, NULL, NULL);
34
        assert(res);
35

    
36
        pb = pschannel_bucket_new(NULL, NULL);
37
        res = pschannel_bucket_insert(pb, NULL, NULL, NULL, NULL, NULL);
38
        assert(res);
39
        res = pschannel_bucket_insert(pb, NULL, "10.0.0.1", "8000", NULL, NULL);
40
        assert(res);
41

    
42
        res = pschannel_bucket_insert(pb, "channel1", "10.0.0.1", "8000", "1Mbps", "localhost/channel.sdp");
43
        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
        pb = pschannel_bucket_new(NULL, NULL);
58
        iter = pschannel_bucket_iter(pb, iter);
59
        assert(iter == NULL);
60

    
61
        pschannel_bucket_insert(pb, "channel1", "10.0.0.1", "8000", "1Mbps", "localhost/channel.sdp");
62
        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
        pb = pschannel_bucket_new(NULL, NULL);
80

    
81
        s = pschannel_bucket_to_json(pb);
82
        assert(strcmp(s, "[]") == 0);
83
        free(s);
84

    
85
        pschannel_bucket_insert(pb, "channel1", "10.0.0.1", "8000", "1Mbps", "localhost/channel.sdp");
86
        s = pschannel_bucket_to_json(pb);
87
        assert(strcmp(s, "[{\"name\":\"channel1\",\"ipaddr\":\"10.0.0.1\",\"port\":\"8000\",\"quality\":\"1Mbps\"}]") == 0);
88
        free(s);
89

    
90
        pschannel_bucket_insert(pb, "channel2", "10.0.0.1", "8001", "1Mbps", "localhost/channel.sdp");
91
        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
        free(s);
94

    
95
        pschannel_bucket_destroy(&pb);
96
        fprintf(stderr,"%s successfully passed!\n",__func__);
97
}
98

    
99
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
        psb = pschannel_bucket_new(NULL, NULL);
111
        res = pschannel_bucket_loadfile(psb);
112
        assert(res == -1);
113
        pschannel_bucket_destroy(&psb);
114

    
115
        psb = pschannel_bucket_new("nonexisting", NULL);
116
        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
        psb = pschannel_bucket_new(testfile, NULL);
125
        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
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
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
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
        pschannel_bucket_loadfile_test();
209
        pschannel_load_local_streams_test();
210
        pschannel_bucket_save2file_test();
211
        return 0;
212
}