1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 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  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <shared/nano_endian.h>
18 
19 #include <cstdint>
20 #include <cstring>
21 
22 #include <gtest/gtest.h>
23 #include <shared/array_length.h>
24 
25 
26 template<size_t kByteCount>
swapByteTest()27 static void swapByteTest() {
28   uint8_t bytes[] = {
29     0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
30     0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 };
31   static constexpr uint8_t postSwap[] = {
32     0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09,
33     0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
34 
35 #ifdef __clang__
36   // This static_assert crashes g++, but it's legit and works in clang.
37   static_assert(arrayLength(bytes) == arrayLength(postSwap),
38                 "Mismatched arrays");
39 #endif
40   static_assert((kByteCount > 0) && (kByteCount <= arrayLength(bytes)),
41                 "Invalid test");
42 
43   constexpr const uint8_t *kExpected =
44       postSwap + (arrayLength(postSwap) - kByteCount);
45   nanoapp_testing::swapBytes(bytes, kByteCount);
46   EXPECT_EQ(0, ::memcmp(bytes, kExpected, kByteCount));
47 
48   if (arrayLength(bytes) < kByteCount) {
49     // Confirm that we didn't modify out of bounds.
50     EXPECT_EQ(kByteCount + 1, bytes[kByteCount]);
51   }
52 }
53 
54 
TEST(EndianTest,SwapBytes1)55 TEST(EndianTest, SwapBytes1) {
56   swapByteTest<1>();
57 }
58 
TEST(EndianTest,SwapBytes2)59 TEST(EndianTest, SwapBytes2) {
60   swapByteTest<2>();
61 }
62 
TEST(EndianTest,SwapBytes4)63 TEST(EndianTest, SwapBytes4) {
64   swapByteTest<4>();
65 }
66 
TEST(EndianTest,SwapBytes8)67 TEST(EndianTest, SwapBytes8) {
68   swapByteTest<8>();
69 }
70 
TEST(EndianTest,SwapBytes16)71 TEST(EndianTest, SwapBytes16) {
72   swapByteTest<16>();
73 }
74 
75 
76 // These tests should work regardless of which endian platform this
77 // test happens to be built and running on.
78 
79 static constexpr uint32_t kValue = UINT32_C(0x04030201);
80 static constexpr uint8_t kLittleEndianRepresentation[4] = {
81   0x01, 0x02, 0x03, 0x04 };
82 
TEST(EndianTest,LittleEndianToHost)83 TEST(EndianTest, LittleEndianToHost) {
84   uint32_t value;
85   ::memcpy(&value, kLittleEndianRepresentation, sizeof(value));
86 
87   value = nanoapp_testing::littleEndianToHost(value);
88   EXPECT_EQ(kValue, value);
89 }
90 
TEST(EndianTest,HostToLittleEndian)91 TEST(EndianTest, HostToLittleEndian) {
92   uint32_t value = kValue;
93   value = nanoapp_testing::hostToLittleEndian(value);
94 
95   const uint8_t *bytes = reinterpret_cast<uint8_t*>(&value);
96   EXPECT_EQ(0, ::memcmp(kLittleEndianRepresentation, bytes,
97                         sizeof(kLittleEndianRepresentation)));
98 }
99