Debian Codegraph @

test/rist/unit/test_peer_config_versioned_defaults.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * Verifies rist_peer_config_defaults_set_versioned() writes only fields
4 * that exist at or below the requested version, so a config struct from
5 * an older RIST_PEER_CONFIG_VERSION is never written past its end.
6 *
7 * Field introduction versions (see include/librist/peer.h history):
8 * split_mode, merge_mode : version 1
9 * profile, profile_set : version 4
10 * rtt_drop et al : version 6
11 *
12 * Links against the public library only; no cmocka. */
13
14 #include "librist/librist.h"
15 #include "librist/peer.h"
16
17 #include <stdio.h>
18 #include <string.h>
19
20 #define SENTINEL 0x5a5a5a5a
21
22 static int check(int version, int expect_split_set, int expect_profile_set,
23 int expect_rtt_set)
24 {
25 struct rist_peer_config cfg;
26 memset(&cfg, 0, sizeof(cfg));
27 cfg.split_mode = SENTINEL;
28 cfg.merge_mode = SENTINEL;
29 cfg.profile = (enum rist_profile)SENTINEL;
30 cfg.profile_set = SENTINEL;
31 cfg.rtt_drop = SENTINEL;
32 cfg.rtt_restore = SENTINEL;
33 cfg.rtt_drop_settle = SENTINEL;
34 cfg.rtt_drop_trickle = SENTINEL;
35
36 if (rist_peer_config_defaults_set_versioned(&cfg, version) != 0) {
37 fprintf(stderr, "FAIL: versioned(v=%d) returned error\n", version);
38 return 1;
39 }
40
41 int failures = 0;
42 if (cfg.version != version) {
43 fprintf(stderr, "FAIL: v=%d recorded version=%d\n",
44 version, cfg.version);
45 failures++;
46 }
47
48 int split_written = (cfg.split_mode != SENTINEL) ||
49 (cfg.merge_mode != SENTINEL);
50 if (split_written != expect_split_set) {
51 fprintf(stderr, "FAIL: v=%d split/merge written=%d (want %d)\n",
52 version, split_written, expect_split_set);
53 failures++;
54 }
55
56 int profile_written = ((int)cfg.profile != (int)(enum rist_profile)SENTINEL) ||
57 (cfg.profile_set != SENTINEL);
58 if (profile_written != expect_profile_set) {
59 fprintf(stderr, "FAIL: v=%d profile written=%d (want %d)\n",
60 version, profile_written, expect_profile_set);
61 failures++;
62 }
63
64 int rtt_written = (cfg.rtt_drop != SENTINEL) || (cfg.rtt_restore != SENTINEL) ||
65 (cfg.rtt_drop_settle != SENTINEL) || (cfg.rtt_drop_trickle != SENTINEL);
66 if (rtt_written != expect_rtt_set) {
67 fprintf(stderr, "FAIL: v=%d rtt fields written=%d (want %d)\n",
68 version, rtt_written, expect_rtt_set);
69 failures++;
70 }
71 return failures;
72 }
73
74 int main(void)
75 {
76 int failures = 0;
77
78 failures += check(0, 0, 0, 0);
79 failures += check(1, 1, 0, 0);
80 failures += check(3, 1, 0, 0);
81 failures += check(4, 1, 1, 0);
82 failures += check(5, 1, 1, 0);
83 failures += check(6, 1, 1, 1);
84
85 /* The public header maps rist_peer_config_defaults_set() to the
86 * caller's compiled RIST_PEER_CONFIG_VERSION. */
87 struct rist_peer_config cfg;
88 memset(&cfg, 0, sizeof(cfg));
89 if (rist_peer_config_defaults_set(&cfg) != 0) {
90 fprintf(stderr, "FAIL: defaults_set returned error\n");
91 failures++;
92 } else if (cfg.version != RIST_PEER_CONFIG_VERSION) {
93 fprintf(stderr,
94 "FAIL: defaults_set recorded version=%d (want %d)\n",
95 cfg.version, RIST_PEER_CONFIG_VERSION);
96 failures++;
97 }
98
99 if (failures == 0) {
100 printf("OK\n");
101 return 0;
102 }
103 fprintf(stderr, "Total failures: %d\n", failures);
104 return 1;
105 }