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,AsciiMessageChainingWorks)36 TEST(ProtocolTest, AsciiMessageChainingWorks) {
37   using protocol::AsciiMessage;
38   uint8_t tagLen = 3;
39   uint16_t dataLen = 7;
40   const auto ascii_message_header =
41       AsciiMessage().set_tag_len(tagLen).set_data_len(dataLen);
42   EXPECT_EQ(tagLen, ascii_message_header.tag_len);
43   EXPECT_EQ(dataLen, ascii_message_header.data_len);
44 }
45 
TEST(ProtocolTest,AsciiMessageNonChainingWorks)46 TEST(ProtocolTest, AsciiMessageNonChainingWorks) {
47   using protocol::AsciiMessage;
48   uint8_t tagLen = 3;
49   uint16_t dataLen = 7;
50   AsciiMessage ascii_message_header = AsciiMessage();
51   ascii_message_header.set_tag_len(tagLen);
52   ascii_message_header.set_data_len(dataLen);
53   EXPECT_EQ(tagLen, ascii_message_header.tag_len);
54   EXPECT_EQ(dataLen, ascii_message_header.data_len);
55 }
56 
TEST(ProtocolTest,AsciiMessageLayoutIsUnchanged)57 TEST(ProtocolTest, AsciiMessageLayoutIsUnchanged) {
58   using protocol::AsciiMessage;
59   ASSERT_TRUE(std::is_standard_layout<AsciiMessage>::value);
60 
61   EXPECT_EQ(0U, offsetof(AsciiMessage, data_len));
62   EXPECT_EQ(2U, sizeof(AsciiMessage::data_len));
63 
64   EXPECT_EQ(2U, offsetof(AsciiMessage, tag_len));
65   EXPECT_EQ(1U, sizeof(AsciiMessage::tag_len));
66 
67   EXPECT_EQ(3U, offsetof(AsciiMessage, severity));
68   EXPECT_EQ(1U, sizeof(AsciiMessage::severity));
69 
70   EXPECT_EQ(4U, sizeof(AsciiMessage));
71 }
72 
TEST(ProtocolTest,CommandLayoutIsUnchanged)73 TEST(ProtocolTest, CommandLayoutIsUnchanged) {
74   using protocol::Command;
75   ASSERT_TRUE(std::is_standard_layout<Command>::value);
76 
77   EXPECT_EQ(0U, offsetof(Command, src_boottime_nsec));
78   EXPECT_EQ(8U, sizeof(Command::src_boottime_nsec));
79 
80   EXPECT_EQ(8U, offsetof(Command, sequence_num));
81   EXPECT_EQ(2U, sizeof(Command::sequence_num));
82 
83   EXPECT_EQ(10U, offsetof(Command, opcode));
84   EXPECT_EQ(2U, sizeof(Command::opcode));
85 
86   EXPECT_EQ(12U, offsetof(Command, payload_len));
87   EXPECT_EQ(2U, sizeof(Command::payload_len));
88 
89   // The |reserved| field fills out Command, in place of padding that
90   // would otherwise be added by the compiler.
91   EXPECT_EQ(14U, offsetof(Command, reserved));
92   EXPECT_EQ(2U, sizeof(Command::reserved));
93 
94   EXPECT_EQ(16U, sizeof(Command));
95 }
96 
TEST(ProtocolTest,MaxMessageSizeHasNotShrunk)97 TEST(ProtocolTest, MaxMessageSizeHasNotShrunk) {
98   EXPECT_GE(protocol::kMaxMessageSize, 4096U);
99 }
100 
TEST(ProtocolTest,MessageSeveritiesAreUnchanged)101 TEST(ProtocolTest, MessageSeveritiesAreUnchanged) {
102   using protocol::MessageSeverity;
103   EXPECT_EQ(0U, static_cast<uint8_t>(MessageSeverity::kError));
104   EXPECT_EQ(1U, static_cast<uint8_t>(MessageSeverity::kWarning));
105   EXPECT_EQ(2U, static_cast<uint8_t>(MessageSeverity::kInformational));
106   EXPECT_EQ(3U, static_cast<uint8_t>(MessageSeverity::kTrace));
107   EXPECT_EQ(4U, static_cast<uint8_t>(MessageSeverity::kDump));
108 }
109 
TEST(ProtocolTest,OpcodesAreUnchanged)110 TEST(ProtocolTest, OpcodesAreUnchanged) {
111   using protocol::Opcode;
112   EXPECT_EQ(2U, sizeof(Opcode));
113   EXPECT_EQ(0U, static_cast<uint16_t>(Opcode::kWriteAsciiMessage));
114   EXPECT_EQ(0x20U, static_cast<uint16_t>(Opcode::kDumpBuffers));
115 }
116 
117 }  // namespace wifilogd
118 }  // namespace android
119