1 /******************************************************************************
2  *
3  *  Copyright (C) 2017 The Android Open Source Project
4  *
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 
TEST(AdvertiseDataParserTest,IsValidEmpty)22 TEST(AdvertiseDataParserTest, IsValidEmpty) {
23   const std::vector<uint8_t> data0;
24   EXPECT_TRUE(AdvertiseDataParser::IsValid(data0));
25 
26   // Single empty field not allowed.
27   const std::vector<uint8_t> data1{0x00};
28   EXPECT_FALSE(AdvertiseDataParser::IsValid(data1));
29 }
30 
TEST(AdvertiseDataParserTest,IsValidBad)31 TEST(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));
47 }
48 
TEST(AdvertiseDataParserTest,IsValidGood)49 TEST(AdvertiseDataParserTest, IsValidGood) {
50   // Single field.
51   const std::vector<uint8_t> data0{0x03, 0x02, 0x01, 0x02};
52   EXPECT_TRUE(AdvertiseDataParser::IsValid(data0));
53 
54   // Two fields.
55   const std::vector<uint8_t> data1{0x03, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01};
56   EXPECT_TRUE(AdvertiseDataParser::IsValid(data1));
57 }
58 
TEST(AdvertiseDataParserTest,GetFieldByType)59 TEST(AdvertiseDataParserTest, GetFieldByType) {
60   // Single field.
61   const std::vector<uint8_t> data0{0x03, 0x02, 0x01, 0x02};
62 
63   uint8_t p_length;
64   const uint8_t* data =
65       AdvertiseDataParser::GetFieldByType(data0, 0x02, &p_length);
66   EXPECT_EQ(data0.data() + 2, data);
67   EXPECT_EQ(2, p_length);
68 
69   // Two fields, second field length too long.
70   const std::vector<uint8_t> data1{0x02, 0x02, 0x00, 0x03, 0x00};
71 
72   // First field is ok.
73   data = AdvertiseDataParser::GetFieldByType(data1, 0x02, &p_length);
74   EXPECT_EQ(data1.data() + 2, data);
75   EXPECT_EQ(0x01, p_length);
76 
77   // Second field have bad length.
78   data = AdvertiseDataParser::GetFieldByType(data1, 0x03, &p_length);
79   EXPECT_EQ(nullptr, data);
80   EXPECT_EQ(0, p_length);
81 }