1*1323ec57SEd Maste /* $OpenBSD: test_sshbuf_getput_basic.c,v 1.3 2021/12/14 21:25:27 deraadt Exp $ */
2a0ee8cc6SDag-Erling Smørgrav /*
3a0ee8cc6SDag-Erling Smørgrav * Regress test for sshbuf.h buffer API
4a0ee8cc6SDag-Erling Smørgrav *
5a0ee8cc6SDag-Erling Smørgrav * Placed in the public domain
6a0ee8cc6SDag-Erling Smørgrav */
7a0ee8cc6SDag-Erling Smørgrav
8a0ee8cc6SDag-Erling Smørgrav #include "includes.h"
9a0ee8cc6SDag-Erling Smørgrav
10a0ee8cc6SDag-Erling Smørgrav #include <sys/types.h>
11a0ee8cc6SDag-Erling Smørgrav #include <stdio.h>
12a0ee8cc6SDag-Erling Smørgrav #ifdef HAVE_STDINT_H
13a0ee8cc6SDag-Erling Smørgrav # include <stdint.h>
14a0ee8cc6SDag-Erling Smørgrav #endif
15a0ee8cc6SDag-Erling Smørgrav #include <stdlib.h>
16a0ee8cc6SDag-Erling Smørgrav #include <string.h>
17a0ee8cc6SDag-Erling Smørgrav
18a0ee8cc6SDag-Erling Smørgrav #include "../test_helper/test_helper.h"
19a0ee8cc6SDag-Erling Smørgrav #include "ssherr.h"
20a0ee8cc6SDag-Erling Smørgrav #include "sshbuf.h"
21a0ee8cc6SDag-Erling Smørgrav
22a0ee8cc6SDag-Erling Smørgrav void sshbuf_getput_basic_tests(void);
23a0ee8cc6SDag-Erling Smørgrav
24a0ee8cc6SDag-Erling Smørgrav void
sshbuf_getput_basic_tests(void)25a0ee8cc6SDag-Erling Smørgrav sshbuf_getput_basic_tests(void)
26a0ee8cc6SDag-Erling Smørgrav {
27a0ee8cc6SDag-Erling Smørgrav struct sshbuf *p1, *p2;
28a0ee8cc6SDag-Erling Smørgrav const u_char *cd;
29a0ee8cc6SDag-Erling Smørgrav u_char *d, d2[32], x[] = {
30a0ee8cc6SDag-Erling Smørgrav 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x00, 0x99
31a0ee8cc6SDag-Erling Smørgrav };
32a0ee8cc6SDag-Erling Smørgrav u_int64_t v64;
33a0ee8cc6SDag-Erling Smørgrav u_int32_t v32;
34a0ee8cc6SDag-Erling Smørgrav u_int16_t v16;
35a0ee8cc6SDag-Erling Smørgrav u_char v8;
36a0ee8cc6SDag-Erling Smørgrav size_t s;
37a0ee8cc6SDag-Erling Smørgrav char *s2;
38a0ee8cc6SDag-Erling Smørgrav int r;
39a0ee8cc6SDag-Erling Smørgrav u_char bn1[] = { 0x00, 0x00, 0x00 };
40a0ee8cc6SDag-Erling Smørgrav u_char bn2[] = { 0x00, 0x00, 0x01, 0x02 };
41a0ee8cc6SDag-Erling Smørgrav u_char bn3[] = { 0x00, 0x80, 0x09 };
42a0ee8cc6SDag-Erling Smørgrav u_char bn_exp1[] = { 0x00, 0x00, 0x00, 0x00 };
43a0ee8cc6SDag-Erling Smørgrav u_char bn_exp2[] = { 0x00, 0x00, 0x00, 0x02, 0x01, 0x02 };
44a0ee8cc6SDag-Erling Smørgrav u_char bn_exp3[] = { 0x00, 0x00, 0x00, 0x03, 0x00, 0x80, 0x09 };
45a0ee8cc6SDag-Erling Smørgrav
46a0ee8cc6SDag-Erling Smørgrav TEST_START("PEEK_U64");
47a0ee8cc6SDag-Erling Smørgrav ASSERT_U64_EQ(PEEK_U64(x), 0x1122334455667788ULL);
48a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
49a0ee8cc6SDag-Erling Smørgrav
50a0ee8cc6SDag-Erling Smørgrav TEST_START("PEEK_U32");
51a0ee8cc6SDag-Erling Smørgrav ASSERT_U32_EQ(PEEK_U32(x), 0x11223344);
52a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
53a0ee8cc6SDag-Erling Smørgrav
54a0ee8cc6SDag-Erling Smørgrav TEST_START("PEEK_U16");
55a0ee8cc6SDag-Erling Smørgrav ASSERT_U16_EQ(PEEK_U16(x), 0x1122);
56a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
57a0ee8cc6SDag-Erling Smørgrav
58a0ee8cc6SDag-Erling Smørgrav TEST_START("POKE_U64");
59a0ee8cc6SDag-Erling Smørgrav bzero(d2, sizeof(d2));
60a0ee8cc6SDag-Erling Smørgrav POKE_U64(d2, 0x1122334455667788ULL);
61a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(d2, x, 8);
62a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
63a0ee8cc6SDag-Erling Smørgrav
64a0ee8cc6SDag-Erling Smørgrav TEST_START("POKE_U32");
65a0ee8cc6SDag-Erling Smørgrav bzero(d2, sizeof(d2));
66a0ee8cc6SDag-Erling Smørgrav POKE_U32(d2, 0x11223344);
67a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(d2, x, 4);
68a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
69a0ee8cc6SDag-Erling Smørgrav
70a0ee8cc6SDag-Erling Smørgrav TEST_START("POKE_U16");
71a0ee8cc6SDag-Erling Smørgrav bzero(d2, sizeof(d2));
72a0ee8cc6SDag-Erling Smørgrav POKE_U16(d2, 0x1122);
73a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(d2, x, 2);
74a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
75a0ee8cc6SDag-Erling Smørgrav
76a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put");
77a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
78a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
79a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, x, 5), 0);
80a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 5);
81a0ee8cc6SDag-Erling Smørgrav cd = sshbuf_ptr(p1);
82a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(cd, NULL);
83a0ee8cc6SDag-Erling Smørgrav ASSERT_U8_EQ(cd[0], 0x11);
84a0ee8cc6SDag-Erling Smørgrav ASSERT_U8_EQ(cd[1], 0x22);
85a0ee8cc6SDag-Erling Smørgrav ASSERT_U8_EQ(cd[2], 0x33);
86a0ee8cc6SDag-Erling Smørgrav ASSERT_U8_EQ(cd[3], 0x44);
87a0ee8cc6SDag-Erling Smørgrav ASSERT_U8_EQ(cd[4], 0x55);
88a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
89a0ee8cc6SDag-Erling Smørgrav
90a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get");
91a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get(p1, d2, 4), 0);
92a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(d2, x, 4);
93a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
94a0ee8cc6SDag-Erling Smørgrav ASSERT_U8_EQ(*(sshbuf_ptr(p1)), 0x55);
95a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
96a0ee8cc6SDag-Erling Smørgrav
97a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get truncated");
98a0ee8cc6SDag-Erling Smørgrav r = sshbuf_get(p1, d2, 4);
99a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
100a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
101a0ee8cc6SDag-Erling Smørgrav ASSERT_U8_EQ(*(sshbuf_ptr(p1)), 0x55);
102a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
103a0ee8cc6SDag-Erling Smørgrav
104a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put truncated");
105a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_set_max_size(p1, 4), 0);
106a0ee8cc6SDag-Erling Smørgrav r = sshbuf_put(p1, x, 5);
107a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
108a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
109a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
110a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
111a0ee8cc6SDag-Erling Smørgrav
112a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_u64");
113a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
114a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
115a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, x, 10), 0);
116a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 10);
117a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get_u64(p1, &v64), 0);
118a0ee8cc6SDag-Erling Smørgrav ASSERT_U64_EQ(v64, 0x1122334455667788ULL);
119a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
120a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
121a0ee8cc6SDag-Erling Smørgrav
122a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_u64 truncated");
123a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
124a0ee8cc6SDag-Erling Smørgrav r = sshbuf_get_u64(p1, &v64);
125a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
126a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
127a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
128a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
129a0ee8cc6SDag-Erling Smørgrav
130a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_u32");
131a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
132a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
133a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, x, 10), 0);
134a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 10);
135a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get_u32(p1, &v32), 0);
136a0ee8cc6SDag-Erling Smørgrav ASSERT_U32_EQ(v32, 0x11223344);
137a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 6);
138a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get_u32(p1, &v32), 0);
139a0ee8cc6SDag-Erling Smørgrav ASSERT_U32_EQ(v32, 0x55667788);
140a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
141a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
142a0ee8cc6SDag-Erling Smørgrav
143a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_u32 truncated");
144a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
145a0ee8cc6SDag-Erling Smørgrav r = sshbuf_get_u32(p1, &v32);
146a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
147a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
148a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
149a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
150a0ee8cc6SDag-Erling Smørgrav
151a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_u16");
152a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
153a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
154a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, x, 9), 0);
155a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 9);
156a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get_u16(p1, &v16), 0);
157a0ee8cc6SDag-Erling Smørgrav ASSERT_U16_EQ(v16, 0x1122);
158a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 7);
159a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get_u16(p1, &v16), 0);
160a0ee8cc6SDag-Erling Smørgrav ASSERT_U16_EQ(v16, 0x3344);
161a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 5);
162a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get_u16(p1, &v16), 0);
163a0ee8cc6SDag-Erling Smørgrav ASSERT_U16_EQ(v16, 0x5566);
164a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 3);
165a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get_u16(p1, &v16), 0);
166a0ee8cc6SDag-Erling Smørgrav ASSERT_U16_EQ(v16, 0x7788);
167a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
168a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
169a0ee8cc6SDag-Erling Smørgrav
170a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_u16 truncated");
171a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
172a0ee8cc6SDag-Erling Smørgrav r = sshbuf_get_u16(p1, &v16);
173a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
174a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
175a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
176a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
177a0ee8cc6SDag-Erling Smørgrav
178a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_u8");
179a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
180a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
181a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, x, 2), 0);
182a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
183a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get_u8(p1, &v8), 0);
184a0ee8cc6SDag-Erling Smørgrav ASSERT_U8_EQ(v8, 0x11);
185a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
186a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get_u8(p1, &v8), 0);
187a0ee8cc6SDag-Erling Smørgrav ASSERT_U8_EQ(v8, 0x22);
188a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
189a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
190a0ee8cc6SDag-Erling Smørgrav
191a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_u8 truncated");
192a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
193a0ee8cc6SDag-Erling Smørgrav r = sshbuf_get_u8(p1, &v8);
194a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
195a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
196a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
197a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
198a0ee8cc6SDag-Erling Smørgrav
199a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_u64");
200a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
201a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
202a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u64(p1, 0x1122334455667788ULL), 0);
203a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 8);
204a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 8);
205a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
206a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
207a0ee8cc6SDag-Erling Smørgrav
208a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_u64 exact");
209a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
210a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
211a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_set_max_size(p1, 8), 0);
212a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u64(p1, 0x1122334455667788ULL), 0);
213a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 8);
214a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 8);
215a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
216a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
217a0ee8cc6SDag-Erling Smørgrav
218a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_u64 limited");
219a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
220a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
221a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_set_max_size(p1, 7), 0);
222a0ee8cc6SDag-Erling Smørgrav r = sshbuf_put_u64(p1, 0x1122334455667788ULL);
223a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
224a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
225a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
226a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
227a0ee8cc6SDag-Erling Smørgrav
228a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_u32");
229a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
230a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
231a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x11223344), 0);
232a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
233a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 4);
234a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
235a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
236a0ee8cc6SDag-Erling Smørgrav
237a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_u32 exact");
238a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
239a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
240a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_set_max_size(p1, 4), 0);
241a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x11223344), 0);
242a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
243a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 4);
244a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
245a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
246a0ee8cc6SDag-Erling Smørgrav
247a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_u32 limited");
248a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
249a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
250a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_set_max_size(p1, 3), 0);
251a0ee8cc6SDag-Erling Smørgrav r = sshbuf_put_u32(p1, 0x11223344);
252a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
253a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
254a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
255a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
256a0ee8cc6SDag-Erling Smørgrav
257a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_u16");
258a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
259a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
260a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u16(p1, 0x1122), 0);
261a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
262a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 2);
263a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
264a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
265a0ee8cc6SDag-Erling Smørgrav
266a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_u16");
267a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
268a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
269a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_set_max_size(p1, 2), 0);
270a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u16(p1, 0x1122), 0);
271a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
272a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 2);
273a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
274a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
275a0ee8cc6SDag-Erling Smørgrav
276a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_u16 limited");
277a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
278a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
279a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_set_max_size(p1, 1), 0);
280a0ee8cc6SDag-Erling Smørgrav r = sshbuf_put_u16(p1, 0x1122);
281a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
282a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
283a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
284a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
285a0ee8cc6SDag-Erling Smørgrav
286a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_string");
287a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
288a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
289a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
290a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
291a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
292a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4 + 4);
293a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get_string(p1, &d, &s), 0);
294a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(s, sizeof(x));
295a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(d, x, sizeof(x));
296a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
297a0ee8cc6SDag-Erling Smørgrav free(d);
298a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
299a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
300a0ee8cc6SDag-Erling Smørgrav
301a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_string exact");
302a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
303a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
304a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(x) + 4), 0);
305a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
306a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
307a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
308a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get_string(p1, &d, &s), 0);
309a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(s, sizeof(x));
310a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(d, x, sizeof(x));
311a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
312a0ee8cc6SDag-Erling Smørgrav free(d);
313a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
314a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
315a0ee8cc6SDag-Erling Smørgrav
316a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_string truncated");
317a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
318a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
319a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
320a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
321a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
322a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_consume_end(p1, 1), 0);
323a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 3);
324a0ee8cc6SDag-Erling Smørgrav r = sshbuf_get_string(p1, &d, &s);
325a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
326a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 3);
327a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
328a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
329a0ee8cc6SDag-Erling Smørgrav
330a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_string giant");
331a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
332a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
333a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u32(p1, 0xffffffff), 0);
334a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
335a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
336a0ee8cc6SDag-Erling Smørgrav r = sshbuf_get_string(p1, &d, &s);
337a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_STRING_TOO_LARGE);
338a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
339a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
340a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
341a0ee8cc6SDag-Erling Smørgrav
342a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_cstring giant");
343a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
344a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
345a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u32(p1, 0xffffffff), 0);
346a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
347a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
348a0ee8cc6SDag-Erling Smørgrav r = sshbuf_get_cstring(p1, &s2, &s);
349a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_STRING_TOO_LARGE);
350a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
351a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
352a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
353a0ee8cc6SDag-Erling Smørgrav
354a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_cstring embedded \\0");
355a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
356a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
357a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
358a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
359a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
360a0ee8cc6SDag-Erling Smørgrav r = sshbuf_get_cstring(p1, &s2, NULL);
361a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_INVALID_FORMAT);
362a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
363a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
364a0ee8cc6SDag-Erling Smørgrav
365a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_get_cstring trailing \\0");
366a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
367a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
368a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x) - 1), 0);
369a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x) - 1), 0);
370a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4 - 1);
371a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_get_cstring(p1, &s2, &s), 0);
372a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(s, sizeof(x) - 1);
373a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(s2, x, s);
374a0ee8cc6SDag-Erling Smørgrav free(s2);
375a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
376a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
377a0ee8cc6SDag-Erling Smørgrav
378a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_string");
379a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
380a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
381a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_string(p1, x, sizeof(x)), 0);
382a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
383a0ee8cc6SDag-Erling Smørgrav ASSERT_U32_EQ(PEEK_U32(sshbuf_ptr(p1)), sizeof(x));
384a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1) + 4, x, sizeof(x));
385a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
386a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
387a0ee8cc6SDag-Erling Smørgrav
388a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_string limited");
389a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
390a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
391a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(x) + 4 - 1), 0);
392a0ee8cc6SDag-Erling Smørgrav r = sshbuf_put_string(p1, x, sizeof(x));
393a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
394a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
395a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
396a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
397a0ee8cc6SDag-Erling Smørgrav
398a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_string giant");
399a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
400a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
401a0ee8cc6SDag-Erling Smørgrav r = sshbuf_put_string(p1, (void *)0x01, 0xfffffffc);
402a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
403a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
404a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
405a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
406a0ee8cc6SDag-Erling Smørgrav
407a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_putf");
408a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
409a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
410a0ee8cc6SDag-Erling Smørgrav r = sshbuf_putf(p1, "%s %d %x", "hello", 23, 0x5f);
411a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(r, 0);
412a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), 11);
413a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), "hello 23 5f", 11);
414a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
415a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
416a0ee8cc6SDag-Erling Smørgrav
417a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_putb");
418a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
419a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
420a0ee8cc6SDag-Erling Smørgrav p2 = sshbuf_new();
421a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p2, NULL);
422a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put(p1, "blahblahblah", 12), 0);
423a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_putb(p2, p1), 0);
424a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
425a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p2), 12);
426a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p2), "blahblahblah", 12);
427a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p2);
428a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
429a0ee8cc6SDag-Erling Smørgrav
430a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_bignum2_bytes empty buf");
431a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
432a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
433a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, NULL, 0), 0);
434a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp1));
435a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp1, sizeof(bn_exp1));
436a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
437a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
438a0ee8cc6SDag-Erling Smørgrav
439a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_bignum2_bytes all zeroes");
440a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
441a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
442a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn1, sizeof(bn1)), 0);
443a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp1));
444a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp1, sizeof(bn_exp1));
445a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
446a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
447a0ee8cc6SDag-Erling Smørgrav
448a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_bignum2_bytes simple");
449a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
450a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
451a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn2+2, sizeof(bn2)-2), 0);
452a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp2));
453a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp2, sizeof(bn_exp2));
454a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
455a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
456a0ee8cc6SDag-Erling Smørgrav
457a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_bignum2_bytes leading zero");
458a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
459a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
460a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn2, sizeof(bn2)), 0);
461a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp2));
462a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp2, sizeof(bn_exp2));
463a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
464a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
465a0ee8cc6SDag-Erling Smørgrav
466a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_bignum2_bytes neg");
467a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
468a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
469a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn3+1, sizeof(bn3)-1), 0);
470a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp3));
471a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp3, sizeof(bn_exp3));
472a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
473a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
474a0ee8cc6SDag-Erling Smørgrav
475a0ee8cc6SDag-Erling Smørgrav TEST_START("sshbuf_put_bignum2_bytes neg and leading zero");
476a0ee8cc6SDag-Erling Smørgrav p1 = sshbuf_new();
477a0ee8cc6SDag-Erling Smørgrav ASSERT_PTR_NE(p1, NULL);
478a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn3, sizeof(bn3)), 0);
479a0ee8cc6SDag-Erling Smørgrav ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp3));
480a0ee8cc6SDag-Erling Smørgrav ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp3, sizeof(bn_exp3));
481a0ee8cc6SDag-Erling Smørgrav sshbuf_free(p1);
482a0ee8cc6SDag-Erling Smørgrav TEST_DONE();
48319261079SEd Maste
48419261079SEd Maste TEST_START("sshbuf_peek_u64");
48519261079SEd Maste p1 = sshbuf_new();
48619261079SEd Maste ASSERT_PTR_NE(p1, NULL);
48719261079SEd Maste ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
48819261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u64(p1, 0, &v64), 0);
48919261079SEd Maste ASSERT_U64_EQ(v64, 0x1122334455667788ULL);
49019261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u64(p1, 2, &v64), 0);
49119261079SEd Maste ASSERT_U64_EQ(v64, 0x3344556677880099ULL);
49219261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u64(p1, 3, &v64), SSH_ERR_MESSAGE_INCOMPLETE);
49319261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u64(p1, sizeof(x), &v64),
49419261079SEd Maste SSH_ERR_MESSAGE_INCOMPLETE);
49519261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u64(p1, 1000, &v64),
49619261079SEd Maste SSH_ERR_MESSAGE_INCOMPLETE);
49719261079SEd Maste sshbuf_free(p1);
49819261079SEd Maste TEST_DONE();
49919261079SEd Maste
50019261079SEd Maste TEST_START("sshbuf_peek_u32");
50119261079SEd Maste p1 = sshbuf_new();
50219261079SEd Maste ASSERT_PTR_NE(p1, NULL);
50319261079SEd Maste ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
50419261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u32(p1, 0, &v32), 0);
50519261079SEd Maste ASSERT_U32_EQ(v32, 0x11223344);
50619261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u32(p1, 6, &v32), 0);
50719261079SEd Maste ASSERT_U32_EQ(v32, 0x77880099);
50819261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u32(p1, 7, &v32), SSH_ERR_MESSAGE_INCOMPLETE);
50919261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u32(p1, sizeof(x), &v32),
51019261079SEd Maste SSH_ERR_MESSAGE_INCOMPLETE);
51119261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u32(p1, 1000, &v32),
51219261079SEd Maste SSH_ERR_MESSAGE_INCOMPLETE);
51319261079SEd Maste sshbuf_free(p1);
51419261079SEd Maste TEST_DONE();
51519261079SEd Maste
51619261079SEd Maste TEST_START("sshbuf_peek_u16");
51719261079SEd Maste p1 = sshbuf_new();
51819261079SEd Maste ASSERT_PTR_NE(p1, NULL);
51919261079SEd Maste ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
52019261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u16(p1, 0, &v16), 0);
52119261079SEd Maste ASSERT_U16_EQ(v16, 0x1122);
52219261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u16(p1, 8, &v16), 0);
52319261079SEd Maste ASSERT_U16_EQ(v16, 0x99);
52419261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u16(p1, 9, &v16), SSH_ERR_MESSAGE_INCOMPLETE);
52519261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u16(p1, sizeof(x), &v16),
52619261079SEd Maste SSH_ERR_MESSAGE_INCOMPLETE);
52719261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u16(p1, 1000, &v16),
52819261079SEd Maste SSH_ERR_MESSAGE_INCOMPLETE);
52919261079SEd Maste sshbuf_free(p1);
53019261079SEd Maste TEST_DONE();
53119261079SEd Maste
53219261079SEd Maste TEST_START("sshbuf_peek_u8");
53319261079SEd Maste p1 = sshbuf_new();
53419261079SEd Maste ASSERT_PTR_NE(p1, NULL);
53519261079SEd Maste ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
53619261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u8(p1, 0, &v8), 0);
53719261079SEd Maste ASSERT_U8_EQ(v8, 0x11);
53819261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u8(p1, 9, &v8), 0);
53919261079SEd Maste ASSERT_U8_EQ(v8, 0x99);
54019261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u8(p1, sizeof(x), &v8),
54119261079SEd Maste SSH_ERR_MESSAGE_INCOMPLETE);
54219261079SEd Maste ASSERT_INT_EQ(sshbuf_peek_u8(p1, 1000, &v8),
54319261079SEd Maste SSH_ERR_MESSAGE_INCOMPLETE);
54419261079SEd Maste sshbuf_free(p1);
54519261079SEd Maste TEST_DONE();
54619261079SEd Maste
54719261079SEd Maste TEST_START("sshbuf_poke_u64");
54819261079SEd Maste p1 = sshbuf_new();
54919261079SEd Maste ASSERT_PTR_NE(p1, NULL);
55019261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
55119261079SEd Maste /* poke at start of buffer */
55219261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u64(p1, 0, 0xa1b2c3d4e5f60718ULL), 0);
55319261079SEd Maste s2 = sshbuf_dtob16(p1);
55419261079SEd Maste ASSERT_PTR_NE(s2, NULL);
55519261079SEd Maste ASSERT_STRING_EQ(s2, "a1b2c3d4e5f607180000");
55619261079SEd Maste free(s2);
55719261079SEd Maste sshbuf_reset(p1);
55819261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
55919261079SEd Maste /* poke aligned with end of buffer */
56019261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u64(p1, 2, 0xa1b2c3d4e5f60718ULL), 0);
56119261079SEd Maste s2 = sshbuf_dtob16(p1);
56219261079SEd Maste ASSERT_PTR_NE(s2, NULL);
56319261079SEd Maste ASSERT_STRING_EQ(s2, "0000a1b2c3d4e5f60718");
56419261079SEd Maste free(s2);
56519261079SEd Maste sshbuf_reset(p1);
56619261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
56719261079SEd Maste /* poke past end of buffer */
56819261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u64(p1, 3, 0xa1b2c3d4e5f60718ULL),
56919261079SEd Maste SSH_ERR_NO_BUFFER_SPACE);
57019261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u64(p1, 10, 0xa1b2c3d4e5f60718ULL),
57119261079SEd Maste SSH_ERR_NO_BUFFER_SPACE);
57219261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u64(p1, 1000, 0xa1b2c3d4e5f60718ULL),
57319261079SEd Maste SSH_ERR_NO_BUFFER_SPACE);
57419261079SEd Maste /* ensure failed pokes do not modify buffer */
57519261079SEd Maste s2 = sshbuf_dtob16(p1);
57619261079SEd Maste ASSERT_PTR_NE(s2, NULL);
57719261079SEd Maste ASSERT_STRING_EQ(s2, "00000000000000000000");
57819261079SEd Maste sshbuf_free(p1);
57919261079SEd Maste TEST_DONE();
58019261079SEd Maste
58119261079SEd Maste TEST_START("sshbuf_poke_u32");
58219261079SEd Maste p1 = sshbuf_new();
58319261079SEd Maste ASSERT_PTR_NE(p1, NULL);
58419261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
58519261079SEd Maste /* poke at start of buffer */
58619261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u32(p1, 0, 0xa1b2c3d4), 0);
58719261079SEd Maste s2 = sshbuf_dtob16(p1);
58819261079SEd Maste ASSERT_PTR_NE(s2, NULL);
58919261079SEd Maste ASSERT_STRING_EQ(s2, "a1b2c3d4000000000000");
59019261079SEd Maste free(s2);
59119261079SEd Maste sshbuf_reset(p1);
59219261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
59319261079SEd Maste /* poke aligned with end of buffer */
59419261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u32(p1, 6, 0xa1b2c3d4), 0);
59519261079SEd Maste s2 = sshbuf_dtob16(p1);
59619261079SEd Maste ASSERT_PTR_NE(s2, NULL);
59719261079SEd Maste ASSERT_STRING_EQ(s2, "000000000000a1b2c3d4");
59819261079SEd Maste free(s2);
59919261079SEd Maste sshbuf_reset(p1);
60019261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
60119261079SEd Maste /* poke past end of buffer */
60219261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u32(p1, 7, 0xa1b2c3d4),
60319261079SEd Maste SSH_ERR_NO_BUFFER_SPACE);
60419261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u32(p1, 10, 0xa1b2c3d4),
60519261079SEd Maste SSH_ERR_NO_BUFFER_SPACE);
60619261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u32(p1, 1000, 0xa1b2c3d4),
60719261079SEd Maste SSH_ERR_NO_BUFFER_SPACE);
60819261079SEd Maste /* ensure failed pokes do not modify buffer */
60919261079SEd Maste s2 = sshbuf_dtob16(p1);
61019261079SEd Maste ASSERT_PTR_NE(s2, NULL);
61119261079SEd Maste ASSERT_STRING_EQ(s2, "00000000000000000000");
61219261079SEd Maste sshbuf_free(p1);
61319261079SEd Maste TEST_DONE();
61419261079SEd Maste
61519261079SEd Maste TEST_START("sshbuf_poke_u16");
61619261079SEd Maste p1 = sshbuf_new();
61719261079SEd Maste ASSERT_PTR_NE(p1, NULL);
61819261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
61919261079SEd Maste /* poke at start of buffer */
62019261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u16(p1, 0, 0xa1b2), 0);
62119261079SEd Maste s2 = sshbuf_dtob16(p1);
62219261079SEd Maste ASSERT_PTR_NE(s2, NULL);
62319261079SEd Maste ASSERT_STRING_EQ(s2, "a1b20000000000000000");
62419261079SEd Maste free(s2);
62519261079SEd Maste sshbuf_reset(p1);
62619261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
62719261079SEd Maste /* poke aligned with end of buffer */
62819261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u16(p1, 8, 0xa1b2), 0);
62919261079SEd Maste s2 = sshbuf_dtob16(p1);
63019261079SEd Maste ASSERT_PTR_NE(s2, NULL);
63119261079SEd Maste ASSERT_STRING_EQ(s2, "0000000000000000a1b2");
63219261079SEd Maste free(s2);
63319261079SEd Maste sshbuf_reset(p1);
63419261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
63519261079SEd Maste /* poke past end of buffer */
63619261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u16(p1, 9, 0xa1b2),
63719261079SEd Maste SSH_ERR_NO_BUFFER_SPACE);
63819261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u16(p1, 10, 0xa1b2),
63919261079SEd Maste SSH_ERR_NO_BUFFER_SPACE);
64019261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u16(p1, 1000, 0xa1b2),
64119261079SEd Maste SSH_ERR_NO_BUFFER_SPACE);
64219261079SEd Maste /* ensure failed pokes do not modify buffer */
64319261079SEd Maste s2 = sshbuf_dtob16(p1);
64419261079SEd Maste ASSERT_PTR_NE(s2, NULL);
64519261079SEd Maste ASSERT_STRING_EQ(s2, "00000000000000000000");
64619261079SEd Maste sshbuf_free(p1);
64719261079SEd Maste TEST_DONE();
64819261079SEd Maste
64919261079SEd Maste TEST_START("sshbuf_poke_u8");
65019261079SEd Maste p1 = sshbuf_new();
65119261079SEd Maste ASSERT_PTR_NE(p1, NULL);
65219261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
65319261079SEd Maste /* poke at start of buffer */
65419261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u8(p1, 0, 0xa1), 0);
65519261079SEd Maste s2 = sshbuf_dtob16(p1);
65619261079SEd Maste ASSERT_PTR_NE(s2, NULL);
65719261079SEd Maste ASSERT_STRING_EQ(s2, "a1000000000000000000");
65819261079SEd Maste free(s2);
65919261079SEd Maste sshbuf_reset(p1);
66019261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
66119261079SEd Maste /* poke aligned with end of buffer */
66219261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u8(p1, 9, 0xa1), 0);
66319261079SEd Maste s2 = sshbuf_dtob16(p1);
66419261079SEd Maste ASSERT_PTR_NE(s2, NULL);
66519261079SEd Maste ASSERT_STRING_EQ(s2, "000000000000000000a1");
66619261079SEd Maste free(s2);
66719261079SEd Maste sshbuf_reset(p1);
66819261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
66919261079SEd Maste /* poke past end of buffer */
67019261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u8(p1, 10, 0xa1), SSH_ERR_NO_BUFFER_SPACE);
67119261079SEd Maste ASSERT_INT_EQ(sshbuf_poke_u8(p1, 1000, 0xa1), SSH_ERR_NO_BUFFER_SPACE);
67219261079SEd Maste /* ensure failed pokes do not modify buffer */
67319261079SEd Maste s2 = sshbuf_dtob16(p1);
67419261079SEd Maste ASSERT_PTR_NE(s2, NULL);
67519261079SEd Maste ASSERT_STRING_EQ(s2, "00000000000000000000");
67619261079SEd Maste sshbuf_free(p1);
67719261079SEd Maste TEST_DONE();
67819261079SEd Maste
67919261079SEd Maste TEST_START("sshbuf_poke");
68019261079SEd Maste p1 = sshbuf_new();
68119261079SEd Maste ASSERT_PTR_NE(p1, NULL);
68219261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
68319261079SEd Maste /* poke at start of buffer */
68419261079SEd Maste ASSERT_INT_EQ(sshbuf_poke(p1, 0, "hello!", 6), 0);
68519261079SEd Maste s2 = sshbuf_dtob16(p1);
68619261079SEd Maste ASSERT_PTR_NE(s2, NULL);
68719261079SEd Maste ASSERT_STRING_EQ(s2, "68656c6c6f2100000000");
68819261079SEd Maste free(s2);
68919261079SEd Maste sshbuf_reset(p1);
69019261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
69119261079SEd Maste /* poke aligned with end of buffer */
69219261079SEd Maste ASSERT_INT_EQ(sshbuf_poke(p1, 4, "hello!", 6), 0);
69319261079SEd Maste s2 = sshbuf_dtob16(p1);
69419261079SEd Maste ASSERT_PTR_NE(s2, NULL);
69519261079SEd Maste ASSERT_STRING_EQ(s2, "0000000068656c6c6f21");
69619261079SEd Maste free(s2);
69719261079SEd Maste sshbuf_reset(p1);
69819261079SEd Maste ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
69919261079SEd Maste /* poke past end of buffer */
70019261079SEd Maste ASSERT_INT_EQ(sshbuf_poke(p1, 7, "hello!", 6),
70119261079SEd Maste SSH_ERR_NO_BUFFER_SPACE);
70219261079SEd Maste ASSERT_INT_EQ(sshbuf_poke(p1, 10, "hello!", 6),
70319261079SEd Maste SSH_ERR_NO_BUFFER_SPACE);
70419261079SEd Maste ASSERT_INT_EQ(sshbuf_poke(p1, 1000, "hello!", 6),
70519261079SEd Maste SSH_ERR_NO_BUFFER_SPACE);
70619261079SEd Maste /* ensure failed pokes do not modify buffer */
70719261079SEd Maste s2 = sshbuf_dtob16(p1);
70819261079SEd Maste ASSERT_PTR_NE(s2, NULL);
70919261079SEd Maste ASSERT_STRING_EQ(s2, "00000000000000000000");
71019261079SEd Maste sshbuf_free(p1);
71119261079SEd Maste TEST_DONE();
712a0ee8cc6SDag-Erling Smørgrav }
713