1 /* $OpenBSD: test_sshbuf_getput_basic.c,v 1.3 2021/12/14 21:25:27 deraadt 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 <stdio.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "test_helper.h"
15 #include "ssherr.h"
16 #include "sshbuf.h"
17
18 void sshbuf_getput_basic_tests(void);
19
20 void
sshbuf_getput_basic_tests(void)21 sshbuf_getput_basic_tests(void)
22 {
23 struct sshbuf *p1, *p2;
24 const u_char *cd;
25 u_char *d, d2[32], x[] = {
26 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x00, 0x99
27 };
28 u_int64_t v64;
29 u_int32_t v32;
30 u_int16_t v16;
31 u_char v8;
32 size_t s;
33 char *s2;
34 int r;
35 u_char bn1[] = { 0x00, 0x00, 0x00 };
36 u_char bn2[] = { 0x00, 0x00, 0x01, 0x02 };
37 u_char bn3[] = { 0x00, 0x80, 0x09 };
38 u_char bn_exp1[] = { 0x00, 0x00, 0x00, 0x00 };
39 u_char bn_exp2[] = { 0x00, 0x00, 0x00, 0x02, 0x01, 0x02 };
40 u_char bn_exp3[] = { 0x00, 0x00, 0x00, 0x03, 0x00, 0x80, 0x09 };
41
42 TEST_START("PEEK_U64");
43 ASSERT_U64_EQ(PEEK_U64(x), 0x1122334455667788ULL);
44 TEST_DONE();
45
46 TEST_START("PEEK_U32");
47 ASSERT_U32_EQ(PEEK_U32(x), 0x11223344);
48 TEST_DONE();
49
50 TEST_START("PEEK_U16");
51 ASSERT_U16_EQ(PEEK_U16(x), 0x1122);
52 TEST_DONE();
53
54 TEST_START("POKE_U64");
55 bzero(d2, sizeof(d2));
56 POKE_U64(d2, 0x1122334455667788ULL);
57 ASSERT_MEM_EQ(d2, x, 8);
58 TEST_DONE();
59
60 TEST_START("POKE_U32");
61 bzero(d2, sizeof(d2));
62 POKE_U32(d2, 0x11223344);
63 ASSERT_MEM_EQ(d2, x, 4);
64 TEST_DONE();
65
66 TEST_START("POKE_U16");
67 bzero(d2, sizeof(d2));
68 POKE_U16(d2, 0x1122);
69 ASSERT_MEM_EQ(d2, x, 2);
70 TEST_DONE();
71
72 TEST_START("sshbuf_put");
73 p1 = sshbuf_new();
74 ASSERT_PTR_NE(p1, NULL);
75 ASSERT_INT_EQ(sshbuf_put(p1, x, 5), 0);
76 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 5);
77 cd = sshbuf_ptr(p1);
78 ASSERT_PTR_NE(cd, NULL);
79 ASSERT_U8_EQ(cd[0], 0x11);
80 ASSERT_U8_EQ(cd[1], 0x22);
81 ASSERT_U8_EQ(cd[2], 0x33);
82 ASSERT_U8_EQ(cd[3], 0x44);
83 ASSERT_U8_EQ(cd[4], 0x55);
84 TEST_DONE();
85
86 TEST_START("sshbuf_get");
87 ASSERT_INT_EQ(sshbuf_get(p1, d2, 4), 0);
88 ASSERT_MEM_EQ(d2, x, 4);
89 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
90 ASSERT_U8_EQ(*(sshbuf_ptr(p1)), 0x55);
91 TEST_DONE();
92
93 TEST_START("sshbuf_get truncated");
94 r = sshbuf_get(p1, d2, 4);
95 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
96 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
97 ASSERT_U8_EQ(*(sshbuf_ptr(p1)), 0x55);
98 TEST_DONE();
99
100 TEST_START("sshbuf_put truncated");
101 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 4), 0);
102 r = sshbuf_put(p1, x, 5);
103 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
104 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
105 sshbuf_free(p1);
106 TEST_DONE();
107
108 TEST_START("sshbuf_get_u64");
109 p1 = sshbuf_new();
110 ASSERT_PTR_NE(p1, NULL);
111 ASSERT_INT_EQ(sshbuf_put(p1, x, 10), 0);
112 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 10);
113 ASSERT_INT_EQ(sshbuf_get_u64(p1, &v64), 0);
114 ASSERT_U64_EQ(v64, 0x1122334455667788ULL);
115 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
116 TEST_DONE();
117
118 TEST_START("sshbuf_get_u64 truncated");
119 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
120 r = sshbuf_get_u64(p1, &v64);
121 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
122 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
123 sshbuf_free(p1);
124 TEST_DONE();
125
126 TEST_START("sshbuf_get_u32");
127 p1 = sshbuf_new();
128 ASSERT_PTR_NE(p1, NULL);
129 ASSERT_INT_EQ(sshbuf_put(p1, x, 10), 0);
130 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 10);
131 ASSERT_INT_EQ(sshbuf_get_u32(p1, &v32), 0);
132 ASSERT_U32_EQ(v32, 0x11223344);
133 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 6);
134 ASSERT_INT_EQ(sshbuf_get_u32(p1, &v32), 0);
135 ASSERT_U32_EQ(v32, 0x55667788);
136 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
137 TEST_DONE();
138
139 TEST_START("sshbuf_get_u32 truncated");
140 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
141 r = sshbuf_get_u32(p1, &v32);
142 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
143 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
144 sshbuf_free(p1);
145 TEST_DONE();
146
147 TEST_START("sshbuf_get_u16");
148 p1 = sshbuf_new();
149 ASSERT_PTR_NE(p1, NULL);
150 ASSERT_INT_EQ(sshbuf_put(p1, x, 9), 0);
151 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 9);
152 ASSERT_INT_EQ(sshbuf_get_u16(p1, &v16), 0);
153 ASSERT_U16_EQ(v16, 0x1122);
154 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 7);
155 ASSERT_INT_EQ(sshbuf_get_u16(p1, &v16), 0);
156 ASSERT_U16_EQ(v16, 0x3344);
157 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 5);
158 ASSERT_INT_EQ(sshbuf_get_u16(p1, &v16), 0);
159 ASSERT_U16_EQ(v16, 0x5566);
160 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 3);
161 ASSERT_INT_EQ(sshbuf_get_u16(p1, &v16), 0);
162 ASSERT_U16_EQ(v16, 0x7788);
163 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
164 TEST_DONE();
165
166 TEST_START("sshbuf_get_u16 truncated");
167 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
168 r = sshbuf_get_u16(p1, &v16);
169 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
170 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
171 sshbuf_free(p1);
172 TEST_DONE();
173
174 TEST_START("sshbuf_get_u8");
175 p1 = sshbuf_new();
176 ASSERT_PTR_NE(p1, NULL);
177 ASSERT_INT_EQ(sshbuf_put(p1, x, 2), 0);
178 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
179 ASSERT_INT_EQ(sshbuf_get_u8(p1, &v8), 0);
180 ASSERT_U8_EQ(v8, 0x11);
181 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
182 ASSERT_INT_EQ(sshbuf_get_u8(p1, &v8), 0);
183 ASSERT_U8_EQ(v8, 0x22);
184 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
185 TEST_DONE();
186
187 TEST_START("sshbuf_get_u8 truncated");
188 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
189 r = sshbuf_get_u8(p1, &v8);
190 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
191 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
192 sshbuf_free(p1);
193 TEST_DONE();
194
195 TEST_START("sshbuf_put_u64");
196 p1 = sshbuf_new();
197 ASSERT_PTR_NE(p1, NULL);
198 ASSERT_INT_EQ(sshbuf_put_u64(p1, 0x1122334455667788ULL), 0);
199 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 8);
200 ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 8);
201 sshbuf_free(p1);
202 TEST_DONE();
203
204 TEST_START("sshbuf_put_u64 exact");
205 p1 = sshbuf_new();
206 ASSERT_PTR_NE(p1, NULL);
207 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 8), 0);
208 ASSERT_INT_EQ(sshbuf_put_u64(p1, 0x1122334455667788ULL), 0);
209 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 8);
210 ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 8);
211 sshbuf_free(p1);
212 TEST_DONE();
213
214 TEST_START("sshbuf_put_u64 limited");
215 p1 = sshbuf_new();
216 ASSERT_PTR_NE(p1, NULL);
217 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 7), 0);
218 r = sshbuf_put_u64(p1, 0x1122334455667788ULL);
219 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
220 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
221 sshbuf_free(p1);
222 TEST_DONE();
223
224 TEST_START("sshbuf_put_u32");
225 p1 = sshbuf_new();
226 ASSERT_PTR_NE(p1, NULL);
227 ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x11223344), 0);
228 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
229 ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 4);
230 sshbuf_free(p1);
231 TEST_DONE();
232
233 TEST_START("sshbuf_put_u32 exact");
234 p1 = sshbuf_new();
235 ASSERT_PTR_NE(p1, NULL);
236 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 4), 0);
237 ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x11223344), 0);
238 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
239 ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 4);
240 sshbuf_free(p1);
241 TEST_DONE();
242
243 TEST_START("sshbuf_put_u32 limited");
244 p1 = sshbuf_new();
245 ASSERT_PTR_NE(p1, NULL);
246 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 3), 0);
247 r = sshbuf_put_u32(p1, 0x11223344);
248 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
249 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
250 sshbuf_free(p1);
251 TEST_DONE();
252
253 TEST_START("sshbuf_put_u16");
254 p1 = sshbuf_new();
255 ASSERT_PTR_NE(p1, NULL);
256 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0x1122), 0);
257 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
258 ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 2);
259 sshbuf_free(p1);
260 TEST_DONE();
261
262 TEST_START("sshbuf_put_u16");
263 p1 = sshbuf_new();
264 ASSERT_PTR_NE(p1, NULL);
265 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 2), 0);
266 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0x1122), 0);
267 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
268 ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 2);
269 sshbuf_free(p1);
270 TEST_DONE();
271
272 TEST_START("sshbuf_put_u16 limited");
273 p1 = sshbuf_new();
274 ASSERT_PTR_NE(p1, NULL);
275 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 1), 0);
276 r = sshbuf_put_u16(p1, 0x1122);
277 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
278 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
279 sshbuf_free(p1);
280 TEST_DONE();
281
282 TEST_START("sshbuf_get_string");
283 p1 = sshbuf_new();
284 ASSERT_PTR_NE(p1, NULL);
285 ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
286 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
287 ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
288 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4 + 4);
289 ASSERT_INT_EQ(sshbuf_get_string(p1, &d, &s), 0);
290 ASSERT_SIZE_T_EQ(s, sizeof(x));
291 ASSERT_MEM_EQ(d, x, sizeof(x));
292 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
293 free(d);
294 sshbuf_free(p1);
295 TEST_DONE();
296
297 TEST_START("sshbuf_get_string exact");
298 p1 = sshbuf_new();
299 ASSERT_PTR_NE(p1, NULL);
300 ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(x) + 4), 0);
301 ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
302 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
303 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
304 ASSERT_INT_EQ(sshbuf_get_string(p1, &d, &s), 0);
305 ASSERT_SIZE_T_EQ(s, sizeof(x));
306 ASSERT_MEM_EQ(d, x, sizeof(x));
307 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
308 free(d);
309 sshbuf_free(p1);
310 TEST_DONE();
311
312 TEST_START("sshbuf_get_string truncated");
313 p1 = sshbuf_new();
314 ASSERT_PTR_NE(p1, NULL);
315 ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
316 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
317 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
318 ASSERT_INT_EQ(sshbuf_consume_end(p1, 1), 0);
319 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 3);
320 r = sshbuf_get_string(p1, &d, &s);
321 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
322 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 3);
323 sshbuf_free(p1);
324 TEST_DONE();
325
326 TEST_START("sshbuf_get_string giant");
327 p1 = sshbuf_new();
328 ASSERT_PTR_NE(p1, NULL);
329 ASSERT_INT_EQ(sshbuf_put_u32(p1, 0xffffffff), 0);
330 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
331 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
332 r = sshbuf_get_string(p1, &d, &s);
333 ASSERT_INT_EQ(r, SSH_ERR_STRING_TOO_LARGE);
334 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
335 sshbuf_free(p1);
336 TEST_DONE();
337
338 TEST_START("sshbuf_get_cstring giant");
339 p1 = sshbuf_new();
340 ASSERT_PTR_NE(p1, NULL);
341 ASSERT_INT_EQ(sshbuf_put_u32(p1, 0xffffffff), 0);
342 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
343 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
344 r = sshbuf_get_cstring(p1, &s2, &s);
345 ASSERT_INT_EQ(r, SSH_ERR_STRING_TOO_LARGE);
346 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
347 sshbuf_free(p1);
348 TEST_DONE();
349
350 TEST_START("sshbuf_get_cstring embedded \\0");
351 p1 = sshbuf_new();
352 ASSERT_PTR_NE(p1, NULL);
353 ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
354 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
355 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
356 r = sshbuf_get_cstring(p1, &s2, NULL);
357 ASSERT_INT_EQ(r, SSH_ERR_INVALID_FORMAT);
358 sshbuf_free(p1);
359 TEST_DONE();
360
361 TEST_START("sshbuf_get_cstring trailing \\0");
362 p1 = sshbuf_new();
363 ASSERT_PTR_NE(p1, NULL);
364 ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x) - 1), 0);
365 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x) - 1), 0);
366 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4 - 1);
367 ASSERT_INT_EQ(sshbuf_get_cstring(p1, &s2, &s), 0);
368 ASSERT_SIZE_T_EQ(s, sizeof(x) - 1);
369 ASSERT_MEM_EQ(s2, x, s);
370 free(s2);
371 sshbuf_free(p1);
372 TEST_DONE();
373
374 TEST_START("sshbuf_put_string");
375 p1 = sshbuf_new();
376 ASSERT_PTR_NE(p1, NULL);
377 ASSERT_INT_EQ(sshbuf_put_string(p1, x, sizeof(x)), 0);
378 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
379 ASSERT_U32_EQ(PEEK_U32(sshbuf_ptr(p1)), sizeof(x));
380 ASSERT_MEM_EQ(sshbuf_ptr(p1) + 4, x, sizeof(x));
381 sshbuf_free(p1);
382 TEST_DONE();
383
384 TEST_START("sshbuf_put_string limited");
385 p1 = sshbuf_new();
386 ASSERT_PTR_NE(p1, NULL);
387 ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(x) + 4 - 1), 0);
388 r = sshbuf_put_string(p1, x, sizeof(x));
389 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
390 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
391 sshbuf_free(p1);
392 TEST_DONE();
393
394 TEST_START("sshbuf_put_string giant");
395 p1 = sshbuf_new();
396 ASSERT_PTR_NE(p1, NULL);
397 r = sshbuf_put_string(p1, (void *)0x01, 0xfffffffc);
398 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
399 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
400 sshbuf_free(p1);
401 TEST_DONE();
402
403 TEST_START("sshbuf_putf");
404 p1 = sshbuf_new();
405 ASSERT_PTR_NE(p1, NULL);
406 r = sshbuf_putf(p1, "%s %d %x", "hello", 23, 0x5f);
407 ASSERT_INT_EQ(r, 0);
408 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 11);
409 ASSERT_MEM_EQ(sshbuf_ptr(p1), "hello 23 5f", 11);
410 sshbuf_free(p1);
411 TEST_DONE();
412
413 TEST_START("sshbuf_putb");
414 p1 = sshbuf_new();
415 ASSERT_PTR_NE(p1, NULL);
416 p2 = sshbuf_new();
417 ASSERT_PTR_NE(p2, NULL);
418 ASSERT_INT_EQ(sshbuf_put(p1, "blahblahblah", 12), 0);
419 ASSERT_INT_EQ(sshbuf_putb(p2, p1), 0);
420 sshbuf_free(p1);
421 ASSERT_SIZE_T_EQ(sshbuf_len(p2), 12);
422 ASSERT_MEM_EQ(sshbuf_ptr(p2), "blahblahblah", 12);
423 sshbuf_free(p2);
424 TEST_DONE();
425
426 TEST_START("sshbuf_put_bignum2_bytes empty buf");
427 p1 = sshbuf_new();
428 ASSERT_PTR_NE(p1, NULL);
429 ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, NULL, 0), 0);
430 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp1));
431 ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp1, sizeof(bn_exp1));
432 sshbuf_free(p1);
433 TEST_DONE();
434
435 TEST_START("sshbuf_put_bignum2_bytes all zeroes");
436 p1 = sshbuf_new();
437 ASSERT_PTR_NE(p1, NULL);
438 ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn1, sizeof(bn1)), 0);
439 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp1));
440 ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp1, sizeof(bn_exp1));
441 sshbuf_free(p1);
442 TEST_DONE();
443
444 TEST_START("sshbuf_put_bignum2_bytes simple");
445 p1 = sshbuf_new();
446 ASSERT_PTR_NE(p1, NULL);
447 ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn2+2, sizeof(bn2)-2), 0);
448 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp2));
449 ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp2, sizeof(bn_exp2));
450 sshbuf_free(p1);
451 TEST_DONE();
452
453 TEST_START("sshbuf_put_bignum2_bytes leading zero");
454 p1 = sshbuf_new();
455 ASSERT_PTR_NE(p1, NULL);
456 ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn2, sizeof(bn2)), 0);
457 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp2));
458 ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp2, sizeof(bn_exp2));
459 sshbuf_free(p1);
460 TEST_DONE();
461
462 TEST_START("sshbuf_put_bignum2_bytes neg");
463 p1 = sshbuf_new();
464 ASSERT_PTR_NE(p1, NULL);
465 ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn3+1, sizeof(bn3)-1), 0);
466 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp3));
467 ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp3, sizeof(bn_exp3));
468 sshbuf_free(p1);
469 TEST_DONE();
470
471 TEST_START("sshbuf_put_bignum2_bytes neg and leading zero");
472 p1 = sshbuf_new();
473 ASSERT_PTR_NE(p1, NULL);
474 ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn3, sizeof(bn3)), 0);
475 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp3));
476 ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp3, sizeof(bn_exp3));
477 sshbuf_free(p1);
478 TEST_DONE();
479
480 TEST_START("sshbuf_peek_u64");
481 p1 = sshbuf_new();
482 ASSERT_PTR_NE(p1, NULL);
483 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
484 ASSERT_INT_EQ(sshbuf_peek_u64(p1, 0, &v64), 0);
485 ASSERT_U64_EQ(v64, 0x1122334455667788ULL);
486 ASSERT_INT_EQ(sshbuf_peek_u64(p1, 2, &v64), 0);
487 ASSERT_U64_EQ(v64, 0x3344556677880099ULL);
488 ASSERT_INT_EQ(sshbuf_peek_u64(p1, 3, &v64), SSH_ERR_MESSAGE_INCOMPLETE);
489 ASSERT_INT_EQ(sshbuf_peek_u64(p1, sizeof(x), &v64),
490 SSH_ERR_MESSAGE_INCOMPLETE);
491 ASSERT_INT_EQ(sshbuf_peek_u64(p1, 1000, &v64),
492 SSH_ERR_MESSAGE_INCOMPLETE);
493 sshbuf_free(p1);
494 TEST_DONE();
495
496 TEST_START("sshbuf_peek_u32");
497 p1 = sshbuf_new();
498 ASSERT_PTR_NE(p1, NULL);
499 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
500 ASSERT_INT_EQ(sshbuf_peek_u32(p1, 0, &v32), 0);
501 ASSERT_U32_EQ(v32, 0x11223344);
502 ASSERT_INT_EQ(sshbuf_peek_u32(p1, 6, &v32), 0);
503 ASSERT_U32_EQ(v32, 0x77880099);
504 ASSERT_INT_EQ(sshbuf_peek_u32(p1, 7, &v32), SSH_ERR_MESSAGE_INCOMPLETE);
505 ASSERT_INT_EQ(sshbuf_peek_u32(p1, sizeof(x), &v32),
506 SSH_ERR_MESSAGE_INCOMPLETE);
507 ASSERT_INT_EQ(sshbuf_peek_u32(p1, 1000, &v32),
508 SSH_ERR_MESSAGE_INCOMPLETE);
509 sshbuf_free(p1);
510 TEST_DONE();
511
512 TEST_START("sshbuf_peek_u16");
513 p1 = sshbuf_new();
514 ASSERT_PTR_NE(p1, NULL);
515 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
516 ASSERT_INT_EQ(sshbuf_peek_u16(p1, 0, &v16), 0);
517 ASSERT_U16_EQ(v16, 0x1122);
518 ASSERT_INT_EQ(sshbuf_peek_u16(p1, 8, &v16), 0);
519 ASSERT_U16_EQ(v16, 0x99);
520 ASSERT_INT_EQ(sshbuf_peek_u16(p1, 9, &v16), SSH_ERR_MESSAGE_INCOMPLETE);
521 ASSERT_INT_EQ(sshbuf_peek_u16(p1, sizeof(x), &v16),
522 SSH_ERR_MESSAGE_INCOMPLETE);
523 ASSERT_INT_EQ(sshbuf_peek_u16(p1, 1000, &v16),
524 SSH_ERR_MESSAGE_INCOMPLETE);
525 sshbuf_free(p1);
526 TEST_DONE();
527
528 TEST_START("sshbuf_peek_u8");
529 p1 = sshbuf_new();
530 ASSERT_PTR_NE(p1, NULL);
531 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
532 ASSERT_INT_EQ(sshbuf_peek_u8(p1, 0, &v8), 0);
533 ASSERT_U8_EQ(v8, 0x11);
534 ASSERT_INT_EQ(sshbuf_peek_u8(p1, 9, &v8), 0);
535 ASSERT_U8_EQ(v8, 0x99);
536 ASSERT_INT_EQ(sshbuf_peek_u8(p1, sizeof(x), &v8),
537 SSH_ERR_MESSAGE_INCOMPLETE);
538 ASSERT_INT_EQ(sshbuf_peek_u8(p1, 1000, &v8),
539 SSH_ERR_MESSAGE_INCOMPLETE);
540 sshbuf_free(p1);
541 TEST_DONE();
542
543 TEST_START("sshbuf_poke_u64");
544 p1 = sshbuf_new();
545 ASSERT_PTR_NE(p1, NULL);
546 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
547 /* poke at start of buffer */
548 ASSERT_INT_EQ(sshbuf_poke_u64(p1, 0, 0xa1b2c3d4e5f60718ULL), 0);
549 s2 = sshbuf_dtob16(p1);
550 ASSERT_PTR_NE(s2, NULL);
551 ASSERT_STRING_EQ(s2, "a1b2c3d4e5f607180000");
552 free(s2);
553 sshbuf_reset(p1);
554 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
555 /* poke aligned with end of buffer */
556 ASSERT_INT_EQ(sshbuf_poke_u64(p1, 2, 0xa1b2c3d4e5f60718ULL), 0);
557 s2 = sshbuf_dtob16(p1);
558 ASSERT_PTR_NE(s2, NULL);
559 ASSERT_STRING_EQ(s2, "0000a1b2c3d4e5f60718");
560 free(s2);
561 sshbuf_reset(p1);
562 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
563 /* poke past end of buffer */
564 ASSERT_INT_EQ(sshbuf_poke_u64(p1, 3, 0xa1b2c3d4e5f60718ULL),
565 SSH_ERR_NO_BUFFER_SPACE);
566 ASSERT_INT_EQ(sshbuf_poke_u64(p1, 10, 0xa1b2c3d4e5f60718ULL),
567 SSH_ERR_NO_BUFFER_SPACE);
568 ASSERT_INT_EQ(sshbuf_poke_u64(p1, 1000, 0xa1b2c3d4e5f60718ULL),
569 SSH_ERR_NO_BUFFER_SPACE);
570 /* ensure failed pokes do not modify buffer */
571 s2 = sshbuf_dtob16(p1);
572 ASSERT_PTR_NE(s2, NULL);
573 ASSERT_STRING_EQ(s2, "00000000000000000000");
574 sshbuf_free(p1);
575 TEST_DONE();
576
577 TEST_START("sshbuf_poke_u32");
578 p1 = sshbuf_new();
579 ASSERT_PTR_NE(p1, NULL);
580 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
581 /* poke at start of buffer */
582 ASSERT_INT_EQ(sshbuf_poke_u32(p1, 0, 0xa1b2c3d4), 0);
583 s2 = sshbuf_dtob16(p1);
584 ASSERT_PTR_NE(s2, NULL);
585 ASSERT_STRING_EQ(s2, "a1b2c3d4000000000000");
586 free(s2);
587 sshbuf_reset(p1);
588 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
589 /* poke aligned with end of buffer */
590 ASSERT_INT_EQ(sshbuf_poke_u32(p1, 6, 0xa1b2c3d4), 0);
591 s2 = sshbuf_dtob16(p1);
592 ASSERT_PTR_NE(s2, NULL);
593 ASSERT_STRING_EQ(s2, "000000000000a1b2c3d4");
594 free(s2);
595 sshbuf_reset(p1);
596 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
597 /* poke past end of buffer */
598 ASSERT_INT_EQ(sshbuf_poke_u32(p1, 7, 0xa1b2c3d4),
599 SSH_ERR_NO_BUFFER_SPACE);
600 ASSERT_INT_EQ(sshbuf_poke_u32(p1, 10, 0xa1b2c3d4),
601 SSH_ERR_NO_BUFFER_SPACE);
602 ASSERT_INT_EQ(sshbuf_poke_u32(p1, 1000, 0xa1b2c3d4),
603 SSH_ERR_NO_BUFFER_SPACE);
604 /* ensure failed pokes do not modify buffer */
605 s2 = sshbuf_dtob16(p1);
606 ASSERT_PTR_NE(s2, NULL);
607 ASSERT_STRING_EQ(s2, "00000000000000000000");
608 sshbuf_free(p1);
609 TEST_DONE();
610
611 TEST_START("sshbuf_poke_u16");
612 p1 = sshbuf_new();
613 ASSERT_PTR_NE(p1, NULL);
614 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
615 /* poke at start of buffer */
616 ASSERT_INT_EQ(sshbuf_poke_u16(p1, 0, 0xa1b2), 0);
617 s2 = sshbuf_dtob16(p1);
618 ASSERT_PTR_NE(s2, NULL);
619 ASSERT_STRING_EQ(s2, "a1b20000000000000000");
620 free(s2);
621 sshbuf_reset(p1);
622 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
623 /* poke aligned with end of buffer */
624 ASSERT_INT_EQ(sshbuf_poke_u16(p1, 8, 0xa1b2), 0);
625 s2 = sshbuf_dtob16(p1);
626 ASSERT_PTR_NE(s2, NULL);
627 ASSERT_STRING_EQ(s2, "0000000000000000a1b2");
628 free(s2);
629 sshbuf_reset(p1);
630 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
631 /* poke past end of buffer */
632 ASSERT_INT_EQ(sshbuf_poke_u16(p1, 9, 0xa1b2),
633 SSH_ERR_NO_BUFFER_SPACE);
634 ASSERT_INT_EQ(sshbuf_poke_u16(p1, 10, 0xa1b2),
635 SSH_ERR_NO_BUFFER_SPACE);
636 ASSERT_INT_EQ(sshbuf_poke_u16(p1, 1000, 0xa1b2),
637 SSH_ERR_NO_BUFFER_SPACE);
638 /* ensure failed pokes do not modify buffer */
639 s2 = sshbuf_dtob16(p1);
640 ASSERT_PTR_NE(s2, NULL);
641 ASSERT_STRING_EQ(s2, "00000000000000000000");
642 sshbuf_free(p1);
643 TEST_DONE();
644
645 TEST_START("sshbuf_poke_u8");
646 p1 = sshbuf_new();
647 ASSERT_PTR_NE(p1, NULL);
648 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
649 /* poke at start of buffer */
650 ASSERT_INT_EQ(sshbuf_poke_u8(p1, 0, 0xa1), 0);
651 s2 = sshbuf_dtob16(p1);
652 ASSERT_PTR_NE(s2, NULL);
653 ASSERT_STRING_EQ(s2, "a1000000000000000000");
654 free(s2);
655 sshbuf_reset(p1);
656 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
657 /* poke aligned with end of buffer */
658 ASSERT_INT_EQ(sshbuf_poke_u8(p1, 9, 0xa1), 0);
659 s2 = sshbuf_dtob16(p1);
660 ASSERT_PTR_NE(s2, NULL);
661 ASSERT_STRING_EQ(s2, "000000000000000000a1");
662 free(s2);
663 sshbuf_reset(p1);
664 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
665 /* poke past end of buffer */
666 ASSERT_INT_EQ(sshbuf_poke_u8(p1, 10, 0xa1), SSH_ERR_NO_BUFFER_SPACE);
667 ASSERT_INT_EQ(sshbuf_poke_u8(p1, 1000, 0xa1), SSH_ERR_NO_BUFFER_SPACE);
668 /* ensure failed pokes do not modify buffer */
669 s2 = sshbuf_dtob16(p1);
670 ASSERT_PTR_NE(s2, NULL);
671 ASSERT_STRING_EQ(s2, "00000000000000000000");
672 sshbuf_free(p1);
673 TEST_DONE();
674
675 TEST_START("sshbuf_poke");
676 p1 = sshbuf_new();
677 ASSERT_PTR_NE(p1, NULL);
678 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
679 /* poke at start of buffer */
680 ASSERT_INT_EQ(sshbuf_poke(p1, 0, "hello!", 6), 0);
681 s2 = sshbuf_dtob16(p1);
682 ASSERT_PTR_NE(s2, NULL);
683 ASSERT_STRING_EQ(s2, "68656c6c6f2100000000");
684 free(s2);
685 sshbuf_reset(p1);
686 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
687 /* poke aligned with end of buffer */
688 ASSERT_INT_EQ(sshbuf_poke(p1, 4, "hello!", 6), 0);
689 s2 = sshbuf_dtob16(p1);
690 ASSERT_PTR_NE(s2, NULL);
691 ASSERT_STRING_EQ(s2, "0000000068656c6c6f21");
692 free(s2);
693 sshbuf_reset(p1);
694 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0);
695 /* poke past end of buffer */
696 ASSERT_INT_EQ(sshbuf_poke(p1, 7, "hello!", 6),
697 SSH_ERR_NO_BUFFER_SPACE);
698 ASSERT_INT_EQ(sshbuf_poke(p1, 10, "hello!", 6),
699 SSH_ERR_NO_BUFFER_SPACE);
700 ASSERT_INT_EQ(sshbuf_poke(p1, 1000, "hello!", 6),
701 SSH_ERR_NO_BUFFER_SPACE);
702 /* ensure failed pokes do not modify buffer */
703 s2 = sshbuf_dtob16(p1);
704 ASSERT_PTR_NE(s2, NULL);
705 ASSERT_STRING_EQ(s2, "00000000000000000000");
706 sshbuf_free(p1);
707 TEST_DONE();
708 }
709