• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  Copyright 2019 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 
19 #include <gtest/gtest.h>
20 
21 #include "hci/hci_layer.h"
22 
23 namespace bluetooth {
24 namespace security {
25 
26 using common::ContextualOnceCallback;
27 using hci::BitInserter;
28 using hci::CommandCompleteView;
29 using hci::CommandStatusView;
30 using hci::EventBuilder;
31 using hci::EventCode;
32 using hci::EventView;
33 using hci::HciLayer;
34 using hci::kLittleEndian;
35 using hci::PacketView;
36 
37 namespace {
38 
GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet)39 PacketView<kLittleEndian> GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet) {
40   auto bytes = std::make_shared<std::vector<uint8_t>>();
41   BitInserter i(*bytes);
42   bytes->reserve(packet->size());
43   packet->Serialize(i);
44   return packet::PacketView<packet::kLittleEndian>(bytes);
45 }
46 
47 class CommandQueueEntry {
48  public:
CommandQueueEntry(std::unique_ptr<hci::CommandBuilder> command_packet,ContextualOnceCallback<void (CommandCompleteView)> on_complete_function)49   CommandQueueEntry(
50       std::unique_ptr<hci::CommandBuilder> command_packet,
51       ContextualOnceCallback<void(CommandCompleteView)> on_complete_function)
52       : command(std::move(command_packet)), waiting_for_status_(false), on_complete(std::move(on_complete_function)) {}
53 
CommandQueueEntry(std::unique_ptr<hci::CommandBuilder> command_packet,ContextualOnceCallback<void (CommandStatusView)> on_status_function)54   CommandQueueEntry(
55       std::unique_ptr<hci::CommandBuilder> command_packet,
56       ContextualOnceCallback<void(CommandStatusView)> on_status_function)
57       : command(std::move(command_packet)), waiting_for_status_(true), on_status(std::move(on_status_function)) {}
58 
59   std::unique_ptr<hci::CommandBuilder> command;
60   bool waiting_for_status_;
61   ContextualOnceCallback<void(CommandStatusView)> on_status;
62   ContextualOnceCallback<void(CommandCompleteView)> on_complete;
63 };
64 
65 }  // namespace
66 
67 class FakeHciLayer : public HciLayer {
68  public:
EnqueueCommand(std::unique_ptr<hci::CommandBuilder> command,ContextualOnceCallback<void (CommandStatusView)> on_status)69   void EnqueueCommand(
70       std::unique_ptr<hci::CommandBuilder> command,
71       ContextualOnceCallback<void(CommandStatusView)> on_status) override {
72     auto command_queue_entry = std::make_unique<CommandQueueEntry>(std::move(command), std::move(on_status));
73     command_queue_.push(std::move(command_queue_entry));
74   }
75 
EnqueueCommand(std::unique_ptr<hci::CommandBuilder> command,ContextualOnceCallback<void (CommandCompleteView)> on_complete)76   void EnqueueCommand(
77       std::unique_ptr<hci::CommandBuilder> command,
78       ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
79     auto command_queue_entry = std::make_unique<CommandQueueEntry>(std::move(command), std::move(on_complete));
80     command_queue_.push(std::move(command_queue_entry));
81   }
82 
GetLastCommand()83   std::unique_ptr<CommandQueueEntry> GetLastCommand() {
84     EXPECT_FALSE(command_queue_.empty());
85     auto last = std::move(command_queue_.front());
86     command_queue_.pop();
87     return last;
88   }
89 
RegisterEventHandler(EventCode event_code,common::ContextualCallback<void (EventView)> event_handler)90   void RegisterEventHandler(EventCode event_code, common::ContextualCallback<void(EventView)> event_handler) override {
91     registered_events_[event_code] = event_handler;
92   }
93 
UnregisterEventHandler(EventCode event_code)94   void UnregisterEventHandler(EventCode event_code) override {
95     registered_events_.erase(event_code);
96   }
97 
IncomingEvent(std::unique_ptr<EventBuilder> event_builder)98   void IncomingEvent(std::unique_ptr<EventBuilder> event_builder) {
99     auto packet = GetPacketView(std::move(event_builder));
100     EventView event = EventView::Create(packet);
101     ASSERT_TRUE(event.IsValid());
102     EventCode event_code = event.GetEventCode();
103     ASSERT_TRUE(registered_events_.find(event_code) != registered_events_.end());
104     registered_events_[event_code].Invoke(event);
105   }
106 
ListDependencies(ModuleList *)107   void ListDependencies(ModuleList* /* list */) const override {}
Start()108   void Start() override {}
Stop()109   void Stop() override {}
110 
111  private:
112   std::map<EventCode, common::ContextualCallback<void(EventView)>> registered_events_;
113   std::queue<std::unique_ptr<CommandQueueEntry>> command_queue_;
114 };
115 
116 }  // namespace security
117 }  // namespace bluetooth
118