1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "host_harness.h"
16
17 #include "snapuserd_logging.h"
18
19 namespace android {
20 namespace snapshot {
21
WaitForShutdown()22 void TestBlockServerQueue::WaitForShutdown() {
23 std::unique_lock lock(m_);
24 if (shutdown_) {
25 return;
26 }
27 cv_.wait(lock, [this]() -> bool { return shutdown_; });
28 }
29
Shutdown()30 void TestBlockServerQueue::Shutdown() {
31 std::unique_lock lock(m_);
32 shutdown_ = true;
33 cv_.notify_all();
34 }
35
TestBlockServer(std::shared_ptr<TestBlockServerQueue> queue,const std::string & misc_name)36 TestBlockServer::TestBlockServer(std::shared_ptr<TestBlockServerQueue> queue,
37 const std::string& misc_name)
38 : queue_(queue), misc_name_(misc_name) {}
39
ProcessRequests()40 bool TestBlockServer::ProcessRequests() {
41 queue_->WaitForShutdown();
42 return false;
43 }
44
GetResponseBuffer(size_t size,size_t to_write)45 void* TestBlockServer::GetResponseBuffer(size_t size, size_t to_write) {
46 std::string buffer(size, '\0');
47 buffered_.emplace_back(std::move(buffer), to_write);
48 return buffered_.back().first.data();
49 }
50
SendBufferedIo()51 bool TestBlockServer::SendBufferedIo() {
52 for (const auto& [data, to_write] : buffered_) {
53 sent_io_ += data.substr(0, to_write);
54 }
55 buffered_.clear();
56 return true;
57 }
58
TestBlockServerOpener(std::shared_ptr<TestBlockServerQueue> queue,const std::string & misc_name)59 TestBlockServerOpener::TestBlockServerOpener(std::shared_ptr<TestBlockServerQueue> queue,
60 const std::string& misc_name)
61 : queue_(queue), misc_name_(misc_name) {}
62
Open(IBlockServer::Delegate *,size_t)63 std::unique_ptr<IBlockServer> TestBlockServerOpener::Open(IBlockServer::Delegate*, size_t) {
64 return std::make_unique<TestBlockServer>(queue_, misc_name_);
65 }
66
CreateTestOpener(const std::string & misc_name)67 std::shared_ptr<TestBlockServerOpener> TestBlockServerFactory::CreateTestOpener(
68 const std::string& misc_name) {
69 if (queues_.count(misc_name)) {
70 LOG(ERROR) << "Cannot create opener for " << misc_name << ", already exists";
71 return nullptr;
72 }
73 auto queue = std::make_shared<TestBlockServerQueue>();
74 queues_.emplace(misc_name, queue);
75 return std::make_shared<TestBlockServerOpener>(queue, misc_name);
76 }
77
CreateOpener(const std::string & misc_name)78 std::shared_ptr<IBlockServerOpener> TestBlockServerFactory::CreateOpener(
79 const std::string& misc_name) {
80 return CreateTestOpener(misc_name);
81 }
82
DeleteQueue(const std::string & misc_name)83 bool TestBlockServerFactory::DeleteQueue(const std::string& misc_name) {
84 auto iter = queues_.find(misc_name);
85 if (iter == queues_.end()) {
86 LOG(ERROR) << "Cannot delete queue " << misc_name << ", not found";
87 return false;
88 }
89 iter->second->Shutdown();
90 queues_.erase(iter);
91 return true;
92 }
93
HostUserDevice(TestBlockServerFactory * factory,const std::string & misc_name)94 HostUserDevice::HostUserDevice(TestBlockServerFactory* factory, const std::string& misc_name)
95 : factory_(factory), misc_name_(misc_name) {}
96
Destroy()97 bool HostUserDevice::Destroy() {
98 return factory_->DeleteQueue(misc_name_);
99 }
100
CreateUserDevice(const std::string &,const std::string & misc_name,uint64_t)101 std::unique_ptr<IUserDevice> HostTestHarness::CreateUserDevice(const std::string&,
102 const std::string& misc_name,
103 uint64_t) {
104 return std::make_unique<HostUserDevice>(&factory_, misc_name);
105 }
106
GetBlockServerFactory()107 IBlockServerFactory* HostTestHarness::GetBlockServerFactory() {
108 return &factory_;
109 }
110
111 } // namespace snapshot
112 } // namespace android
113