Myles Watson | 0ead597 | 2019-04-01 13:21:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 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 "packet/raw_builder.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <forward_list> |
| 21 | #include <memory> |
| 22 | |
Martin Brabham | 94db40c | 2019-03-29 10:24:52 -0700 | [diff] [blame] | 23 | #include "hci/address.h" |
Myles Watson | 0ead597 | 2019-04-01 13:21:25 -0700 | [diff] [blame] | 24 | |
Martin Brabham | 94db40c | 2019-03-29 10:24:52 -0700 | [diff] [blame] | 25 | using bluetooth::hci::Address; |
Myles Watson | 0ead597 | 2019-04-01 13:21:25 -0700 | [diff] [blame] | 26 | using bluetooth::packet::BitInserter; |
| 27 | using std::vector; |
| 28 | |
| 29 | namespace { |
| 30 | vector<uint8_t> count = { |
| 31 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 32 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 33 | }; |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | namespace bluetooth { |
| 38 | namespace packet { |
| 39 | |
| 40 | class RawBuilderTest : public ::testing::Test { |
| 41 | public: |
| 42 | RawBuilderTest() = default; |
| 43 | ~RawBuilderTest() = default; |
| 44 | }; |
| 45 | |
| 46 | TEST(RawBuilderTest, buildCountTest) { |
| 47 | std::unique_ptr<RawBuilder> count_builder = std::make_unique<RawBuilder>(); |
| 48 | ASSERT_EQ(0u, count_builder->size()); |
| 49 | count_builder->AddOctets8(0x0706050403020100); |
| 50 | count_builder->AddOctets4(0x0b0a0908); |
| 51 | count_builder->AddOctets2(0x0d0c); |
| 52 | count_builder->AddOctets1(0x0e); |
| 53 | count_builder->AddOctets1(0x0f); |
| 54 | count_builder->AddAddress(Address({0x10, 0x11, 0x12, 0x13, 0x14, 0x15})); |
| 55 | std::vector<uint8_t> count_subset(count.begin() + 0x16, count.end()); |
| 56 | count_builder->AddOctets(count_subset); |
| 57 | |
| 58 | ASSERT_EQ(count.size(), count_builder->size()); |
| 59 | |
| 60 | std::vector<uint8_t> packet; |
| 61 | BitInserter it(packet); |
| 62 | |
| 63 | count_builder->Serialize(it); |
| 64 | |
| 65 | ASSERT_EQ(count, packet); |
| 66 | } |
| 67 | |
Myles Watson | 44b68e2 | 2019-11-22 10:34:11 -0800 | [diff] [blame^] | 68 | TEST(RawBuilderTest, buildStartingWithVector) { |
| 69 | std::vector<uint8_t> count_first(count.begin(), count.begin() + 0x8); |
| 70 | std::unique_ptr<RawBuilder> count_builder = std::make_unique<RawBuilder>(count_first); |
| 71 | count_builder->AddOctets4(0x0b0a0908); |
| 72 | count_builder->AddOctets2(0x0d0c); |
| 73 | count_builder->AddOctets1(0x0e); |
| 74 | count_builder->AddOctets1(0x0f); |
| 75 | count_builder->AddOctets8(0x1716151413121110); |
| 76 | std::vector<uint8_t> count_last(count.begin() + 0x18, count.end()); |
| 77 | count_builder->AddOctets(count_last); |
| 78 | |
| 79 | ASSERT_EQ(count.size(), count_builder->size()); |
| 80 | |
| 81 | std::vector<uint8_t> packet; |
| 82 | BitInserter it(packet); |
| 83 | |
| 84 | count_builder->Serialize(it); |
| 85 | |
| 86 | ASSERT_EQ(count, packet); |
| 87 | } |
| 88 | |
| 89 | TEST(RawBuilderTest, testMaxBytes) { |
| 90 | const size_t kMaxBytes = count.size(); |
| 91 | std::unique_ptr<RawBuilder> count_builder = std::make_unique<RawBuilder>(kMaxBytes); |
| 92 | ASSERT_TRUE(count_builder->AddOctets(count)); |
| 93 | ASSERT_FALSE(count_builder->AddOctets4(0x0b0a0908)); |
| 94 | ASSERT_FALSE(count_builder->AddOctets2(0x0d0c)); |
| 95 | ASSERT_FALSE(count_builder->AddOctets1(0x0e)); |
| 96 | ASSERT_FALSE(count_builder->AddOctets1(0x0f)); |
| 97 | ASSERT_FALSE(count_builder->AddOctets8(0x1716151413121110)); |
| 98 | std::vector<uint8_t> count_last(count.begin() + 0x18, count.end()); |
| 99 | ASSERT_FALSE(count_builder->AddOctets(count_last)); |
| 100 | |
| 101 | ASSERT_EQ(count.size(), count_builder->size()); |
| 102 | |
| 103 | std::vector<uint8_t> packet; |
| 104 | BitInserter it(packet); |
| 105 | |
| 106 | count_builder->Serialize(it); |
| 107 | |
| 108 | ASSERT_EQ(count, packet); |
| 109 | } |
| 110 | |
Myles Watson | 0ead597 | 2019-04-01 13:21:25 -0700 | [diff] [blame] | 111 | } // namespace packet |
| 112 | } // namespace bluetooth |