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_) {} |
| 24 | ~FakeProcessRunner() = default; |
| 25 | |
| 26 | int Run(const std::vector<std::string>& argv, bool log_failures) override { |
| 27 | if (capture_) |
| 28 | runs_->emplace_back(base::JoinString(argv, " ")); |
Taoyu Li | ca49c83 | 2019-12-06 17:56:43 +0900 | [diff] [blame] | 29 | if (run_override_) |
| 30 | return run_override_.Run(argv); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 31 | return 0; |
| 32 | } |
| 33 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 34 | int RestoreDefaultNamespace(const std::string& ifname, pid_t pid) override { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 35 | return 0; |
| 36 | } |
| 37 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 38 | void Capture(bool on, std::vector<std::string>* runs = nullptr) { |
| 39 | capture_ = on; |
| 40 | if (runs) |
| 41 | runs_ = runs; |
| 42 | } |
| 43 | |
| 44 | void VerifyRuns(const std::vector<std::string>& expected) { |
| 45 | VerifyRuns(*runs_, expected); |
| 46 | } |
| 47 | |
| 48 | static void VerifyRuns(const std::vector<std::string>& got, |
| 49 | const std::vector<std::string>& expected) { |
| 50 | ASSERT_EQ(got.size(), expected.size()); |
| 51 | for (int i = 0; i < got.size(); ++i) { |
| 52 | EXPECT_EQ(got[i], expected[i]); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void VerifyAddInterface(const std::string& host_ifname, |
| 57 | const std::string& con_ifname, |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 58 | uint32_t con_ipv4, |
| 59 | uint32_t con_prefix_len, |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 60 | bool enable_multicast, |
| 61 | const std::string& con_pid) { |
| 62 | EXPECT_EQ(host_ifname, add_host_ifname_); |
| 63 | EXPECT_EQ(con_ifname, add_con_ifname_); |
| 64 | EXPECT_EQ(con_ipv4, add_con_ipv4_); |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 65 | EXPECT_EQ(con_prefix_len, add_con_prefix_len_); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 66 | EXPECT_EQ(enable_multicast, add_enable_multicast_); |
| 67 | EXPECT_EQ(con_pid, add_con_pid_); |
| 68 | } |
| 69 | |
Taoyu Li | ca49c83 | 2019-12-06 17:56:43 +0900 | [diff] [blame] | 70 | void SetRunOverride( |
| 71 | base::Callback<int(const std::vector<std::string>&)> callback) { |
| 72 | run_override_ = callback; |
| 73 | } |
| 74 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 75 | private: |
| 76 | bool capture_ = false; |
Taoyu Li | ca49c83 | 2019-12-06 17:56:43 +0900 | [diff] [blame] | 77 | base::Callback<int(const std::vector<std::string>&)> run_override_; |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 78 | std::vector<std::string>* runs_; |
| 79 | std::vector<std::string> runs_vec_; |
| 80 | std::string add_host_ifname_; |
| 81 | std::string add_con_ifname_; |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 82 | uint32_t add_con_ipv4_; |
| 83 | uint32_t add_con_prefix_len_; |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 84 | bool add_enable_multicast_; |
| 85 | std::string add_con_pid_; |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 86 | |
| 87 | DISALLOW_COPY_AND_ASSIGN(FakeProcessRunner); |
| 88 | }; |
| 89 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 90 | } // namespace patchpanel |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 91 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 92 | #endif // PATCHPANEL_FAKE_PROCESS_RUNNER_H_ |