Skip to content

Commit 817e009

Browse files
committed
Add rustls_result test
1 parent 95aa4f4 commit 817e009

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

tests/unit/rustls_result.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// no-compile: refcount
2+
#include <stdio.h>
3+
4+
#include "../../rules/rustls/rustls.h"
5+
6+
static int to_code(rustls_result r) {
7+
switch (r) {
8+
case RUSTLS_RESULT_OK:
9+
return 0;
10+
case RUSTLS_RESULT_NULL_PARAMETER:
11+
return 1;
12+
default:
13+
return 2;
14+
}
15+
}
16+
17+
static int classify(rustls_result r) {
18+
if (r == RUSTLS_RESULT_PLAINTEXT_EMPTY) {
19+
return 10;
20+
} else if (r == RUSTLS_RESULT_UNEXPECTED_EOF) {
21+
return 20;
22+
} else if (r != RUSTLS_RESULT_OK) {
23+
return 30;
24+
}
25+
return 0;
26+
}
27+
28+
static void describe(rustls_result r, char *buf, size_t len, size_t *out_n) {
29+
size_t n = 0;
30+
if (r != RUSTLS_RESULT_OK && len > 0) {
31+
buf[n++] = '!';
32+
}
33+
*out_n = n;
34+
}
35+
36+
static rustls_result init_zero(void) {
37+
rustls_result r = 0;
38+
return r;
39+
}
40+
41+
static rustls_result reset(rustls_result r) {
42+
r = 0;
43+
return r;
44+
}
45+
46+
static int is_unset(rustls_result r) { return r == 0; }
47+
48+
int main(void) {
49+
rustls_result r = RUSTLS_RESULT_OK;
50+
char buf[16];
51+
size_t n = 0;
52+
53+
printf("%d\n", to_code(r));
54+
printf("%d\n", to_code(RUSTLS_RESULT_NULL_PARAMETER));
55+
printf("%d\n", classify(RUSTLS_RESULT_PLAINTEXT_EMPTY));
56+
printf("%d\n", classify(RUSTLS_RESULT_UNEXPECTED_EOF));
57+
printf("%d\n", classify(RUSTLS_RESULT_OK));
58+
59+
describe(RUSTLS_RESULT_NULL_PARAMETER, buf, sizeof(buf), &n);
60+
printf("%d\n", (int)n);
61+
describe(RUSTLS_RESULT_OK, buf, sizeof(buf), &n);
62+
printf("%d\n", (int)n);
63+
64+
printf("%d\n", is_unset(init_zero()));
65+
printf("%d\n", to_code(init_zero()));
66+
printf("%d\n", is_unset(reset(RUSTLS_RESULT_OK)));
67+
return 0;
68+
}

0 commit comments

Comments
 (0)