| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "librist/librist.h" |
| 6 |
#include "librist/peer.h" |
| 7 |
#include "librist/receiver.h" |
| 8 |
#include "librist/logging.h" |
| 9 |
#include "librist/urlparam.h" |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
static int parse_cfg(const char *url, struct rist_peer_config **cfg) |
| 16 |
{ |
| 17 |
*cfg = NULL; |
| 18 |
int ret = rist_parse_address2(url, cfg); |
| 19 |
if (ret != 0 || *cfg == NULL) { |
| 20 |
fprintf(stderr, "parse_cfg(%s) ret=%d cfg=%p\n", |
| 21 |
url, ret, (void *)*cfg); |
| 22 |
return -1; |
| 23 |
} |
| 24 |
return 0; |
| 25 |
} |
| 26 |
|
| 27 |
static int test_override_then_match(struct rist_logging_settings *logs) |
| 28 |
{ |
| 29 |
struct rist_ctx *ctx = NULL; |
| 30 |
if (rist_receiver_create(&ctx, RIST_PROFILE_MAIN, logs) != 0) { |
| 31 |
fprintf(stderr, "test1: rist_receiver_create failed\n"); |
| 32 |
return 1; |
| 33 |
} |
| 34 |
|
| 35 |
struct rist_peer_config *cfg1 = NULL, *cfg2 = NULL; |
| 36 |
struct rist_peer *p1 = NULL, *p2 = NULL; |
| 37 |
int fails = 0; |
| 38 |
|
| 39 |
if (parse_cfg("rist://@127.0.0.1:30100?profile=2", &cfg1) < 0) { |
| 40 |
fails++; |
| 41 |
goto cleanup; |
| 42 |
} |
| 43 |
if (rist_peer_create(ctx, &p1, cfg1) != 0) { |
| 44 |
fprintf(stderr, |
| 45 |
"test1: first peer (?profile=2) rejected; override should have applied\n"); |
| 46 |
fails++; |
| 47 |
goto cleanup; |
| 48 |
} |
| 49 |
|
| 50 |
if (parse_cfg("rist://@127.0.0.1:30102?profile=2", &cfg2) < 0) { |
| 51 |
fails++; |
| 52 |
goto cleanup; |
| 53 |
} |
| 54 |
if (rist_peer_create(ctx, &p2, cfg2) != 0) { |
| 55 |
fprintf(stderr, |
| 56 |
"test1: second peer with matching ?profile=2 rejected\n"); |
| 57 |
fails++; |
| 58 |
} |
| 59 |
|
| 60 |
cleanup: |
| 61 |
if (cfg1) rist_peer_config_free2(&cfg1); |
| 62 |
if (cfg2) rist_peer_config_free2(&cfg2); |
| 63 |
rist_destroy(ctx); |
| 64 |
return fails; |
| 65 |
} |
| 66 |
|
| 67 |
static int test_conflict_refused(struct rist_logging_settings *logs) |
| 68 |
{ |
| 69 |
struct rist_ctx *ctx = NULL; |
| 70 |
if (rist_receiver_create(&ctx, RIST_PROFILE_MAIN, logs) != 0) { |
| 71 |
fprintf(stderr, "test2: rist_receiver_create failed\n"); |
| 72 |
return 1; |
| 73 |
} |
| 74 |
|
| 75 |
struct rist_peer_config *cfg1 = NULL, *cfg2 = NULL; |
| 76 |
struct rist_peer *p1 = NULL, *p2 = NULL; |
| 77 |
int fails = 0; |
| 78 |
|
| 79 |
if (parse_cfg("rist://@127.0.0.1:30110?profile=2", &cfg1) < 0) { |
| 80 |
fails++; |
| 81 |
goto cleanup; |
| 82 |
} |
| 83 |
if (rist_peer_create(ctx, &p1, cfg1) != 0) { |
| 84 |
fprintf(stderr, "test2: first peer rejected\n"); |
| 85 |
fails++; |
| 86 |
goto cleanup; |
| 87 |
} |
| 88 |
|
| 89 |
if (parse_cfg("rist://@127.0.0.1:30112?profile=1", &cfg2) < 0) { |
| 90 |
fails++; |
| 91 |
goto cleanup; |
| 92 |
} |
| 93 |
int ret = rist_peer_create(ctx, &p2, cfg2); |
| 94 |
if (ret != -1) { |
| 95 |
fprintf(stderr, |
| 96 |
"test2: conflicting ?profile=1 peer returned %d; expected -1\n", |
| 97 |
ret); |
| 98 |
fails++; |
| 99 |
} |
| 100 |
|
| 101 |
cleanup: |
| 102 |
if (cfg1) rist_peer_config_free2(&cfg1); |
| 103 |
if (cfg2) rist_peer_config_free2(&cfg2); |
| 104 |
rist_destroy(ctx); |
| 105 |
return fails; |
| 106 |
} |
| 107 |
|
| 108 |
static int test_late_peer_refused(struct rist_logging_settings *logs) |
| 109 |
{ |
| 110 |
struct rist_ctx *ctx = NULL; |
| 111 |
if (rist_receiver_create(&ctx, RIST_PROFILE_MAIN, logs) != 0) { |
| 112 |
fprintf(stderr, "test3: rist_receiver_create failed\n"); |
| 113 |
return 1; |
| 114 |
} |
| 115 |
|
| 116 |
struct rist_peer_config *cfg_pre = NULL, *cfg_late = NULL; |
| 117 |
struct rist_peer *p_pre = NULL, *p_late = NULL; |
| 118 |
int fails = 0; |
| 119 |
|
| 120 |
if (parse_cfg("rist://@127.0.0.1:30120", &cfg_pre) < 0) { |
| 121 |
fails++; |
| 122 |
goto cleanup; |
| 123 |
} |
| 124 |
if (rist_peer_create(ctx, &p_pre, cfg_pre) != 0) { |
| 125 |
fprintf(stderr, "test3: pre-start peer rejected\n"); |
| 126 |
fails++; |
| 127 |
goto cleanup; |
| 128 |
} |
| 129 |
if (rist_start(ctx) != 0) { |
| 130 |
fprintf(stderr, "test3: rist_start failed\n"); |
| 131 |
fails++; |
| 132 |
goto cleanup; |
| 133 |
} |
| 134 |
|
| 135 |
if (parse_cfg("rist://@127.0.0.1:30122?profile=2", &cfg_late) < 0) { |
| 136 |
fails++; |
| 137 |
goto cleanup; |
| 138 |
} |
| 139 |
int ret = rist_peer_create(ctx, &p_late, cfg_late); |
| 140 |
if (ret != -1) { |
| 141 |
fprintf(stderr, |
| 142 |
"test3: late peer with ?profile=2 returned %d; expected -1\n", |
| 143 |
ret); |
| 144 |
fails++; |
| 145 |
} |
| 146 |
|
| 147 |
cleanup: |
| 148 |
if (cfg_pre) rist_peer_config_free2(&cfg_pre); |
| 149 |
if (cfg_late) rist_peer_config_free2(&cfg_late); |
| 150 |
rist_destroy(ctx); |
| 151 |
return fails; |
| 152 |
} |
| 153 |
|
| 154 |
int main(void) |
| 155 |
{ |
| 156 |
struct rist_logging_settings *logs = NULL; |
| 157 |
if (rist_logging_set(&logs, RIST_LOG_WARN, NULL, NULL, NULL, |
| 158 |
stderr) != 0) { |
| 159 |
fprintf(stderr, "rist_logging_set failed\n"); |
| 160 |
return 1; |
| 161 |
} |
| 162 |
|
| 163 |
int fails = 0; |
| 164 |
fails += test_override_then_match(logs); |
| 165 |
fails += test_conflict_refused(logs); |
| 166 |
fails += test_late_peer_refused(logs); |
| 167 |
|
| 168 |
rist_logging_settings_free2(&logs); |
| 169 |
|
| 170 |
if (fails == 0) { |
| 171 |
printf("OK\n"); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
fprintf(stderr, "Total failures: %d\n", fails); |
| 175 |
return 1; |
| 176 |
} |