hexstr_test.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. /*
  11. * This program tests the use of OSSL_PARAM, currently in raw form.
  12. */
  13. #include "internal/nelem.h"
  14. #include "internal/cryptlib.h"
  15. #include "testutil.h"
  16. struct testdata
  17. {
  18. const char *in;
  19. const unsigned char *expected;
  20. size_t expected_len;
  21. const char sep;
  22. };
  23. static const unsigned char test_1[] = { 0xAB, 0xCD, 0xEF, 0xF1 };
  24. static const unsigned char test_2[] = { 0xAB, 0xCD, 0xEF, 0x76, 0x00 };
  25. static struct testdata tbl_testdata[] = {
  26. {
  27. "AB:CD:EF:F1",
  28. test_1, sizeof(test_1),
  29. ':',
  30. },
  31. {
  32. "AB:CD:EF:76:00",
  33. test_2, sizeof(test_2),
  34. ':',
  35. },
  36. {
  37. "AB_CD_EF_F1",
  38. test_1, sizeof(test_1),
  39. '_',
  40. },
  41. {
  42. "AB_CD_EF_76_00",
  43. test_2, sizeof(test_2),
  44. '_',
  45. },
  46. {
  47. "ABCDEFF1",
  48. test_1, sizeof(test_1),
  49. '\0',
  50. },
  51. {
  52. "ABCDEF7600",
  53. test_2, sizeof(test_2),
  54. '\0',
  55. },
  56. };
  57. static int test_hexstr_sep_to_from(int test_index)
  58. {
  59. int ret = 0;
  60. long len = 0;
  61. unsigned char *buf = NULL;
  62. char *out = NULL;
  63. struct testdata *test = &tbl_testdata[test_index];
  64. if (!TEST_ptr(buf = ossl_hexstr2buf_sep(test->in, &len, test->sep))
  65. || !TEST_mem_eq(buf, len, test->expected, test->expected_len)
  66. || !TEST_ptr(out = ossl_buf2hexstr_sep(buf, len, test->sep))
  67. || !TEST_str_eq(out, test->in))
  68. goto err;
  69. ret = 1;
  70. err:
  71. OPENSSL_free(buf);
  72. OPENSSL_free(out);
  73. return ret;
  74. }
  75. static int test_hexstr_to_from(int test_index)
  76. {
  77. int ret = 0;
  78. long len = 0;
  79. unsigned char *buf = NULL;
  80. char *out = NULL;
  81. struct testdata *test = &tbl_testdata[test_index];
  82. if (test->sep != '_') {
  83. if (!TEST_ptr(buf = OPENSSL_hexstr2buf(test->in, &len))
  84. || !TEST_mem_eq(buf, len, test->expected, test->expected_len)
  85. || !TEST_ptr(out = OPENSSL_buf2hexstr(buf, len)))
  86. goto err;
  87. if (test->sep == ':') {
  88. if (!TEST_str_eq(out, test->in))
  89. goto err;
  90. } else if (!TEST_str_ne(out, test->in)) {
  91. goto err;
  92. }
  93. } else {
  94. if (!TEST_ptr_null(buf = OPENSSL_hexstr2buf(test->in, &len)))
  95. goto err;
  96. }
  97. ret = 1;
  98. err:
  99. OPENSSL_free(buf);
  100. OPENSSL_free(out);
  101. return ret;
  102. }
  103. static int test_hexstr_ex_to_from(int test_index)
  104. {
  105. size_t len = 0;
  106. char out[64];
  107. unsigned char buf[64];
  108. struct testdata *test = &tbl_testdata[test_index];
  109. return TEST_true(OPENSSL_hexstr2buf_ex(buf, sizeof(buf), &len, test->in, ':'))
  110. && TEST_mem_eq(buf, len, test->expected, test->expected_len)
  111. && TEST_true(OPENSSL_buf2hexstr_ex(out, sizeof(out), NULL, buf, len,
  112. ':'))
  113. && TEST_str_eq(out, test->in);
  114. }
  115. int setup_tests(void)
  116. {
  117. ADD_ALL_TESTS(test_hexstr_sep_to_from, OSSL_NELEM(tbl_testdata));
  118. ADD_ALL_TESTS(test_hexstr_to_from, OSSL_NELEM(tbl_testdata));
  119. ADD_ALL_TESTS(test_hexstr_ex_to_from, 2);
  120. return 1;
  121. }