blob: 139cf657c462ec5082f10ea7d629bb2b34710153 [file] [log] [blame]
Enrico Granata60b1cbc2019-05-20 11:06:46 -07001// Copyright 2019 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
Enrico Granata9bb17522019-06-28 17:22:42 -07005#ifndef MEMS_SETUP_TEST_FAKES_H_
6#define MEMS_SETUP_TEST_FAKES_H_
Enrico Granata60b1cbc2019-05-20 11:06:46 -07007
8#include <algorithm>
9#include <functional>
10#include <map>
11#include <memory>
12#include <optional>
Enrico Granatafc2e3472019-07-16 11:23:25 -070013#include <set>
Enrico Granata60b1cbc2019-05-20 11:06:46 -070014#include <string>
15#include <utility>
16#include <vector>
17
Harvey Yang06ce00f2020-12-01 18:05:23 +080018#include "libmems/test_fakes.h"
Enrico Granata60b1cbc2019-05-20 11:06:46 -070019#include "mems_setup/delegate.h"
Enrico Granata60b1cbc2019-05-20 11:06:46 -070020
Qijiang Fan713061e2021-03-08 15:45:12 +090021#include <base/check.h>
22
Enrico Granata60b1cbc2019-05-20 11:06:46 -070023namespace mems_setup {
Enrico Granata9bb17522019-06-28 17:22:42 -070024namespace fakes {
Enrico Granata60b1cbc2019-05-20 11:06:46 -070025
Enrico Granata9bb17522019-06-28 17:22:42 -070026class FakeDelegate : public Delegate {
Enrico Granata60b1cbc2019-05-20 11:06:46 -070027 public:
28 void SetVpdValue(const std::string& name, const std::string& value) {
29 CHECK(!name.empty());
30 CHECK(!value.empty());
31 vpd_.emplace(name, value);
32 }
33
34 base::Optional<std::string> ReadVpdValue(const std::string& key) override;
35
36 bool ProbeKernelModule(const std::string& module) override;
37
38 size_t GetNumModulesProbed() const { return probed_modules_.size(); }
39
Harvey Yang06ce00f2020-12-01 18:05:23 +080040 bool CreateDirectory(const base::FilePath&) override;
Enrico Granata60b1cbc2019-05-20 11:06:46 -070041 bool Exists(const base::FilePath&) override;
42
Enrico Granatafc2e3472019-07-16 11:23:25 -070043 void CreateFile(const base::FilePath&);
44
Enrico Granata10e19de2019-05-21 14:17:36 -070045 base::Optional<gid_t> FindGroupId(const char* group) override;
46
47 int GetPermissions(const base::FilePath& path) override;
48 bool SetPermissions(const base::FilePath& path, int mode) override;
49
50 void AddGroup(const std::string& name, gid_t gid) {
51 groups_.emplace(name, gid);
52 }
53
54 bool GetOwnership(const base::FilePath& path, uid_t* user, gid_t* group);
55 bool SetOwnership(const base::FilePath& path,
56 uid_t user,
57 gid_t group) override;
58
Harvey Yang06ce00f2020-12-01 18:05:23 +080059 void SetMockContext(libmems::fakes::FakeIioContext* mock_context) {
60 mock_context_ = mock_context;
61 }
62
Enrico Granata60b1cbc2019-05-20 11:06:46 -070063 private:
64 std::vector<std::string> probed_modules_;
65 std::map<std::string, std::string> vpd_;
Enrico Granata10e19de2019-05-21 14:17:36 -070066 std::map<std::string, gid_t> groups_;
67 std::map<std::string, int> permissions_;
68 std::map<std::string, std::pair<uid_t, gid_t>> ownerships_;
Enrico Granatafc2e3472019-07-16 11:23:25 -070069 std::set<base::FilePath> existing_files_;
Harvey Yang06ce00f2020-12-01 18:05:23 +080070
71 libmems::fakes::FakeIioContext* mock_context_ = nullptr;
Enrico Granata60b1cbc2019-05-20 11:06:46 -070072};
73
Enrico Granata9bb17522019-06-28 17:22:42 -070074} // namespace fakes
Enrico Granata60b1cbc2019-05-20 11:06:46 -070075
76} // namespace mems_setup
77
Enrico Granata9bb17522019-06-28 17:22:42 -070078#endif // MEMS_SETUP_TEST_FAKES_H_