summaryrefslogtreecommitdiff
path: root/regress/unittests/sshbuf/test_sshbuf_getput_basic.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2014-05-15 15:17:15 +1000
committerDamien Miller <djm@mindrot.org>2014-05-15 15:17:15 +1000
commitdef1de086707b0e6b046fe7e115c60aca0227a99 (patch)
tree16a736c080243e1e80fd1ea850ca0e88d657c2cc /regress/unittests/sshbuf/test_sshbuf_getput_basic.c
parent167685756fde8bc213a8df2c8e1848e312db0f46 (diff)
- (djm) [regress/unittests/Makefile]
[regress/unittests/Makefile.inc] [regress/unittests/sshbuf/Makefile] [regress/unittests/sshbuf/test_sshbuf.c] [regress/unittests/sshbuf/test_sshbuf_fixed.c] [regress/unittests/sshbuf/test_sshbuf_fuzz.c] [regress/unittests/sshbuf/test_sshbuf_getput_basic.c] [regress/unittests/sshbuf/test_sshbuf_getput_crypto.c] [regress/unittests/sshbuf/test_sshbuf_getput_fuzz.c] [regress/unittests/sshbuf/test_sshbuf_misc.c] [regress/unittests/sshbuf/tests.c] [regress/unittests/test_helper/Makefile] [regress/unittests/test_helper/fuzz.c] [regress/unittests/test_helper/test_helper.c] [regress/unittests/test_helper/test_helper.h] Import new unit tests from OpenBSD; not yet hooked up to build.
Diffstat (limited to 'regress/unittests/sshbuf/test_sshbuf_getput_basic.c')
-rw-r--r--regress/unittests/sshbuf/test_sshbuf_getput_basic.c480
1 files changed, 480 insertions, 0 deletions
diff --git a/regress/unittests/sshbuf/test_sshbuf_getput_basic.c b/regress/unittests/sshbuf/test_sshbuf_getput_basic.c
new file mode 100644
index 000000000..2d469ec11
--- /dev/null
+++ b/regress/unittests/sshbuf/test_sshbuf_getput_basic.c
@@ -0,0 +1,480 @@
1/* $OpenBSD: test_sshbuf_getput_basic.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#include "ssherr.h"
17#include "sshbuf.h"
18
19void sshbuf_getput_basic_tests(void);
20
21void
22sshbuf_getput_basic_tests(void)
23{
24 struct sshbuf *p1, *p2;
25 const u_char *cd;
26 u_char *d, d2[32], x[] = {
27 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x00, 0x99
28 };
29 u_int64_t v64;
30 u_int32_t v32;
31 u_int16_t v16;
32 u_char v8;
33 size_t s;
34 char *s2;
35 int r;
36 u_char bn1[] = { 0x00, 0x00, 0x00 };
37 u_char bn2[] = { 0x00, 0x00, 0x01, 0x02 };
38 u_char bn3[] = { 0x00, 0x80, 0x09 };
39 u_char bn_exp1[] = { 0x00, 0x00, 0x00, 0x00 };
40 u_char bn_exp2[] = { 0x00, 0x00, 0x00, 0x02, 0x01, 0x02 };
41 u_char bn_exp3[] = { 0x00, 0x00, 0x00, 0x03, 0x00, 0x80, 0x09 };
42
43 TEST_START("PEEK_U64");
44 ASSERT_U64_EQ(PEEK_U64(x), 0x1122334455667788ULL);
45 TEST_DONE();
46
47 TEST_START("PEEK_U32");
48 ASSERT_U32_EQ(PEEK_U32(x), 0x11223344);
49 TEST_DONE();
50
51 TEST_START("PEEK_U16");
52 ASSERT_U16_EQ(PEEK_U16(x), 0x1122);
53 TEST_DONE();
54
55 TEST_START("POKE_U64");
56 bzero(d2, sizeof(d2));
57 POKE_U64(d2, 0x1122334455667788ULL);
58 ASSERT_MEM_EQ(d2, x, 8);
59 TEST_DONE();
60
61 TEST_START("POKE_U32");
62 bzero(d2, sizeof(d2));
63 POKE_U32(d2, 0x11223344);
64 ASSERT_MEM_EQ(d2, x, 4);
65 TEST_DONE();
66
67 TEST_START("POKE_U16");
68 bzero(d2, sizeof(d2));
69 POKE_U16(d2, 0x1122);
70 ASSERT_MEM_EQ(d2, x, 2);
71 TEST_DONE();
72
73 TEST_START("sshbuf_put");
74 p1 = sshbuf_new();
75 ASSERT_PTR_NE(p1, NULL);
76 ASSERT_INT_EQ(sshbuf_put(p1, x, 5), 0);
77 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 5);
78 cd = sshbuf_ptr(p1);
79 ASSERT_PTR_NE(cd, NULL);
80 ASSERT_U8_EQ(cd[0], 0x11);
81 ASSERT_U8_EQ(cd[1], 0x22);
82 ASSERT_U8_EQ(cd[2], 0x33);
83 ASSERT_U8_EQ(cd[3], 0x44);
84 ASSERT_U8_EQ(cd[4], 0x55);
85 TEST_DONE();
86
87 TEST_START("sshbuf_get");
88 ASSERT_INT_EQ(sshbuf_get(p1, d2, 4), 0);
89 ASSERT_MEM_EQ(d2, x, 4);
90 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
91 ASSERT_U8_EQ(*(sshbuf_ptr(p1)), 0x55);
92 TEST_DONE();
93
94 TEST_START("sshbuf_get truncated");
95 r = sshbuf_get(p1, d2, 4);
96 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
97 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
98 ASSERT_U8_EQ(*(sshbuf_ptr(p1)), 0x55);
99 TEST_DONE();
100
101 TEST_START("sshbuf_put truncated");
102 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 4), 0);
103 r = sshbuf_put(p1, x, 5);
104 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
105 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
106 sshbuf_free(p1);
107 TEST_DONE();
108
109 TEST_START("sshbuf_get_u64");
110 p1 = sshbuf_new();
111 ASSERT_PTR_NE(p1, NULL);
112 ASSERT_INT_EQ(sshbuf_put(p1, x, 10), 0);
113 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 10);
114 ASSERT_INT_EQ(sshbuf_get_u64(p1, &v64), 0);
115 ASSERT_U64_EQ(v64, 0x1122334455667788ULL);
116 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
117 TEST_DONE();
118
119 TEST_START("sshbuf_get_u64 truncated");
120 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
121 r = sshbuf_get_u64(p1, &v64);
122 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
123 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
124 sshbuf_free(p1);
125 TEST_DONE();
126
127 TEST_START("sshbuf_get_u32");
128 p1 = sshbuf_new();
129 ASSERT_PTR_NE(p1, NULL);
130 ASSERT_INT_EQ(sshbuf_put(p1, x, 10), 0);
131 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 10);
132 ASSERT_INT_EQ(sshbuf_get_u32(p1, &v32), 0);
133 ASSERT_U32_EQ(v32, 0x11223344);
134 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 6);
135 ASSERT_INT_EQ(sshbuf_get_u32(p1, &v32), 0);
136 ASSERT_U32_EQ(v32, 0x55667788);
137 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
138 TEST_DONE();
139
140 TEST_START("sshbuf_get_u32 truncated");
141 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
142 r = sshbuf_get_u32(p1, &v32);
143 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
144 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
145 sshbuf_free(p1);
146 TEST_DONE();
147
148 TEST_START("sshbuf_get_u16");
149 p1 = sshbuf_new();
150 ASSERT_PTR_NE(p1, NULL);
151 ASSERT_INT_EQ(sshbuf_put(p1, x, 9), 0);
152 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 9);
153 ASSERT_INT_EQ(sshbuf_get_u16(p1, &v16), 0);
154 ASSERT_U16_EQ(v16, 0x1122);
155 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 7);
156 ASSERT_INT_EQ(sshbuf_get_u16(p1, &v16), 0);
157 ASSERT_U16_EQ(v16, 0x3344);
158 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 5);
159 ASSERT_INT_EQ(sshbuf_get_u16(p1, &v16), 0);
160 ASSERT_U16_EQ(v16, 0x5566);
161 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 3);
162 ASSERT_INT_EQ(sshbuf_get_u16(p1, &v16), 0);
163 ASSERT_U16_EQ(v16, 0x7788);
164 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
165 TEST_DONE();
166
167 TEST_START("sshbuf_get_u16 truncated");
168 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
169 r = sshbuf_get_u16(p1, &v16);
170 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
171 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
172 sshbuf_free(p1);
173 TEST_DONE();
174
175 TEST_START("sshbuf_get_u8");
176 p1 = sshbuf_new();
177 ASSERT_PTR_NE(p1, NULL);
178 ASSERT_INT_EQ(sshbuf_put(p1, x, 2), 0);
179 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
180 ASSERT_INT_EQ(sshbuf_get_u8(p1, &v8), 0);
181 ASSERT_U8_EQ(v8, 0x11);
182 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
183 ASSERT_INT_EQ(sshbuf_get_u8(p1, &v8), 0);
184 ASSERT_U8_EQ(v8, 0x22);
185 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
186 TEST_DONE();
187
188 TEST_START("sshbuf_get_u8 truncated");
189 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
190 r = sshbuf_get_u8(p1, &v8);
191 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
192 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
193 sshbuf_free(p1);
194 TEST_DONE();
195
196 TEST_START("sshbuf_put_u64");
197 p1 = sshbuf_new();
198 ASSERT_PTR_NE(p1, NULL);
199 ASSERT_INT_EQ(sshbuf_put_u64(p1, 0x1122334455667788ULL), 0);
200 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 8);
201 ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 8);
202 sshbuf_free(p1);
203 TEST_DONE();
204
205 TEST_START("sshbuf_put_u64 exact");
206 p1 = sshbuf_new();
207 ASSERT_PTR_NE(p1, NULL);
208 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 8), 0);
209 ASSERT_INT_EQ(sshbuf_put_u64(p1, 0x1122334455667788ULL), 0);
210 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 8);
211 ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 8);
212 sshbuf_free(p1);
213 TEST_DONE();
214
215 TEST_START("sshbuf_put_u64 limited");
216 p1 = sshbuf_new();
217 ASSERT_PTR_NE(p1, NULL);
218 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 7), 0);
219 r = sshbuf_put_u64(p1, 0x1122334455667788ULL);
220 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
221 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
222 sshbuf_free(p1);
223 TEST_DONE();
224
225 TEST_START("sshbuf_put_u32");
226 p1 = sshbuf_new();
227 ASSERT_PTR_NE(p1, NULL);
228 ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x11223344), 0);
229 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
230 ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 4);
231 sshbuf_free(p1);
232 TEST_DONE();
233
234 TEST_START("sshbuf_put_u32 exact");
235 p1 = sshbuf_new();
236 ASSERT_PTR_NE(p1, NULL);
237 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 4), 0);
238 ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x11223344), 0);
239 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
240 ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 4);
241 sshbuf_free(p1);
242 TEST_DONE();
243
244 TEST_START("sshbuf_put_u32 limited");
245 p1 = sshbuf_new();
246 ASSERT_PTR_NE(p1, NULL);
247 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 3), 0);
248 r = sshbuf_put_u32(p1, 0x11223344);
249 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
250 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
251 sshbuf_free(p1);
252 TEST_DONE();
253
254 TEST_START("sshbuf_put_u16");
255 p1 = sshbuf_new();
256 ASSERT_PTR_NE(p1, NULL);
257 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0x1122), 0);
258 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
259 ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 2);
260 sshbuf_free(p1);
261 TEST_DONE();
262
263 TEST_START("sshbuf_put_u16");
264 p1 = sshbuf_new();
265 ASSERT_PTR_NE(p1, NULL);
266 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 2), 0);
267 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0x1122), 0);
268 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
269 ASSERT_MEM_EQ(sshbuf_ptr(p1), x, 2);
270 sshbuf_free(p1);
271 TEST_DONE();
272
273 TEST_START("sshbuf_put_u16 limited");
274 p1 = sshbuf_new();
275 ASSERT_PTR_NE(p1, NULL);
276 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 1), 0);
277 r = sshbuf_put_u16(p1, 0x1122);
278 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
279 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
280 sshbuf_free(p1);
281 TEST_DONE();
282
283 TEST_START("sshbuf_get_string");
284 p1 = sshbuf_new();
285 ASSERT_PTR_NE(p1, NULL);
286 ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
287 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
288 ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
289 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4 + 4);
290 ASSERT_INT_EQ(sshbuf_get_string(p1, &d, &s), 0);
291 ASSERT_SIZE_T_EQ(s, sizeof(x));
292 ASSERT_MEM_EQ(d, x, sizeof(x));
293 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
294 free(d);
295 sshbuf_free(p1);
296 TEST_DONE();
297
298 TEST_START("sshbuf_get_string exact");
299 p1 = sshbuf_new();
300 ASSERT_PTR_NE(p1, NULL);
301 ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(x) + 4), 0);
302 ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
303 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
304 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
305 ASSERT_INT_EQ(sshbuf_get_string(p1, &d, &s), 0);
306 ASSERT_SIZE_T_EQ(s, sizeof(x));
307 ASSERT_MEM_EQ(d, x, sizeof(x));
308 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
309 free(d);
310 sshbuf_free(p1);
311 TEST_DONE();
312
313 TEST_START("sshbuf_get_string truncated");
314 p1 = sshbuf_new();
315 ASSERT_PTR_NE(p1, NULL);
316 ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
317 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
318 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
319 ASSERT_INT_EQ(sshbuf_consume_end(p1, 1), 0);
320 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 3);
321 r = sshbuf_get_string(p1, &d, &s);
322 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
323 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 3);
324 sshbuf_free(p1);
325 TEST_DONE();
326
327 TEST_START("sshbuf_get_string giant");
328 p1 = sshbuf_new();
329 ASSERT_PTR_NE(p1, NULL);
330 ASSERT_INT_EQ(sshbuf_put_u32(p1, 0xffffffff), 0);
331 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
332 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
333 r = sshbuf_get_string(p1, &d, &s);
334 ASSERT_INT_EQ(r, SSH_ERR_STRING_TOO_LARGE);
335 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
336 sshbuf_free(p1);
337 TEST_DONE();
338
339 TEST_START("sshbuf_get_cstring giant");
340 p1 = sshbuf_new();
341 ASSERT_PTR_NE(p1, NULL);
342 ASSERT_INT_EQ(sshbuf_put_u32(p1, 0xffffffff), 0);
343 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
344 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
345 r = sshbuf_get_cstring(p1, &s2, &s);
346 ASSERT_INT_EQ(r, SSH_ERR_STRING_TOO_LARGE);
347 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
348 sshbuf_free(p1);
349 TEST_DONE();
350
351 TEST_START("sshbuf_get_cstring embedded \\0");
352 p1 = sshbuf_new();
353 ASSERT_PTR_NE(p1, NULL);
354 ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x)), 0);
355 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0);
356 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
357 r = sshbuf_get_cstring(p1, &s2, NULL);
358 ASSERT_INT_EQ(r, SSH_ERR_INVALID_FORMAT);
359 sshbuf_free(p1);
360 TEST_DONE();
361
362 TEST_START("sshbuf_get_cstring trailing \\0");
363 p1 = sshbuf_new();
364 ASSERT_PTR_NE(p1, NULL);
365 ASSERT_INT_EQ(sshbuf_put_u32(p1, sizeof(x) - 1), 0);
366 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x) - 1), 0);
367 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4 - 1);
368 ASSERT_INT_EQ(sshbuf_get_cstring(p1, &s2, &s), 0);
369 ASSERT_SIZE_T_EQ(s, sizeof(x) - 1);
370 ASSERT_MEM_EQ(s2, x, s);
371 free(s2);
372 sshbuf_free(p1);
373 TEST_DONE();
374
375 TEST_START("sshbuf_put_string");
376 p1 = sshbuf_new();
377 ASSERT_PTR_NE(p1, NULL);
378 ASSERT_INT_EQ(sshbuf_put_string(p1, x, sizeof(x)), 0);
379 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(x) + 4);
380 ASSERT_U32_EQ(PEEK_U32(sshbuf_ptr(p1)), sizeof(x));
381 ASSERT_MEM_EQ(sshbuf_ptr(p1) + 4, x, sizeof(x));
382 sshbuf_free(p1);
383 TEST_DONE();
384
385 TEST_START("sshbuf_put_string limited");
386 p1 = sshbuf_new();
387 ASSERT_PTR_NE(p1, NULL);
388 ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(x) + 4 - 1), 0);
389 r = sshbuf_put_string(p1, x, sizeof(x));
390 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
391 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
392 sshbuf_free(p1);
393 TEST_DONE();
394
395 TEST_START("sshbuf_put_string giant");
396 p1 = sshbuf_new();
397 ASSERT_PTR_NE(p1, NULL);
398 r = sshbuf_put_string(p1, (void *)0x01, 0xfffffffc);
399 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
400 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
401 sshbuf_free(p1);
402 TEST_DONE();
403
404 TEST_START("sshbuf_putf");
405 p1 = sshbuf_new();
406 ASSERT_PTR_NE(p1, NULL);
407 r = sshbuf_putf(p1, "%s %d %x", "hello", 23, 0x5f);
408 ASSERT_INT_EQ(r, 0);
409 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 11);
410 ASSERT_MEM_EQ(sshbuf_ptr(p1), "hello 23 5f", 11);
411 sshbuf_free(p1);
412 TEST_DONE();
413
414 TEST_START("sshbuf_putb");
415 p1 = sshbuf_new();
416 ASSERT_PTR_NE(p1, NULL);
417 p2 = sshbuf_new();
418 ASSERT_PTR_NE(p2, NULL);
419 ASSERT_INT_EQ(sshbuf_put(p1, "blahblahblah", 12), 0);
420 ASSERT_INT_EQ(sshbuf_putb(p2, p1), 0);
421 sshbuf_free(p1);
422 ASSERT_SIZE_T_EQ(sshbuf_len(p2), 12);
423 ASSERT_MEM_EQ(sshbuf_ptr(p2), "blahblahblah", 12);
424 sshbuf_free(p2);
425 TEST_DONE();
426
427 TEST_START("sshbuf_put_bignum2_bytes empty buf");
428 p1 = sshbuf_new();
429 ASSERT_PTR_NE(p1, NULL);
430 ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, NULL, 0), 0);
431 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp1));
432 ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp1, sizeof(bn_exp1));
433 sshbuf_free(p1);
434 TEST_DONE();
435
436 TEST_START("sshbuf_put_bignum2_bytes all zeroes");
437 p1 = sshbuf_new();
438 ASSERT_PTR_NE(p1, NULL);
439 ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn1, sizeof(bn1)), 0);
440 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp1));
441 ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp1, sizeof(bn_exp1));
442 sshbuf_free(p1);
443 TEST_DONE();
444
445 TEST_START("sshbuf_put_bignum2_bytes simple");
446 p1 = sshbuf_new();
447 ASSERT_PTR_NE(p1, NULL);
448 ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn2+2, sizeof(bn2)-2), 0);
449 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp2));
450 ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp2, sizeof(bn_exp2));
451 sshbuf_free(p1);
452 TEST_DONE();
453
454 TEST_START("sshbuf_put_bignum2_bytes leading zero");
455 p1 = sshbuf_new();
456 ASSERT_PTR_NE(p1, NULL);
457 ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn2, sizeof(bn2)), 0);
458 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp2));
459 ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp2, sizeof(bn_exp2));
460 sshbuf_free(p1);
461 TEST_DONE();
462
463 TEST_START("sshbuf_put_bignum2_bytes neg");
464 p1 = sshbuf_new();
465 ASSERT_PTR_NE(p1, NULL);
466 ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn3+1, sizeof(bn3)-1), 0);
467 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp3));
468 ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp3, sizeof(bn_exp3));
469 sshbuf_free(p1);
470 TEST_DONE();
471
472 TEST_START("sshbuf_put_bignum2_bytes neg and leading zero");
473 p1 = sshbuf_new();
474 ASSERT_PTR_NE(p1, NULL);
475 ASSERT_INT_EQ(sshbuf_put_bignum2_bytes(p1, bn3, sizeof(bn3)), 0);
476 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(bn_exp3));
477 ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp3, sizeof(bn_exp3));
478 sshbuf_free(p1);
479 TEST_DONE();
480}