summaryrefslogtreecommitdiff
path: root/tests/protocol_unittest.cpp
blob: 76de58b1954d00e43431db3c728766e096bfe89a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2016, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cstddef>
#include <type_traits>

#include "gtest/gtest.h"

#include "wifilogd/protocol.h"

namespace android {
namespace wifilogd {

// The protocol tests aim to provide friction against changes that
// break byte-stream compatibility. Byte-stream compatibility is
// important for two reasons:
//     1. We need to support non-C++ clients. Those clients may use bindings
//        that are implemented in their own language, rather than using a
//        C or C++ client library.
//     2. We need to maintain compatibility with older clients talking to
//        newer versions of wifilogd.

TEST(ProtocolTest, AsciiMessageLayoutIsUnchanged) {
  using protocol::AsciiMessage;
  ASSERT_TRUE(std::is_standard_layout<AsciiMessage>::value);

  EXPECT_EQ(0U, offsetof(AsciiMessage, data_len));
  EXPECT_EQ(2U, sizeof(AsciiMessage::data_len));

  EXPECT_EQ(2U, offsetof(AsciiMessage, tag_len));
  EXPECT_EQ(1U, sizeof(AsciiMessage::tag_len));

  EXPECT_EQ(3U, offsetof(AsciiMessage, severity));
  EXPECT_EQ(1U, sizeof(AsciiMessage::severity));

  EXPECT_EQ(4U, sizeof(AsciiMessage));
}

TEST(ProtocolTest, CommandLayoutIsUnchanged) {
  using protocol::Command;
  ASSERT_TRUE(std::is_standard_layout<Command>::value);

  EXPECT_EQ(0U, offsetof(Command, src_boottime_nsec));
  EXPECT_EQ(8U, sizeof(Command::src_boottime_nsec));

  EXPECT_EQ(8U, offsetof(Command, sequence_num));
  EXPECT_EQ(2U, sizeof(Command::sequence_num));

  EXPECT_EQ(10U, offsetof(Command, opcode));
  EXPECT_EQ(2U, sizeof(Command::opcode));

  EXPECT_EQ(12U, offsetof(Command, payload_len));
  EXPECT_EQ(2U, sizeof(Command::payload_len));

  // The |reserved| field fills out Command, in place of padding that
  // would otherwise be added by the compiler.
  EXPECT_EQ(14U, offsetof(Command, reserved));
  EXPECT_EQ(2U, sizeof(Command::reserved));

  EXPECT_EQ(16U, sizeof(Command));
}

TEST(ProtocolTest, MaxMessageSizeHasNotShrunk) {
  EXPECT_GE(protocol::kMaxMessageSize, 4096U);
}

TEST(ProtocolTest, MessageSeveritiesAreUnchanged) {
  using protocol::MessageSeverity;
  EXPECT_EQ(0U, static_cast<uint8_t>(MessageSeverity::kError));
  EXPECT_EQ(1U, static_cast<uint8_t>(MessageSeverity::kWarning));
  EXPECT_EQ(2U, static_cast<uint8_t>(MessageSeverity::kInformational));
  EXPECT_EQ(3U, static_cast<uint8_t>(MessageSeverity::kTrace));
  EXPECT_EQ(4U, static_cast<uint8_t>(MessageSeverity::kDump));
}

TEST(ProtocolTest, OpcodesAreUnchanged) {
  using protocol::Opcode;
  EXPECT_EQ(2U, sizeof(Opcode));
  EXPECT_EQ(0U, static_cast<uint16_t>(Opcode::kWriteAsciiMessage));
  EXPECT_EQ(0x20U, static_cast<uint16_t>(Opcode::kDumpBuffers));
}

}  // namespace wifilogd
}  // namespace android