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, 0x03, 0x02, 0x01, 0x02};
     79   AdvertiseData adv1(data1);
     80   EXPECT_FALSE(adv1.IsValid());
     81 
     82   // Two fields, second invalid.
     83   const std::vector<uint8_t> data2{
     84       0x03, 0x02, 0x01, 0x02, 0x02, HCI_EIR_FLAGS_TYPE, 0x00};
     85   AdvertiseData adv2(data2);
     86   EXPECT_FALSE(adv2.IsValid());
     87 
     88   // Check all blacklisted fields
     89   uint8_t blacklist[] = {HCI_EIR_FLAGS_TYPE, HCI_EIR_OOB_BD_ADDR_TYPE,
     90                          HCI_EIR_OOB_COD_TYPE, HCI_EIR_OOB_SSP_HASH_C_TYPE,
     91                          HCI_EIR_OOB_SSP_RAND_R_TYPE};
     92   for (size_t i = 0; i < sizeof(blacklist); i++) {
     93     const std::vector<uint8_t> data{0x02, blacklist[i], 0x00};
     94     AdvertiseData adv(data);
     95     EXPECT_FALSE(adv.IsValid());
     96   }
     97 }
     98 
     99 TEST(AdvertiseDataTest, EqualsData) {
    100   const std::vector<uint8_t> data0{0x02, 0x02, 0x00};
    101   const std::vector<uint8_t> data1{0x02, 0x03, 0x00};
    102 
    103   AdvertiseData adv0(data0);
    104   AdvertiseData adv1(data1);
    105 
    106   EXPECT_FALSE(adv0 == adv1);
    107 
    108   AdvertiseData adv2(data1);
    109   EXPECT_TRUE(adv1 == adv2);
    110 }
    111 
    112 }  // namespace bluetooth
    113