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 "l2cap/classic/internal/link_mock.h"
18 #include "l2cap/internal/dynamic_channel_allocator.h"
19 #include "l2cap/internal/parameter_provider_mock.h"
20 
21 #include <gmock/gmock.h>
22 
23 namespace bluetooth {
24 namespace l2cap {
25 namespace internal {
26 
27 using classic::internal::testing::MockLink;
28 using hci::testing::MockClassicAclConnection;
29 using l2cap::internal::testing::MockParameterProvider;
30 using l2cap::internal::testing::MockScheduler;
31 using ::testing::NiceMock;
32 using ::testing::Return;
33 
34 const hci::AddressWithType device{{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}}, hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
35 
36 class L2capClassicDynamicChannelAllocatorFuzzTest {
37  public:
RunTests(const uint8_t * data,size_t size)38   void RunTests(const uint8_t* data, size_t size) {
39     SetUp();
40     TestPrecondition(data, size);
41     TearDown();
42   }
43 
44  private:
SetUp()45   void SetUp() {
46     thread_ = new os::Thread("test_thread", os::Thread::Priority::NORMAL);
47     handler_ = new os::Handler(thread_);
48     mock_parameter_provider_ = new NiceMock<MockParameterProvider>();
49     mock_classic_link_ = new NiceMock<MockLink>(handler_, mock_parameter_provider_,
50                                                 std::make_unique<NiceMock<MockClassicAclConnection>>(), nullptr);
51     EXPECT_CALL(*mock_classic_link_, GetDevice()).WillRepeatedly(Return(device));
52     channel_allocator_ = std::make_unique<DynamicChannelAllocator>(mock_classic_link_, handler_);
53   }
54 
TearDown()55   void TearDown() {
56     channel_allocator_.reset();
57     delete mock_classic_link_;
58     delete mock_parameter_provider_;
59     handler_->Clear();
60     delete handler_;
61     delete thread_;
62   }
63 
TestPrecondition(const uint8_t * data,size_t size)64   void TestPrecondition(const uint8_t* data, size_t size) {
65     if (size != 2) {
66       return;
67     }
68     Psm psm = *reinterpret_cast<const Psm*>(data);
69     EXPECT_FALSE(channel_allocator_->IsPsmUsed(psm));
70   }
71 
72   os::Thread* thread_{nullptr};
73   os::Handler* handler_{nullptr};
74   NiceMock<MockParameterProvider>* mock_parameter_provider_{nullptr};
75   NiceMock<MockLink>* mock_classic_link_{nullptr};
76   std::unique_ptr<DynamicChannelAllocator> channel_allocator_;
77 };
78 
79 }  // namespace internal
80 }  // namespace l2cap
81 }  // namespace bluetooth
82 
RunL2capClassicDynamicChannelAllocatorFuzzTest(const uint8_t * data,size_t size)83 void RunL2capClassicDynamicChannelAllocatorFuzzTest(const uint8_t* data, size_t size) {
84   bluetooth::l2cap::internal::L2capClassicDynamicChannelAllocatorFuzzTest test;
85   test.RunTests(data, size);
86 }
87