1 /* $OpenBSD: test_sshbuf_getput_basic.c,v 1.2 2019/07/14 23:33:19 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 TEST_START("sshbuf_peek_u64"); 486 p1 = sshbuf_new(); 487 ASSERT_PTR_NE(p1, NULL); 488 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0); 489 ASSERT_INT_EQ(sshbuf_peek_u64(p1, 0, &v64), 0); 490 ASSERT_U64_EQ(v64, 0x1122334455667788ULL); 491 ASSERT_INT_EQ(sshbuf_peek_u64(p1, 2, &v64), 0); 492 ASSERT_U64_EQ(v64, 0x3344556677880099ULL); 493 ASSERT_INT_EQ(sshbuf_peek_u64(p1, 3, &v64), SSH_ERR_MESSAGE_INCOMPLETE); 494 ASSERT_INT_EQ(sshbuf_peek_u64(p1, sizeof(x), &v64), 495 SSH_ERR_MESSAGE_INCOMPLETE); 496 ASSERT_INT_EQ(sshbuf_peek_u64(p1, 1000, &v64), 497 SSH_ERR_MESSAGE_INCOMPLETE); 498 sshbuf_free(p1); 499 TEST_DONE(); 500 501 TEST_START("sshbuf_peek_u32"); 502 p1 = sshbuf_new(); 503 ASSERT_PTR_NE(p1, NULL); 504 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0); 505 ASSERT_INT_EQ(sshbuf_peek_u32(p1, 0, &v32), 0); 506 ASSERT_U32_EQ(v32, 0x11223344); 507 ASSERT_INT_EQ(sshbuf_peek_u32(p1, 6, &v32), 0); 508 ASSERT_U32_EQ(v32, 0x77880099); 509 ASSERT_INT_EQ(sshbuf_peek_u32(p1, 7, &v32), SSH_ERR_MESSAGE_INCOMPLETE); 510 ASSERT_INT_EQ(sshbuf_peek_u32(p1, sizeof(x), &v32), 511 SSH_ERR_MESSAGE_INCOMPLETE); 512 ASSERT_INT_EQ(sshbuf_peek_u32(p1, 1000, &v32), 513 SSH_ERR_MESSAGE_INCOMPLETE); 514 sshbuf_free(p1); 515 TEST_DONE(); 516 517 TEST_START("sshbuf_peek_u16"); 518 p1 = sshbuf_new(); 519 ASSERT_PTR_NE(p1, NULL); 520 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0); 521 ASSERT_INT_EQ(sshbuf_peek_u16(p1, 0, &v16), 0); 522 ASSERT_U16_EQ(v16, 0x1122); 523 ASSERT_INT_EQ(sshbuf_peek_u16(p1, 8, &v16), 0); 524 ASSERT_U16_EQ(v16, 0x99); 525 ASSERT_INT_EQ(sshbuf_peek_u16(p1, 9, &v16), SSH_ERR_MESSAGE_INCOMPLETE); 526 ASSERT_INT_EQ(sshbuf_peek_u16(p1, sizeof(x), &v16), 527 SSH_ERR_MESSAGE_INCOMPLETE); 528 ASSERT_INT_EQ(sshbuf_peek_u16(p1, 1000, &v16), 529 SSH_ERR_MESSAGE_INCOMPLETE); 530 sshbuf_free(p1); 531 TEST_DONE(); 532 533 TEST_START("sshbuf_peek_u8"); 534 p1 = sshbuf_new(); 535 ASSERT_PTR_NE(p1, NULL); 536 ASSERT_INT_EQ(sshbuf_put(p1, x, sizeof(x)), 0); 537 ASSERT_INT_EQ(sshbuf_peek_u8(p1, 0, &v8), 0); 538 ASSERT_U8_EQ(v8, 0x11); 539 ASSERT_INT_EQ(sshbuf_peek_u8(p1, 9, &v8), 0); 540 ASSERT_U8_EQ(v8, 0x99); 541 ASSERT_INT_EQ(sshbuf_peek_u8(p1, sizeof(x), &v8), 542 SSH_ERR_MESSAGE_INCOMPLETE); 543 ASSERT_INT_EQ(sshbuf_peek_u8(p1, 1000, &v8), 544 SSH_ERR_MESSAGE_INCOMPLETE); 545 sshbuf_free(p1); 546 TEST_DONE(); 547 548 TEST_START("sshbuf_poke_u64"); 549 p1 = sshbuf_new(); 550 ASSERT_PTR_NE(p1, NULL); 551 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 552 /* poke at start of buffer */ 553 ASSERT_INT_EQ(sshbuf_poke_u64(p1, 0, 0xa1b2c3d4e5f60718ULL), 0); 554 s2 = sshbuf_dtob16(p1); 555 ASSERT_PTR_NE(s2, NULL); 556 ASSERT_STRING_EQ(s2, "a1b2c3d4e5f607180000"); 557 free(s2); 558 sshbuf_reset(p1); 559 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 560 /* poke aligned with end of buffer */ 561 ASSERT_INT_EQ(sshbuf_poke_u64(p1, 2, 0xa1b2c3d4e5f60718ULL), 0); 562 s2 = sshbuf_dtob16(p1); 563 ASSERT_PTR_NE(s2, NULL); 564 ASSERT_STRING_EQ(s2, "0000a1b2c3d4e5f60718"); 565 free(s2); 566 sshbuf_reset(p1); 567 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 568 /* poke past end of buffer */ 569 ASSERT_INT_EQ(sshbuf_poke_u64(p1, 3, 0xa1b2c3d4e5f60718ULL), 570 SSH_ERR_NO_BUFFER_SPACE); 571 ASSERT_INT_EQ(sshbuf_poke_u64(p1, 10, 0xa1b2c3d4e5f60718ULL), 572 SSH_ERR_NO_BUFFER_SPACE); 573 ASSERT_INT_EQ(sshbuf_poke_u64(p1, 1000, 0xa1b2c3d4e5f60718ULL), 574 SSH_ERR_NO_BUFFER_SPACE); 575 /* ensure failed pokes do not modify buffer */ 576 s2 = sshbuf_dtob16(p1); 577 ASSERT_PTR_NE(s2, NULL); 578 ASSERT_STRING_EQ(s2, "00000000000000000000"); 579 sshbuf_free(p1); 580 TEST_DONE(); 581 582 TEST_START("sshbuf_poke_u32"); 583 p1 = sshbuf_new(); 584 ASSERT_PTR_NE(p1, NULL); 585 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 586 /* poke at start of buffer */ 587 ASSERT_INT_EQ(sshbuf_poke_u32(p1, 0, 0xa1b2c3d4), 0); 588 s2 = sshbuf_dtob16(p1); 589 ASSERT_PTR_NE(s2, NULL); 590 ASSERT_STRING_EQ(s2, "a1b2c3d4000000000000"); 591 free(s2); 592 sshbuf_reset(p1); 593 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 594 /* poke aligned with end of buffer */ 595 ASSERT_INT_EQ(sshbuf_poke_u32(p1, 6, 0xa1b2c3d4), 0); 596 s2 = sshbuf_dtob16(p1); 597 ASSERT_PTR_NE(s2, NULL); 598 ASSERT_STRING_EQ(s2, "000000000000a1b2c3d4"); 599 free(s2); 600 sshbuf_reset(p1); 601 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 602 /* poke past end of buffer */ 603 ASSERT_INT_EQ(sshbuf_poke_u32(p1, 7, 0xa1b2c3d4), 604 SSH_ERR_NO_BUFFER_SPACE); 605 ASSERT_INT_EQ(sshbuf_poke_u32(p1, 10, 0xa1b2c3d4), 606 SSH_ERR_NO_BUFFER_SPACE); 607 ASSERT_INT_EQ(sshbuf_poke_u32(p1, 1000, 0xa1b2c3d4), 608 SSH_ERR_NO_BUFFER_SPACE); 609 /* ensure failed pokes do not modify buffer */ 610 s2 = sshbuf_dtob16(p1); 611 ASSERT_PTR_NE(s2, NULL); 612 ASSERT_STRING_EQ(s2, "00000000000000000000"); 613 sshbuf_free(p1); 614 TEST_DONE(); 615 616 TEST_START("sshbuf_poke_u16"); 617 p1 = sshbuf_new(); 618 ASSERT_PTR_NE(p1, NULL); 619 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 620 /* poke at start of buffer */ 621 ASSERT_INT_EQ(sshbuf_poke_u16(p1, 0, 0xa1b2), 0); 622 s2 = sshbuf_dtob16(p1); 623 ASSERT_PTR_NE(s2, NULL); 624 ASSERT_STRING_EQ(s2, "a1b20000000000000000"); 625 free(s2); 626 sshbuf_reset(p1); 627 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 628 /* poke aligned with end of buffer */ 629 ASSERT_INT_EQ(sshbuf_poke_u16(p1, 8, 0xa1b2), 0); 630 s2 = sshbuf_dtob16(p1); 631 ASSERT_PTR_NE(s2, NULL); 632 ASSERT_STRING_EQ(s2, "0000000000000000a1b2"); 633 free(s2); 634 sshbuf_reset(p1); 635 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 636 /* poke past end of buffer */ 637 ASSERT_INT_EQ(sshbuf_poke_u16(p1, 9, 0xa1b2), 638 SSH_ERR_NO_BUFFER_SPACE); 639 ASSERT_INT_EQ(sshbuf_poke_u16(p1, 10, 0xa1b2), 640 SSH_ERR_NO_BUFFER_SPACE); 641 ASSERT_INT_EQ(sshbuf_poke_u16(p1, 1000, 0xa1b2), 642 SSH_ERR_NO_BUFFER_SPACE); 643 /* ensure failed pokes do not modify buffer */ 644 s2 = sshbuf_dtob16(p1); 645 ASSERT_PTR_NE(s2, NULL); 646 ASSERT_STRING_EQ(s2, "00000000000000000000"); 647 sshbuf_free(p1); 648 TEST_DONE(); 649 650 TEST_START("sshbuf_poke_u8"); 651 p1 = sshbuf_new(); 652 ASSERT_PTR_NE(p1, NULL); 653 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 654 /* poke at start of buffer */ 655 ASSERT_INT_EQ(sshbuf_poke_u8(p1, 0, 0xa1), 0); 656 s2 = sshbuf_dtob16(p1); 657 ASSERT_PTR_NE(s2, NULL); 658 ASSERT_STRING_EQ(s2, "a1000000000000000000"); 659 free(s2); 660 sshbuf_reset(p1); 661 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 662 /* poke aligned with end of buffer */ 663 ASSERT_INT_EQ(sshbuf_poke_u8(p1, 9, 0xa1), 0); 664 s2 = sshbuf_dtob16(p1); 665 ASSERT_PTR_NE(s2, NULL); 666 ASSERT_STRING_EQ(s2, "000000000000000000a1"); 667 free(s2); 668 sshbuf_reset(p1); 669 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 670 /* poke past end of buffer */ 671 ASSERT_INT_EQ(sshbuf_poke_u8(p1, 10, 0xa1), SSH_ERR_NO_BUFFER_SPACE); 672 ASSERT_INT_EQ(sshbuf_poke_u8(p1, 1000, 0xa1), SSH_ERR_NO_BUFFER_SPACE); 673 /* ensure failed pokes do not modify buffer */ 674 s2 = sshbuf_dtob16(p1); 675 ASSERT_PTR_NE(s2, NULL); 676 ASSERT_STRING_EQ(s2, "00000000000000000000"); 677 sshbuf_free(p1); 678 TEST_DONE(); 679 680 TEST_START("sshbuf_poke"); 681 p1 = sshbuf_new(); 682 ASSERT_PTR_NE(p1, NULL); 683 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 684 /* poke at start of buffer */ 685 ASSERT_INT_EQ(sshbuf_poke(p1, 0, "hello!", 6), 0); 686 s2 = sshbuf_dtob16(p1); 687 ASSERT_PTR_NE(s2, NULL); 688 ASSERT_STRING_EQ(s2, "68656c6c6f2100000000"); 689 free(s2); 690 sshbuf_reset(p1); 691 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 692 /* poke aligned with end of buffer */ 693 ASSERT_INT_EQ(sshbuf_poke(p1, 4, "hello!", 6), 0); 694 s2 = sshbuf_dtob16(p1); 695 ASSERT_PTR_NE(s2, NULL); 696 ASSERT_STRING_EQ(s2, "0000000068656c6c6f21"); 697 free(s2); 698 sshbuf_reset(p1); 699 ASSERT_INT_EQ(sshbuf_reserve(p1, 10, NULL), 0); 700 /* poke past end of buffer */ 701 ASSERT_INT_EQ(sshbuf_poke(p1, 7, "hello!", 6), 702 SSH_ERR_NO_BUFFER_SPACE); 703 ASSERT_INT_EQ(sshbuf_poke(p1, 10, "hello!", 6), 704 SSH_ERR_NO_BUFFER_SPACE); 705 ASSERT_INT_EQ(sshbuf_poke(p1, 1000, "hello!", 6), 706 SSH_ERR_NO_BUFFER_SPACE); 707 /* ensure failed pokes do not modify buffer */ 708 s2 = sshbuf_dtob16(p1); 709 ASSERT_PTR_NE(s2, NULL); 710 ASSERT_STRING_EQ(s2, "00000000000000000000"); 711 sshbuf_free(p1); 712 TEST_DONE(); 713 } 714