From 7e3263d995a25e6545dbddea63573a357de310cc Mon Sep 17 00:00:00 2001 From: Simon Josefsson Date: Sun, 20 Aug 2023 21:27:58 +0200 Subject: [PATCH] tests: Check buffer bignum behaviour. Signed-off-by: Simon Josefsson Signed-off-by: Jakub Jelen Reviewed-by: Andreas Schneider Reviewed-by: Sahana Prasad --- tests/unittests/torture_buffer.c | 61 ++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/tests/unittests/torture_buffer.c b/tests/unittests/torture_buffer.c index 8f732d76..c168036f 100644 --- a/tests/unittests/torture_buffer.c +++ b/tests/unittests/torture_buffer.c @@ -259,17 +259,64 @@ static void torture_buffer_pack_badformat(void **state){ * it could crash the process */ } +static void torture_ssh_buffer_bignum(void **state) +{ + ssh_buffer buffer = NULL; + bignum num = NULL; + int rc; + size_t len; + uint8_t verif[] = "\x00\x00\x00\x04" /* len 4 byte */ + "\x00\x00\x00\xff" /* padded 255 */ + "\x00\x00\x00\x02" /* len 2 byte */ + "\x00\xff"; /* padded 255 */ + + (void)state; + + buffer = ssh_buffer_new(); + assert_non_null(buffer); + + num = bignum_new(); + assert_non_null(num); + assert_int_equal(1, bignum_set_word(num, 255)); + + rc = ssh_buffer_pack(buffer, "FB", num, (size_t)4, num); + assert_int_equal(rc, SSH_OK); + + bignum_safe_free(num); + + len = ssh_buffer_get_len(buffer); + assert_int_equal(len, sizeof(verif) - 1); + assert_memory_equal(ssh_buffer_get(buffer), verif, sizeof(verif) - 1); + + SSH_BUFFER_FREE(buffer); +} + int torture_run_tests(void) { int rc; struct CMUnitTest tests[] = { - cmocka_unit_test_setup_teardown(torture_growing_buffer, setup, teardown), - cmocka_unit_test_setup_teardown(torture_growing_buffer_shifting, setup, teardown), - cmocka_unit_test_setup_teardown(torture_buffer_prepend, setup, teardown), + cmocka_unit_test_setup_teardown(torture_growing_buffer, + setup, + teardown), + cmocka_unit_test_setup_teardown(torture_growing_buffer_shifting, + setup, + teardown), + cmocka_unit_test_setup_teardown(torture_buffer_prepend, + setup, + teardown), cmocka_unit_test(torture_ssh_buffer_get_ssh_string), - cmocka_unit_test_setup_teardown(torture_ssh_buffer_add_format, setup, teardown), - cmocka_unit_test_setup_teardown(torture_ssh_buffer_get_format, setup, teardown), - cmocka_unit_test_setup_teardown(torture_ssh_buffer_get_format_error, setup, teardown), - cmocka_unit_test_setup_teardown(torture_buffer_pack_badformat, setup, teardown) + cmocka_unit_test_setup_teardown(torture_ssh_buffer_add_format, + setup, + teardown), + cmocka_unit_test_setup_teardown(torture_ssh_buffer_get_format, + setup, + teardown), + cmocka_unit_test_setup_teardown(torture_ssh_buffer_get_format_error, + setup, + teardown), + cmocka_unit_test_setup_teardown(torture_buffer_pack_badformat, + setup, + teardown), + cmocka_unit_test(torture_ssh_buffer_bignum), }; ssh_init();