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 <cstddef>
18 #include <type_traits>
19
20 #include "gtest/gtest.h"
21
22 #include "wifilogd/protocol.h"
23
24 namespace android {
25 namespace wifilogd {
26
27 // The protocol tests aim to provide friction against changes that
28 // break byte-stream compatibility. Byte-stream compatibility is
29 // important for two reasons:
30 // 1. We need to support non-C++ clients. Those clients may use bindings
31 // that are implemented in their own language, rather than using a
32 // C or C++ client library.
33 // 2. We need to maintain compatibility with older clients talking to
34 // newer versions of wifilogd.
35
TEST(ProtocolTest,AsciiMessageLayoutIsUnchanged)36 TEST(ProtocolTest, AsciiMessageLayoutIsUnchanged) {
37 using protocol::AsciiMessage;
38 ASSERT_TRUE(std::is_standard_layout<AsciiMessage>::value);
39
40 EXPECT_EQ(0U, offsetof(AsciiMessage, data_len));
41 EXPECT_EQ(2U, sizeof(AsciiMessage::data_len));
42
43 EXPECT_EQ(2U, offsetof(AsciiMessage, tag_len));
44 EXPECT_EQ(1U, sizeof(AsciiMessage::tag_len));
45
46 EXPECT_EQ(3U, offsetof(AsciiMessage, severity));
47 EXPECT_EQ(1U, sizeof(AsciiMessage::severity));
48
49 EXPECT_EQ(4U, sizeof(AsciiMessage));
50 }
51
TEST(ProtocolTest,CommandLayoutIsUnchanged)52 TEST(ProtocolTest, CommandLayoutIsUnchanged) {
53 using protocol::Command;
54 ASSERT_TRUE(std::is_standard_layout<Command>::value);
55
56 EXPECT_EQ(0U, offsetof(Command, src_boottime_nsec));
57 EXPECT_EQ(8U, sizeof(Command::src_boottime_nsec));
58
59 EXPECT_EQ(8U, offsetof(Command, sequence_num));
60 EXPECT_EQ(2U, sizeof(Command::sequence_num));
61
62 EXPECT_EQ(10U, offsetof(Command, opcode));
63 EXPECT_EQ(2U, sizeof(Command::opcode));
64
65 EXPECT_EQ(12U, offsetof(Command, payload_len));
66 EXPECT_EQ(2U, sizeof(Command::payload_len));
67
68 // The |reserved| field fills out Command, in place of padding that
69 // would otherwise be added by the compiler.
70 EXPECT_EQ(14U, offsetof(Command, reserved));
71 EXPECT_EQ(2U, sizeof(Command::reserved));
72
73 EXPECT_EQ(16U, sizeof(Command));
74 }
75
TEST(ProtocolTest,MaxMessageSizeHasNotShrunk)76 TEST(ProtocolTest, MaxMessageSizeHasNotShrunk) {
77 EXPECT_GE(protocol::kMaxMessageSize, 4096U);
78 }
79
TEST(ProtocolTest,MessageSeveritiesAreUnchanged)80 TEST(ProtocolTest, MessageSeveritiesAreUnchanged) {
81 using protocol::MessageSeverity;
82 EXPECT_EQ(0U, static_cast<uint8_t>(MessageSeverity::kError));
83 EXPECT_EQ(1U, static_cast<uint8_t>(MessageSeverity::kWarning));
84 EXPECT_EQ(2U, static_cast<uint8_t>(MessageSeverity::kInformational));
85 EXPECT_EQ(3U, static_cast<uint8_t>(MessageSeverity::kTrace));
86 EXPECT_EQ(4U, static_cast<uint8_t>(MessageSeverity::kDump));
87 }
88
TEST(ProtocolTest,OpcodesAreUnchanged)89 TEST(ProtocolTest, OpcodesAreUnchanged) {
90 using protocol::Opcode;
91 EXPECT_EQ(2U, sizeof(Opcode));
92 EXPECT_EQ(0U, static_cast<uint16_t>(Opcode::kWriteAsciiMessage));
93 EXPECT_EQ(0x20U, static_cast<uint16_t>(Opcode::kDumpBuffers));
94 }
95
96 } // namespace wifilogd
97 } // namespace android
98