xref: /aosp_15_r20/system/core/fs_mgr/libsnapshot/snapuserd/testing/host_harness.h (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
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 #pragma once
16 
17 #include <condition_variable>
18 #include <mutex>
19 #include <string>
20 #include <unordered_map>
21 #include <utility>
22 #include <vector>
23 
24 #include "harness.h"
25 
26 namespace android {
27 namespace snapshot {
28 
29 class TestBlockServerQueue final {
30   public:
31     void WaitForShutdown();
32     void Shutdown();
33 
34   private:
35     std::mutex m_;
36     std::condition_variable cv_;
37     bool shutdown_ = false;
38 };
39 
40 class TestBlockServer final : public IBlockServer {
41   public:
42     TestBlockServer(std::shared_ptr<TestBlockServerQueue> queue, const std::string& misc_name);
43     bool ProcessRequests() override;
44     void* GetResponseBuffer(size_t size, size_t to_write) override;
45     bool SendBufferedIo() override;
46 
sent_io()47     std::string&& sent_io() { return std::move(sent_io_); }
48 
49   private:
50     std::shared_ptr<TestBlockServerQueue> queue_;
51     std::string misc_name_;
52     std::string sent_io_;
53     std::vector<std::pair<std::string, size_t>> buffered_;
54 };
55 
56 class TestBlockServerOpener final : public IBlockServerOpener {
57   public:
58     TestBlockServerOpener(std::shared_ptr<TestBlockServerQueue> queue,
59                           const std::string& misc_name);
60     std::unique_ptr<IBlockServer> Open(IBlockServer::Delegate* delegate,
61                                        size_t buffer_size) override;
62 
63   private:
64     std::shared_ptr<TestBlockServerQueue> queue_;
65     std::string misc_name_;
66 };
67 
68 class TestBlockServerFactory final : public IBlockServerFactory {
69   public:
70     std::shared_ptr<IBlockServerOpener> CreateOpener(const std::string& misc_name) override;
71     std::shared_ptr<TestBlockServerOpener> CreateTestOpener(const std::string& misc_name);
72     bool DeleteQueue(const std::string& misc_name);
73 
74   private:
75     std::unordered_map<std::string, std::shared_ptr<TestBlockServerQueue>> queues_;
76 };
77 
78 class TestBlockServerFactory;
79 
80 class HostUserDevice final : public IUserDevice {
81   public:
82     HostUserDevice(TestBlockServerFactory* factory, const std::string& misc_name);
GetPath()83     const std::string& GetPath() override { return empty_path_; }
84     bool Destroy();
85 
86   private:
87     TestBlockServerFactory* factory_;
88     std::string misc_name_;
89     std::string empty_path_;
90 };
91 
92 class HostTestHarness final : public ITestHarness {
93   public:
94     std::unique_ptr<IUserDevice> CreateUserDevice(const std::string& dev_name,
95                                                   const std::string& misc_name,
96                                                   uint64_t num_sectors) override;
97     IBlockServerFactory* GetBlockServerFactory() override;
HasUserDevice()98     bool HasUserDevice() override { return false; }
99 
100   private:
101     TestBlockServerFactory factory_;
102 };
103 
104 }  // namespace snapshot
105 }  // namespace android
106