Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 1 | // 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 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_FAKE_PROCESS_RUNNER_H_ |
| 6 | #define PATCHPANEL_FAKE_PROCESS_RUNNER_H_ |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/strings/string_util.h> |
| 13 | |
| 14 | #include <gtest/gtest.h> |
| 15 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 16 | #include "patchpanel/minijailed_process_runner.h" |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 17 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 18 | namespace patchpanel { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 19 | |
| 20 | class FakeProcessRunner : public MinijailedProcessRunner { |
| 21 | public: |
| 22 | explicit FakeProcessRunner(std::vector<std::string>* runs = nullptr) |
| 23 | : runs_(runs ? runs : &runs_vec_) {} |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 24 | FakeProcessRunner(const FakeProcessRunner&) = delete; |
| 25 | FakeProcessRunner& operator=(const FakeProcessRunner&) = delete; |
| 26 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 27 | ~FakeProcessRunner() = default; |
| 28 | |
| 29 | int Run(const std::vector<std::string>& argv, bool log_failures) override { |
| 30 | if (capture_) |
| 31 | runs_->emplace_back(base::JoinString(argv, " ")); |
Taoyu Li | ca49c83 | 2019-12-06 17:56:43 +0900 | [diff] [blame] | 32 | if (run_override_) |
| 33 | return run_override_.Run(argv); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 34 | return 0; |
| 35 | } |
| 36 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 37 | void Capture(bool on, std::vector<std::string>* runs = nullptr) { |
| 38 | capture_ = on; |
| 39 | if (runs) |
| 40 | runs_ = runs; |
| 41 | } |
| 42 | |
| 43 | void VerifyRuns(const std::vector<std::string>& expected) { |
| 44 | VerifyRuns(*runs_, expected); |
| 45 | } |
| 46 | |
| 47 | static void VerifyRuns(const std::vector<std::string>& got, |
| 48 | const std::vector<std::string>& expected) { |
| 49 | ASSERT_EQ(got.size(), expected.size()); |
| 50 | for (int i = 0; i < got.size(); ++i) { |
| 51 | EXPECT_EQ(got[i], expected[i]); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void VerifyAddInterface(const std::string& host_ifname, |
| 56 | const std::string& con_ifname, |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 57 | uint32_t con_ipv4, |
| 58 | uint32_t con_prefix_len, |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 59 | bool enable_multicast, |
| 60 | const std::string& con_pid) { |
| 61 | EXPECT_EQ(host_ifname, add_host_ifname_); |
| 62 | EXPECT_EQ(con_ifname, add_con_ifname_); |
| 63 | EXPECT_EQ(con_ipv4, add_con_ipv4_); |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 64 | EXPECT_EQ(con_prefix_len, add_con_prefix_len_); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 65 | EXPECT_EQ(enable_multicast, add_enable_multicast_); |
| 66 | EXPECT_EQ(con_pid, add_con_pid_); |
| 67 | } |
| 68 | |
Taoyu Li | ca49c83 | 2019-12-06 17:56:43 +0900 | [diff] [blame] | 69 | void SetRunOverride( |
| 70 | base::Callback<int(const std::vector<std::string>&)> callback) { |
| 71 | run_override_ = callback; |
| 72 | } |
| 73 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 74 | private: |
| 75 | bool capture_ = false; |
Taoyu Li | ca49c83 | 2019-12-06 17:56:43 +0900 | [diff] [blame] | 76 | base::Callback<int(const std::vector<std::string>&)> run_override_; |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 77 | std::vector<std::string>* runs_; |
| 78 | std::vector<std::string> runs_vec_; |
| 79 | std::string add_host_ifname_; |
| 80 | std::string add_con_ifname_; |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 81 | uint32_t add_con_ipv4_; |
| 82 | uint32_t add_con_prefix_len_; |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 83 | bool add_enable_multicast_; |
| 84 | std::string add_con_pid_; |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 85 | }; |
| 86 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 87 | } // namespace patchpanel |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 88 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 89 | #endif // PATCHPANEL_FAKE_PROCESS_RUNNER_H_ |