blob: 36cc7f26642ac0a27e3f14d2f0e2b9ba0303ef4e [file] [log] [blame]
Jakub Pawlowski103b2c42017-04-28 14:59:46 -07001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2017 The Android Open Source Project
Jakub Pawlowski103b2c42017-04-28 14:59:46 -07004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19#include <gtest/gtest.h>
20#include "advertise_data_parser.h"
21
22TEST(AdvertiseDataParserTest, IsValidEmpty) {
23 const std::vector<uint8_t> data0;
24 EXPECT_TRUE(AdvertiseDataParser::IsValid(data0));
25
Jakub Pawlowski08b907b2017-07-14 15:37:57 -070026 // Single empty field allowed (treated as zero padding).
Jakub Pawlowski103b2c42017-04-28 14:59:46 -070027 const std::vector<uint8_t> data1{0x00};
Jakub Pawlowski08b907b2017-07-14 15:37:57 -070028 EXPECT_TRUE(AdvertiseDataParser::IsValid(data1));
Jakub Pawlowski103b2c42017-04-28 14:59:46 -070029}
30
31TEST(AdvertiseDataParserTest, IsValidBad) {
32 // Single field, field empty.
33 const std::vector<uint8_t> data0{0x01};
34 EXPECT_FALSE(AdvertiseDataParser::IsValid(data0));
35
36 // Single field, first field length too long.
37 const std::vector<uint8_t> data1{0x05, 0x02, 0x00, 0x00, 0x00};
38 EXPECT_FALSE(AdvertiseDataParser::IsValid(data1));
39
40 // Two fields, second field length too long.
41 const std::vector<uint8_t> data2{0x02, 0x02, 0x00, 0x02, 0x00};
42 EXPECT_FALSE(AdvertiseDataParser::IsValid(data2));
43
44 // Two fields, second field empty.
45 const std::vector<uint8_t> data3{0x02, 0x02, 0x00, 0x01};
46 EXPECT_FALSE(AdvertiseDataParser::IsValid(data3));
Jakub Pawlowski08b907b2017-07-14 15:37:57 -070047
48 // Non-zero padding at end of packet.
49 const std::vector<uint8_t> data4{0x03, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01,
50 0x00, 0x00, 0xBA, 0xBA, 0x00, 0x00};
51 EXPECT_FALSE(AdvertiseDataParser::IsValid(data1));
52
53 // Non-zero padding at end of packet.
54 const std::vector<uint8_t> data5{0x03, 0x02, 0x01, 0x02, 0x02,
55 0x03, 0x01, 0x00, 0xBA};
56 EXPECT_FALSE(AdvertiseDataParser::IsValid(data1));
Jakub Pawlowski103b2c42017-04-28 14:59:46 -070057}
58
59TEST(AdvertiseDataParserTest, IsValidGood) {
60 // Single field.
61 const std::vector<uint8_t> data0{0x03, 0x02, 0x01, 0x02};
62 EXPECT_TRUE(AdvertiseDataParser::IsValid(data0));
63
64 // Two fields.
65 const std::vector<uint8_t> data1{0x03, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01};
66 EXPECT_TRUE(AdvertiseDataParser::IsValid(data1));
Jakub Pawlowski08b907b2017-07-14 15:37:57 -070067
68 // Zero padding at end of packet.
69 const std::vector<uint8_t> data2{0x03, 0x02, 0x01, 0x02,
70 0x02, 0x03, 0x01, 0x00};
71 EXPECT_TRUE(AdvertiseDataParser::IsValid(data2));
72
73 // zero padding at end of packet, sample data from real device
74 const std::vector<uint8_t> data3{
75 0x10, 0x096, 0x85, 0x44, 0x32, 0x04, 0x74, 0x32, 0x03, 0x13, 0x93,
76 0xa, 0x32, 0x39, 0x3a, 0x65, 0x32, 0x05, 0x12, 0x50, 0x00, 0x50,
77 0x00, 0x02, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
78 EXPECT_TRUE(AdvertiseDataParser::IsValid(data3));
Jakub Pawlowski02d1ec12017-08-01 15:12:04 -070079
80 // Test Quirk for Traxxas (bad name length, should be 0x11 is 0x14)
81 const std::vector<uint8_t> data4{0x14, 0x09, 0x54, 0x52, 0x58, 0x20,
82 0x42, 0x4C, 0x45, 0x05, 0x12, 0x60,
83 0x00, 0xE8, 0x03, 0x02, 0x0A, 0x00};
84 EXPECT_TRUE(AdvertiseDataParser::IsValid(data4));
85
86 // Test Quirk for Traxxas (bad name length, should be 0x11 is 0x14)
87 const std::vector<uint8_t> data5{0x14, 0x09, 0x54, 0x51, 0x69, 0x20,
88 0x42, 0x4C, 0x45, 0x05, 0x12, 0x64,
89 0x00, 0xE8, 0x03, 0x02, 0x0A, 0x00};
90 EXPECT_TRUE(AdvertiseDataParser::IsValid(data5));
91
92 // Test Quirk for Traxxas (bad name length, should be 0x11 is 0x14)
93 const std::vector<uint8_t> data6{0x14, 0x09, 0x54, 0x51, 0x69, 0x20,
94 0x42, 0x4C, 0x45, 0x05, 0x12, 0x60,
95 0x00, 0xE8, 0x03, 0x02, 0x0A, 0x00};
96 EXPECT_TRUE(AdvertiseDataParser::IsValid(data6));
97
98 // Test Quirk for Traxxas (bad length, should be 0x11 is 0x14)
99 // scan response glued after advertise data
100 const std::vector<uint8_t> data7{
101 0x02, 0x01, 0x06, 0x11, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
102 0x00, 0x64, 0xB1, 0x73, 0x41, 0xE7, 0xF3, 0xC4, 0xB4, 0x80,
103 0x08, 0x14, 0x09, 0x54, 0x51, 0x69, 0x20, 0x42, 0x4C, 0x45,
104 0x05, 0x12, 0x60, 0x00, 0xE8, 0x03, 0x02, 0x0A, 0x00};
105 EXPECT_TRUE(AdvertiseDataParser::IsValid(data7));
Jakub Pawlowski103b2c42017-04-28 14:59:46 -0700106}
107
108TEST(AdvertiseDataParserTest, GetFieldByType) {
109 // Single field.
110 const std::vector<uint8_t> data0{0x03, 0x02, 0x01, 0x02};
111
112 uint8_t p_length;
113 const uint8_t* data =
114 AdvertiseDataParser::GetFieldByType(data0, 0x02, &p_length);
115 EXPECT_EQ(data0.data() + 2, data);
116 EXPECT_EQ(2, p_length);
117
118 // Two fields, second field length too long.
119 const std::vector<uint8_t> data1{0x02, 0x02, 0x00, 0x03, 0x00};
120
121 // First field is ok.
122 data = AdvertiseDataParser::GetFieldByType(data1, 0x02, &p_length);
123 EXPECT_EQ(data1.data() + 2, data);
124 EXPECT_EQ(0x01, p_length);
125
126 // Second field have bad length.
127 data = AdvertiseDataParser::GetFieldByType(data1, 0x03, &p_length);
128 EXPECT_EQ(nullptr, data);
129 EXPECT_EQ(0, p_length);
Jakub Pawlowski35a9ea32017-08-04 08:56:53 -0700130}
131
132// This test makes sure that RemoveTrailingZeros is working correctly. It does
133// run the RemoveTrailingZeros for ad data, then glue scan response at end of
134// it, and checks that the resulting data is good.
135TEST(AdvertiseDataParserTest, RemoveTrailingZeros) {
136 std::vector<uint8_t> podo_ad_data{
137 0x02, 0x01, 0x02, 0x11, 0x06, 0x66, 0x9a, 0x0c, 0x20, 0x00, 0x08,
138 0x37, 0xa8, 0xe5, 0x11, 0x81, 0x8b, 0xd0, 0xf0, 0xf0, 0xf0, 0x00,
139 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Jakub Pawlowski5a290f82018-01-03 01:49:41 -0800140 std::vector<uint8_t> podo_scan_resp{
Jakub Pawlowski35a9ea32017-08-04 08:56:53 -0700141 0x03, 0x19, 0x00, 0x80, 0x09, 0x09, 0x50, 0x6f, 0x64, 0x6f, 0x51,
142 0x35, 0x56, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
144
145 AdvertiseDataParser::RemoveTrailingZeros(podo_ad_data);
Jakub Pawlowski5a290f82018-01-03 01:49:41 -0800146 AdvertiseDataParser::RemoveTrailingZeros(podo_scan_resp);
Jakub Pawlowski35a9ea32017-08-04 08:56:53 -0700147
148 std::vector<uint8_t> glued(podo_ad_data);
Jakub Pawlowski5a290f82018-01-03 01:49:41 -0800149 glued.insert(glued.end(), podo_scan_resp.begin(), podo_scan_resp.end());
150
151 EXPECT_TRUE(AdvertiseDataParser::IsValid(glued));
152}
153
154// This test makes sure that RemoveTrailingZeros is removing all bytes after
155// first zero length field. It does run the RemoveTrailingZeros for data with
156// non-zero bytes after zero length field, then glue scan response at end of it,
157// and checks that the resulting data is good. Note: specification requires all
158// bytes after zero length field to be zero padding, but many legacy devices got
159// this wrong, causing us to have this workaround.
160TEST(AdvertiseDataParserTest, RemoveTrailingZerosMalformed) {
161 std::vector<uint8_t> ad_data{0x02, 0x01, 0x02, 0x11, 0x06, 0x66, 0x9a, 0x0c,
162 0x20, 0x00, 0x08, 0x37, 0xa8, 0xe5, 0x11, 0x81,
163 0x8b, 0xd0, 0xf0, 0xf0, 0xf0, 0x00, 0xFF, 0xFF,
164 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
165 std::vector<uint8_t> scan_resp{0x03, 0x19, 0x00, 0x80, 0x09, 0x09, 0x50, 0x6f,
166 0x64, 0x6f, 0x51, 0x35, 0x56, 0x47, 0x00, 0xFF,
167 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
168 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
169
170 AdvertiseDataParser::RemoveTrailingZeros(ad_data);
171 AdvertiseDataParser::RemoveTrailingZeros(scan_resp);
172
173 std::vector<uint8_t> glued(ad_data);
174 glued.insert(glued.end(), scan_resp.begin(), scan_resp.end());
Jakub Pawlowski35a9ea32017-08-04 08:56:53 -0700175
176 EXPECT_TRUE(AdvertiseDataParser::IsValid(glued));
Jakub Pawlowski103b2c42017-04-28 14:59:46 -0700177}