Home | History | Annotate | Download | only in test
      1 //
      2 //  Copyright (C) 2015 Google, Inc.
      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 <gtest/gtest.h>
     18 
     19 #include "service/common/bluetooth/advertise_data.h"
     20 #include "stack/include/bt_types.h"
     21 #include "stack/include/hcidefs.h"
     22 
     23 namespace bluetooth {
     24 
     25 TEST(AdvertiseDataTest, EmptyData) {
     26   const std::vector<uint8_t> data0;
     27   AdvertiseData adv0(data0);
     28   EXPECT_TRUE(adv0.IsValid());
     29 
     30   // Single empty field not allowed.
     31   const std::vector<uint8_t> data1{ 0x00 };
     32   AdvertiseData adv1(data1);
     33   EXPECT_FALSE(adv1.IsValid());
     34 }
     35 
     36 TEST(AdvertiseDataTest, BadTLV) {
     37   // Single field, field empty.
     38   const std::vector<uint8_t> data0{ 0x01 };
     39   AdvertiseData adv0(data0);
     40   EXPECT_FALSE(adv0.IsValid());
     41 
     42   // Single field, first field length too long.
     43   const std::vector<uint8_t> data1{ 0x05, 0x02, 0x00, 0x00, 0x00 };
     44   AdvertiseData adv1(data1);
     45   EXPECT_FALSE(adv1.IsValid());
     46 
     47   // Two fields, second field length too long.
     48   const std::vector<uint8_t> data2{ 0x02, 0x02, 0x00, 0x02, 0x00 };
     49   AdvertiseData adv2(data2);
     50   EXPECT_FALSE(adv2.IsValid());
     51 
     52   // Two fields, second field empty.
     53   const std::vector<uint8_t> data3{ 0x02, 0x02, 0x00, 0x01 };
     54   AdvertiseData adv3(data3);
     55   EXPECT_FALSE(adv3.IsValid());
     56 }
     57 
     58 TEST(AdvertiseDataTest, GoodTLV) {
     59   // Singe field.
     60   const std::vector<uint8_t> data0{ 0x03, 0x02, 0x01, 0x02 };
     61   AdvertiseData adv0(data0);
     62   EXPECT_TRUE(adv0.IsValid());
     63 
     64   // Twi fields.
     65   const std::vector<uint8_t> data1{ 0x03, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01 };
     66   AdvertiseData adv1(data1);
     67   EXPECT_TRUE(adv0.IsValid());
     68 }
     69 
     70 TEST(AdvertiseDataTest, DisallowedFields) {
     71   // Singe field.
     72   const std::vector<uint8_t> data0{ 0x02, HCI_EIR_FLAGS_TYPE, 0x00 };
     73   AdvertiseData adv0(data0);
     74   EXPECT_FALSE(adv0.IsValid());
     75 
     76   // Two fields, first invalid.
     77   const std::vector<uint8_t> data1{
     78       0x02, HCI_EIR_FLAGS_TYPE, 0x00,
     79       0x03, 0x02, 0x01, 0x02
     80   };
     81   AdvertiseData adv1(data1);
     82   EXPECT_FALSE(adv1.IsValid());
     83 
     84   // Two fields, second invalid.
     85   const std::vector<uint8_t> data2{
     86       0x03, 0x02, 0x01, 0x02,
     87       0x02, HCI_EIR_FLAGS_TYPE, 0x00
     88   };
     89   AdvertiseData adv2(data2);
     90   EXPECT_FALSE(adv2.IsValid());
     91 
     92   // Check all blacklisted fields
     93   uint8_t blacklist[] = {
     94       HCI_EIR_FLAGS_TYPE,
     95       HCI_EIR_TX_POWER_LEVEL_TYPE,
     96       HCI_EIR_SHORTENED_LOCAL_NAME_TYPE,
     97       HCI_EIR_COMPLETE_LOCAL_NAME_TYPE,
     98       HCI_EIR_OOB_BD_ADDR_TYPE,
     99       HCI_EIR_OOB_COD_TYPE,
    100       HCI_EIR_OOB_SSP_HASH_C_TYPE,
    101       HCI_EIR_OOB_SSP_RAND_R_TYPE
    102   };
    103   for (size_t i = 0; i < sizeof(blacklist); i++) {
    104     const std::vector<uint8_t> data{ 0x02, blacklist[i], 0x00 };
    105     AdvertiseData adv(data);
    106     EXPECT_FALSE(adv.IsValid());
    107   }
    108 }
    109 
    110 TEST(AdvertiseDataTest, EqualsData) {
    111   const std::vector<uint8_t> data0{ 0x02, 0x02, 0x00 };
    112   const std::vector<uint8_t> data1{ 0x02, 0x03, 0x00 };
    113 
    114   AdvertiseData adv0(data0);
    115   AdvertiseData adv1(data1);
    116 
    117   EXPECT_FALSE(adv0 == adv1);
    118 
    119   AdvertiseData adv2(data1);
    120   EXPECT_TRUE(adv1 == adv2);
    121 }
    122 
    123 TEST(AdvertiseDataTest, EqualsIncludes) {
    124   const std::vector<uint8_t> data;
    125 
    126   AdvertiseData adv0;
    127   AdvertiseData adv1;
    128   AdvertiseData adv2;
    129   AdvertiseData adv3;
    130   AdvertiseData adv4;
    131 
    132   adv0.set_include_device_name(true);
    133   adv0.set_include_tx_power_level(false);
    134 
    135   adv1.set_include_device_name(false);
    136   adv1.set_include_tx_power_level(false);
    137 
    138   adv2.set_include_device_name(false);
    139   adv2.set_include_tx_power_level(true);
    140 
    141   adv3.set_include_device_name(true);
    142   adv3.set_include_tx_power_level(true);
    143 
    144   adv4.set_include_device_name(true);
    145   adv4.set_include_tx_power_level(false);
    146 
    147   EXPECT_FALSE(adv0 == adv1);
    148   EXPECT_FALSE(adv0 == adv2);
    149   EXPECT_FALSE(adv3 == adv1);
    150   EXPECT_TRUE(adv4 == adv0);
    151 }
    152 
    153 }  // namespace bluetooth
    154