diff options
author | djm@openbsd.org <djm@openbsd.org> | 2019-07-14 23:33:19 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2019-07-15 09:39:42 +1000 |
commit | 121e48fa5305f41f0477d9908e3d862987a68a84 (patch) | |
tree | bf7a23101777affdf04032856484eb7d1c33a0a1 | |
parent | 101d164723ffbc38f8036b6f3ea3bfef771ba250 (diff) |
upstream: unit tests for sshbuf_peek/poke bounds-checked random access
functions. ok markus@
OpenBSD-Regress-ID: 034c4284b1da6b12e25c762a6b958efacdafbaef
-rw-r--r-- | regress/unittests/sshbuf/test_sshbuf_getput_basic.c | 231 |
1 files changed, 230 insertions, 1 deletions
diff --git a/regress/unittests/sshbuf/test_sshbuf_getput_basic.c b/regress/unittests/sshbuf/test_sshbuf_getput_basic.c index 966e8432b..bea89881a 100644 --- a/regress/unittests/sshbuf/test_sshbuf_getput_basic.c +++ b/regress/unittests/sshbuf/test_sshbuf_getput_basic.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: test_sshbuf_getput_basic.c,v 1.1 2014/04/30 05:32:00 djm Exp $ */ | 1 | /* $OpenBSD: test_sshbuf_getput_basic.c,v 1.2 2019/07/14 23:33:19 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Regress test for sshbuf.h buffer API | 3 | * Regress test for sshbuf.h buffer API |
4 | * | 4 | * |
@@ -481,4 +481,233 @@ sshbuf_getput_basic_tests(void) | |||
481 | ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp3, sizeof(bn_exp3)); | 481 | ASSERT_MEM_EQ(sshbuf_ptr(p1), bn_exp3, sizeof(bn_exp3)); |
482 | sshbuf_free(p1); | 482 | sshbuf_free(p1); |
483 | TEST_DONE(); | 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(); | ||
484 | } | 713 | } |