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