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