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