xref: /freebsd/crypto/krb5/src/util/support/t_unal.c (revision b670c9bafc0e31c7609969bf374b2e80bdc00211)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #undef NDEBUG
3 #include "k5-platform.h"
4 
5 int main ()
6 {
7     /* Test some low-level assumptions the Kerberos code depends
8        on.  */
9 
10     union {
11         uint64_t n64;
12         uint32_t n32;
13         uint16_t n16;
14         unsigned char b[9];
15     } u;
16     static unsigned char buf[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
17 
18     assert(load_64_be(buf+1) == 0x0102030405060708LL);
19     assert(load_64_le(buf+1) == 0x0807060504030201LL);
20     assert(load_32_le(buf+2) == 0x05040302);
21     assert(load_32_be(buf+2) == 0x02030405);
22     assert(load_16_be(buf+3) == 0x0304);
23     assert(load_16_le(buf+3) == 0x0403);
24     u.b[0] = 0;
25     assert((store_64_be(0x0102030405060708LL, u.b+1), !memcmp(buf, u.b, 9)));
26     u.b[1] = 9;
27     assert((store_64_le(0x0807060504030201LL, u.b+1), !memcmp(buf, u.b, 9)));
28     u.b[2] = 10;
29     assert((store_32_be(0x02030405, u.b+2), !memcmp(buf, u.b, 9)));
30     u.b[3] = 11;
31     assert((store_32_le(0x05040302, u.b+2), !memcmp(buf, u.b, 9)));
32     u.b[4] = 12;
33     assert((store_16_be(0x0304, u.b+3), !memcmp(buf, u.b, 9)));
34     u.b[4] = 13;
35     assert((store_16_le(0x0403, u.b+3), !memcmp(buf, u.b, 9)));
36     /* Verify that load_*_n properly does native format.  Assume
37        the unaligned thing is okay.  */
38     u.n64 = 0x090a0b0c0d0e0f00LL;
39     assert(load_64_n((unsigned char *) &u.n64) == 0x090a0b0c0d0e0f00LL);
40     u.n32 = 0x06070809;
41     assert(load_32_n((unsigned char *) &u.n32) == 0x06070809);
42     u.n16 = 0x0a0b;
43     assert(load_16_n((unsigned char *) &u.n16) == 0x0a0b);
44 
45     return 0;
46 }
47