check_base64.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <stdlib.h> // EXIT_SUCCESS
  2. #include "util/ua_base64.h"
  3. #include "check.h"
  4. START_TEST(base64_test_2padding)
  5. {
  6. //this is base64'd ASCII string "open62541!"
  7. UA_String encodedString = UA_STRING_STATIC("b3BlbjYyNTQxIQ==");
  8. //assure that we allocate exactly 10 bytes
  9. ck_assert_int_eq(UA_base64_getDecodedSize(&encodedString), 10);
  10. UA_Byte* decodedData = (UA_Byte*)malloc(UA_base64_getDecodedSize(&encodedString));
  11. UA_base64_decode(&encodedString, decodedData);
  12. //check the string
  13. ck_assert_int_eq(decodedData[0], 'o');
  14. ck_assert_int_eq(decodedData[1], 'p');
  15. ck_assert_int_eq(decodedData[2], 'e');
  16. ck_assert_int_eq(decodedData[3], 'n');
  17. ck_assert_int_eq(decodedData[4], '6');
  18. ck_assert_int_eq(decodedData[5], '2');
  19. ck_assert_int_eq(decodedData[6], '5');
  20. ck_assert_int_eq(decodedData[7], '4');
  21. ck_assert_int_eq(decodedData[8], '1');
  22. ck_assert_int_eq(decodedData[9], '!');
  23. free(decodedData);
  24. }
  25. END_TEST
  26. START_TEST(base64_test_1padding)
  27. {
  28. //this is base64'd ASCII string "open62541!!"
  29. UA_String encodedString = UA_STRING_STATIC("b3BlbjYyNTQxISE=");
  30. //assure that we allocate exactly 11 bytes
  31. ck_assert_int_eq(UA_base64_getDecodedSize(&encodedString), 11);
  32. UA_Byte* decodedData = (UA_Byte*)malloc(UA_base64_getDecodedSize(&encodedString));
  33. UA_base64_decode(&encodedString, decodedData);
  34. //check the string
  35. ck_assert_int_eq(decodedData[0], 'o');
  36. ck_assert_int_eq(decodedData[1], 'p');
  37. ck_assert_int_eq(decodedData[2], 'e');
  38. ck_assert_int_eq(decodedData[3], 'n');
  39. ck_assert_int_eq(decodedData[4], '6');
  40. ck_assert_int_eq(decodedData[5], '2');
  41. ck_assert_int_eq(decodedData[6], '5');
  42. ck_assert_int_eq(decodedData[7], '4');
  43. ck_assert_int_eq(decodedData[8], '1');
  44. ck_assert_int_eq(decodedData[9], '!');
  45. ck_assert_int_eq(decodedData[10], '!');
  46. free(decodedData);
  47. }
  48. END_TEST
  49. START_TEST(base64_test_0padding)
  50. {
  51. //this is base64'd ASCII string "open62541"
  52. UA_String encodedString = UA_STRING_STATIC("b3BlbjYyNTQx");
  53. //assure that we allocate exactly 9 bytes
  54. ck_assert_int_eq(UA_base64_getDecodedSize(&encodedString), 9);
  55. UA_Byte* decodedData = (UA_Byte*)malloc(UA_base64_getDecodedSize(&encodedString));
  56. UA_base64_decode(&encodedString, decodedData);
  57. //check the string
  58. ck_assert_int_eq(decodedData[0], 'o');
  59. ck_assert_int_eq(decodedData[1], 'p');
  60. ck_assert_int_eq(decodedData[2], 'e');
  61. ck_assert_int_eq(decodedData[3], 'n');
  62. ck_assert_int_eq(decodedData[4], '6');
  63. ck_assert_int_eq(decodedData[5], '2');
  64. ck_assert_int_eq(decodedData[6], '5');
  65. ck_assert_int_eq(decodedData[7], '4');
  66. ck_assert_int_eq(decodedData[8], '1');
  67. free(decodedData);
  68. }
  69. END_TEST
  70. Suite*base64_testSuite(void)
  71. {
  72. Suite *s = suite_create("base64_test");
  73. TCase *tc_core = tcase_create("Core");
  74. tcase_add_test(tc_core, base64_test_2padding);
  75. tcase_add_test(tc_core, base64_test_1padding);
  76. tcase_add_test(tc_core, base64_test_0padding);
  77. suite_add_tcase(s,tc_core);
  78. return s;
  79. }
  80. int main (void)
  81. {
  82. int number_failed = 0;
  83. Suite* s = base64_testSuite();
  84. SRunner* sr = srunner_create(s);
  85. srunner_run_all(sr,CK_NORMAL);
  86. number_failed += srunner_ntests_failed(sr);
  87. srunner_free(sr);
  88. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  89. }