summaryrefslogtreecommitdiff
path: root/service/test/advertise_data_unittest.cc
blob: bd0787e86b02728570d96c08afa4ee3367b99a54 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
//  Copyright (C) 2015 Google, Inc.
//
//  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 <gtest/gtest.h>

#include "service/common/bluetooth/advertise_data.h"
#include "stack/include/bt_types.h"
#include "stack/include/hcidefs.h"

namespace bluetooth {

TEST(AdvertiseDataTest, EmptyData) {
  const std::vector<uint8_t> data0;
  AdvertiseData adv0(data0);
  EXPECT_TRUE(adv0.IsValid());

  // Single empty field not allowed.
  const std::vector<uint8_t> data1{0x00};
  AdvertiseData adv1(data1);
  EXPECT_FALSE(adv1.IsValid());
}

TEST(AdvertiseDataTest, BadTLV) {
  // Single field, field empty.
  const std::vector<uint8_t> data0{0x01};
  AdvertiseData adv0(data0);
  EXPECT_FALSE(adv0.IsValid());

  // Single field, first field length too long.
  const std::vector<uint8_t> data1{0x05, 0x02, 0x00, 0x00, 0x00};
  AdvertiseData adv1(data1);
  EXPECT_FALSE(adv1.IsValid());

  // Two fields, second field length too long.
  const std::vector<uint8_t> data2{0x02, 0x02, 0x00, 0x02, 0x00};
  AdvertiseData adv2(data2);
  EXPECT_FALSE(adv2.IsValid());

  // Two fields, second field empty.
  const std::vector<uint8_t> data3{0x02, 0x02, 0x00, 0x01};
  AdvertiseData adv3(data3);
  EXPECT_FALSE(adv3.IsValid());
}

TEST(AdvertiseDataTest, GoodTLV) {
  // Singe field.
  const std::vector<uint8_t> data0{0x03, 0x02, 0x01, 0x02};
  AdvertiseData adv0(data0);
  EXPECT_TRUE(adv0.IsValid());

  // Twi fields.
  const std::vector<uint8_t> data1{0x03, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01};
  AdvertiseData adv1(data1);
  EXPECT_TRUE(adv0.IsValid());
}

TEST(AdvertiseDataTest, DisallowedFields) {
  // Singe field.
  const std::vector<uint8_t> data0{0x02, HCI_EIR_FLAGS_TYPE, 0x00};
  AdvertiseData adv0(data0);
  EXPECT_FALSE(adv0.IsValid());

  // Two fields, first invalid.
  const std::vector<uint8_t> data1{
      0x02, HCI_EIR_FLAGS_TYPE, 0x00, 0x03, 0x02, 0x01, 0x02};
  AdvertiseData adv1(data1);
  EXPECT_FALSE(adv1.IsValid());

  // Two fields, second invalid.
  const std::vector<uint8_t> data2{
      0x03, 0x02, 0x01, 0x02, 0x02, HCI_EIR_FLAGS_TYPE, 0x00};
  AdvertiseData adv2(data2);
  EXPECT_FALSE(adv2.IsValid());

  // Check all blacklisted fields
  uint8_t blacklist[] = {HCI_EIR_FLAGS_TYPE, HCI_EIR_OOB_BD_ADDR_TYPE,
                         HCI_EIR_OOB_COD_TYPE, HCI_EIR_OOB_SSP_HASH_C_TYPE,
                         HCI_EIR_OOB_SSP_RAND_R_TYPE};
  for (size_t i = 0; i < sizeof(blacklist); i++) {
    const std::vector<uint8_t> data{0x02, blacklist[i], 0x00};
    AdvertiseData adv(data);
    EXPECT_FALSE(adv.IsValid());
  }
}

TEST(AdvertiseDataTest, EqualsData) {
  const std::vector<uint8_t> data0{0x02, 0x02, 0x00};
  const std::vector<uint8_t> data1{0x02, 0x03, 0x00};

  AdvertiseData adv0(data0);
  AdvertiseData adv1(data1);

  EXPECT_FALSE(adv0 == adv1);

  AdvertiseData adv2(data1);
  EXPECT_TRUE(adv1 == adv2);
}

}  // namespace bluetooth