1 // Copyright 2018 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef LIBBRILLO_BRILLO_BLKDEV_UTILS_DEVICE_MAPPER_FAKE_H_
6 #define LIBBRILLO_BRILLO_BLKDEV_UTILS_DEVICE_MAPPER_FAKE_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include <base/files/file_path.h>
13 #include <brillo/blkdev_utils/device_mapper.h>
14 #include <brillo/blkdev_utils/device_mapper_fake.h>
15 #include <brillo/blkdev_utils/device_mapper_task.h>
16 #include <brillo/secure_blob.h>
17 
18 namespace brillo {
19 namespace fake {
20 
21 // Fake implementation of dm_task primitives.
22 // ------------------------------------------
23 // dm_task is an opaque type in libdevmapper so we
24 // define a minimal struct for DmTask and DmTarget
25 // to avoid linking in libdevmapper.
26 struct DmTarget {
27   uint64_t start;
28   uint64_t size;
29   std::string type;
30   SecureBlob parameters;
31 };
32 
33 struct DmTask {
34   int type;
35   std::string name;
36   std::vector<DmTarget> targets;
37 };
38 
39 // Fake task factory: creates fake tasks that
40 // stub task info into a map.
41 std::unique_ptr<DevmapperTask> CreateDevmapperTask(int type);
42 
43 class FakeDevmapperTask : public brillo::DevmapperTask {
44  public:
45   explicit FakeDevmapperTask(int type);
46   ~FakeDevmapperTask() override = default;
47   bool SetName(const std::string& name) override;
48   bool AddTarget(uint64_t start,
49                  uint64_t sectors,
50                  const std::string& target,
51                  const SecureBlob& parameters) override;
52   bool GetNextTarget(uint64_t* start,
53                      uint64_t* sectors,
54                      std::string* target,
55                      SecureBlob* parameters) override;
56   bool Run(bool udev_sync = true) override;
57 
58  private:
59   std::unique_ptr<DmTask> task_;
60 };
61 
62 }  // namespace fake
63 }  // namespace brillo
64 
65 #endif  // LIBBRILLO_BRILLO_BLKDEV_UTILS_DEVICE_MAPPER_FAKE_H_
66