diff options
Diffstat (limited to 'regress/unittests/sshbuf/test_sshbuf_fixed.c')
-rw-r--r-- | regress/unittests/sshbuf/test_sshbuf_fixed.c | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/regress/unittests/sshbuf/test_sshbuf_fixed.c b/regress/unittests/sshbuf/test_sshbuf_fixed.c new file mode 100644 index 000000000..62c815a2e --- /dev/null +++ b/regress/unittests/sshbuf/test_sshbuf_fixed.c | |||
@@ -0,0 +1,122 @@ | |||
1 | /* $OpenBSD: test_sshbuf_fixed.c,v 1.1 2014/04/30 05:32:00 djm Exp $ */ | ||
2 | /* | ||
3 | * Regress test for sshbuf.h buffer API | ||
4 | * | ||
5 | * Placed in the public domain | ||
6 | */ | ||
7 | |||
8 | #include <sys/types.h> | ||
9 | #include <sys/param.h> | ||
10 | #include <stdio.h> | ||
11 | #include <stdint.h> | ||
12 | #include <stdlib.h> | ||
13 | #include <string.h> | ||
14 | |||
15 | #include "test_helper.h" | ||
16 | |||
17 | #define SSHBUF_INTERNAL 1 /* access internals for testing */ | ||
18 | #include "sshbuf.h" | ||
19 | #include "ssherr.h" | ||
20 | |||
21 | void sshbuf_fixed(void); | ||
22 | |||
23 | const u_char test_buf[] = "\x01\x12\x34\x56\x78\x00\x00\x00\x05hello"; | ||
24 | |||
25 | void | ||
26 | sshbuf_fixed(void) | ||
27 | { | ||
28 | struct sshbuf *p1, *p2, *p3; | ||
29 | u_char c; | ||
30 | char *s; | ||
31 | u_int i; | ||
32 | size_t l; | ||
33 | |||
34 | TEST_START("sshbuf_from"); | ||
35 | p1 = sshbuf_from(test_buf, sizeof(test_buf)); | ||
36 | ASSERT_PTR_NE(p1, NULL); | ||
37 | ASSERT_PTR_EQ(sshbuf_mutable_ptr(p1), NULL); | ||
38 | ASSERT_INT_EQ(sshbuf_check_reserve(p1, 1), SSH_ERR_BUFFER_READ_ONLY); | ||
39 | ASSERT_INT_EQ(sshbuf_reserve(p1, 1, NULL), SSH_ERR_BUFFER_READ_ONLY); | ||
40 | ASSERT_INT_EQ(sshbuf_set_max_size(p1, 200), SSH_ERR_BUFFER_READ_ONLY); | ||
41 | ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x12345678), SSH_ERR_BUFFER_READ_ONLY); | ||
42 | ASSERT_SIZE_T_EQ(sshbuf_avail(p1), 0); | ||
43 | ASSERT_PTR_EQ(sshbuf_ptr(p1), test_buf); | ||
44 | sshbuf_free(p1); | ||
45 | TEST_DONE(); | ||
46 | |||
47 | TEST_START("sshbuf_from data"); | ||
48 | p1 = sshbuf_from(test_buf, sizeof(test_buf) - 1); | ||
49 | ASSERT_PTR_NE(p1, NULL); | ||
50 | ASSERT_PTR_EQ(sshbuf_ptr(p1), test_buf); | ||
51 | ASSERT_INT_EQ(sshbuf_get_u8(p1, &c), 0); | ||
52 | ASSERT_PTR_EQ(sshbuf_ptr(p1), test_buf + 1); | ||
53 | ASSERT_U8_EQ(c, 1); | ||
54 | ASSERT_INT_EQ(sshbuf_get_u32(p1, &i), 0); | ||
55 | ASSERT_PTR_EQ(sshbuf_ptr(p1), test_buf + 5); | ||
56 | ASSERT_U32_EQ(i, 0x12345678); | ||
57 | ASSERT_INT_EQ(sshbuf_get_cstring(p1, &s, &l), 0); | ||
58 | ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0); | ||
59 | ASSERT_STRING_EQ(s, "hello"); | ||
60 | ASSERT_SIZE_T_EQ(l, 5); | ||
61 | sshbuf_free(p1); | ||
62 | free(s); | ||
63 | TEST_DONE(); | ||
64 | |||
65 | TEST_START("sshbuf_fromb "); | ||
66 | p1 = sshbuf_new(); | ||
67 | ASSERT_PTR_NE(p1, NULL); | ||
68 | ASSERT_U_INT_EQ(sshbuf_refcount(p1), 1); | ||
69 | ASSERT_PTR_EQ(sshbuf_parent(p1), NULL); | ||
70 | ASSERT_INT_EQ(sshbuf_put(p1, test_buf, sizeof(test_buf) - 1), 0); | ||
71 | p2 = sshbuf_fromb(p1); | ||
72 | ASSERT_PTR_NE(p2, NULL); | ||
73 | ASSERT_U_INT_EQ(sshbuf_refcount(p1), 2); | ||
74 | ASSERT_PTR_EQ(sshbuf_parent(p1), NULL); | ||
75 | ASSERT_PTR_EQ(sshbuf_parent(p2), p1); | ||
76 | ASSERT_PTR_EQ(sshbuf_ptr(p2), sshbuf_ptr(p1)); | ||
77 | ASSERT_PTR_NE(sshbuf_ptr(p1), NULL); | ||
78 | ASSERT_PTR_NE(sshbuf_ptr(p2), NULL); | ||
79 | ASSERT_PTR_EQ(sshbuf_mutable_ptr(p1), NULL); | ||
80 | ASSERT_PTR_EQ(sshbuf_mutable_ptr(p2), NULL); | ||
81 | ASSERT_SIZE_T_EQ(sshbuf_len(p1), sshbuf_len(p2)); | ||
82 | ASSERT_INT_EQ(sshbuf_get_u8(p2, &c), 0); | ||
83 | ASSERT_PTR_EQ(sshbuf_ptr(p2), sshbuf_ptr(p1) + 1); | ||
84 | ASSERT_U8_EQ(c, 1); | ||
85 | ASSERT_INT_EQ(sshbuf_get_u32(p2, &i), 0); | ||
86 | ASSERT_PTR_EQ(sshbuf_ptr(p2), sshbuf_ptr(p1) + 5); | ||
87 | ASSERT_U32_EQ(i, 0x12345678); | ||
88 | ASSERT_INT_EQ(sshbuf_get_cstring(p2, &s, &l), 0); | ||
89 | ASSERT_SIZE_T_EQ(sshbuf_len(p2), 0); | ||
90 | ASSERT_STRING_EQ(s, "hello"); | ||
91 | ASSERT_SIZE_T_EQ(l, 5); | ||
92 | sshbuf_free(p1); | ||
93 | ASSERT_U_INT_EQ(sshbuf_refcount(p1), 1); | ||
94 | sshbuf_free(p2); | ||
95 | free(s); | ||
96 | TEST_DONE(); | ||
97 | |||
98 | TEST_START("sshbuf_froms"); | ||
99 | p1 = sshbuf_new(); | ||
100 | ASSERT_PTR_NE(p1, NULL); | ||
101 | ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x01), 0); | ||
102 | ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x12345678), 0); | ||
103 | ASSERT_INT_EQ(sshbuf_put_cstring(p1, "hello"), 0); | ||
104 | p2 = sshbuf_new(); | ||
105 | ASSERT_PTR_NE(p2, NULL); | ||
106 | ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(test_buf) - 1); | ||
107 | ASSERT_INT_EQ(sshbuf_put_stringb(p2, p1), 0); | ||
108 | ASSERT_SIZE_T_EQ(sshbuf_len(p2), sizeof(test_buf) + 4 - 1); | ||
109 | ASSERT_INT_EQ(sshbuf_froms(p2, &p3), 0); | ||
110 | ASSERT_SIZE_T_EQ(sshbuf_len(p2), 0); | ||
111 | ASSERT_PTR_NE(p3, NULL); | ||
112 | ASSERT_PTR_NE(sshbuf_ptr(p3), NULL); | ||
113 | ASSERT_SIZE_T_EQ(sshbuf_len(p3), sizeof(test_buf) - 1); | ||
114 | ASSERT_MEM_EQ(sshbuf_ptr(p3), test_buf, sizeof(test_buf) - 1); | ||
115 | sshbuf_free(p3); | ||
116 | ASSERT_INT_EQ(sshbuf_put_stringb(p2, p1), 0); | ||
117 | ASSERT_INT_EQ(sshbuf_consume_end(p2, 1), 0); | ||
118 | ASSERT_INT_EQ(sshbuf_froms(p2, &p3), SSH_ERR_MESSAGE_INCOMPLETE); | ||
119 | ASSERT_PTR_EQ(p3, NULL); | ||
120 | sshbuf_free(p2); | ||
121 | sshbuf_free(p1); | ||
122 | } | ||