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