byteorder_test.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <string.h>
  2. #include <openssl/e_os2.h>
  3. #include <openssl/byteorder.h>
  4. #include "testutil.h"
  5. #include "testutil/output.h"
  6. static int test_byteorder(void)
  7. {
  8. const unsigned char in[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  9. unsigned char out[8];
  10. const unsigned char *restin;
  11. unsigned char *restout;
  12. uint16_t u16;
  13. uint32_t u32;
  14. uint64_t u64;
  15. memset(out, 0xff, sizeof(out));
  16. restin = OPENSSL_load_u16_le(&u16, in);
  17. restout = OPENSSL_store_u16_le(out, u16);
  18. if (!TEST_true(u16 == 0x0100U
  19. && memcmp(in, out, (size_t) 2) == 0
  20. && restin == in + 2
  21. && restout == out + 2)) {
  22. TEST_info("Failed byteorder.h u16 LE load/store");
  23. return 0;
  24. }
  25. memset(out, 0xff, sizeof(out));
  26. restin = OPENSSL_load_u16_be(&u16, in);
  27. restout = OPENSSL_store_u16_be(out, u16);
  28. if (!TEST_true(u16 == 0x0001U
  29. && memcmp(in, out, (size_t) 2) == 0
  30. && restin == in + 2
  31. && restout == out + 2)) {
  32. TEST_info("Failed byteorder.h u16 BE load/store");
  33. return 0;
  34. }
  35. memset(out, 0xff, sizeof(out));
  36. restin = OPENSSL_load_u32_le(&u32, in);
  37. restout = OPENSSL_store_u32_le(out, u32);
  38. if (!TEST_true(u32 == 0x03020100UL
  39. && memcmp(in, out, (size_t) 4) == 0
  40. && restin == in + 4
  41. && restout == out + 4)) {
  42. TEST_info("Failed byteorder.h u32 LE load/store");
  43. return 0;
  44. }
  45. memset(out, 0xff, sizeof(out));
  46. restin = OPENSSL_load_u32_be(&u32, in);
  47. restout = OPENSSL_store_u32_be(out, u32);
  48. if (!TEST_true(u32 == 0x00010203UL
  49. && memcmp(in, out, (size_t) 4) == 0
  50. && restin == in + 4
  51. && restout == out + 4)) {
  52. TEST_info("Failed byteorder.h u32 BE load/store");
  53. return 0;
  54. }
  55. memset(out, 0xff, sizeof(out));
  56. restin = OPENSSL_load_u64_le(&u64, in);
  57. restout = OPENSSL_store_u64_le(out, u64);
  58. if (!TEST_true(u64 == 0x0706050403020100ULL
  59. && memcmp(in, out, (size_t) 8) == 0
  60. && restin == in + 8
  61. && restout == out + 8)) {
  62. TEST_info("Failed byteorder.h u64 LE load/store");
  63. return 0;
  64. }
  65. memset(out, 0xff, sizeof(out));
  66. restin = OPENSSL_load_u64_be(&u64, in);
  67. restout = OPENSSL_store_u64_be(out, u64);
  68. if (!TEST_true(u64 == 0x0001020304050607ULL
  69. && memcmp(in, out, (size_t) 8) == 0
  70. && restin == in + 8
  71. && restout == out + 8)) {
  72. TEST_info("Failed byteorder.h u64 BE load/store");
  73. return 0;
  74. }
  75. return 1;
  76. }
  77. int setup_tests(void)
  78. {
  79. ADD_TEST(test_byteorder);
  80. return 1;
  81. }