blob: c935cf79c01664d3304bb60584cc05d04e43acd1 [file] [log] [blame]
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +09001// Copyright 2017 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 PATCHPANEL_MOCK_FIREWALL_H_
6#define PATCHPANEL_MOCK_FIREWALL_H_
7
8#include <string>
9#include <vector>
10
11#include <base/macros.h>
12#include <gmock/gmock.h>
13
14#include "patchpanel/firewall.h"
15
16namespace patchpanel {
17
18class MockFirewall : public Firewall {
19 public:
20 MockFirewall() = default;
Qijiang Fan6bc59e12020-11-11 02:51:06 +090021 MockFirewall(const MockFirewall&) = delete;
22 MockFirewall& operator=(const MockFirewall&) = delete;
23
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090024 ~MockFirewall() = default;
25
Tom Hughes249adef2020-08-24 18:21:52 -070026 int SetRunInMinijailFailCriterion(const std::vector<std::string>& keywords,
27 bool repeat,
28 bool omit_failure);
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090029 int GetRunInMinijailCriterionMatchCount(int id);
30 bool CheckCommandsUndone();
31 int CountActiveCommands();
32 void ResetStoredCommands();
33
34 // Check if the current command matches a failure rule,
35 // if the failure rule is not a repeat rule, remove it
36 // from the match criteria.
37 bool MatchAndUpdate(const std::vector<std::string>& argv);
38
39 // Returns all commands issued with RunInMinijail during the test.
40 std::vector<std::string> GetAllCommands();
41
42 private:
43 struct Criterion {
44 std::vector<std::string> keywords;
45 // If false, remove the criterion after it's matched.
46 bool repeat;
47 // If false, treat matching commands as failures, otherwise,
48 // omit failing.
49 bool omit_failure;
50 // Count the number of times the criterion is matched.
51 int match_count;
52
53 // Don't take into account match_count.
54 bool operator==(const Criterion& c) const {
Tom Hughes249adef2020-08-24 18:21:52 -070055 return (
56 std::equal(keywords.begin(), keywords.end(), c.keywords.begin()) &&
57 (repeat == c.repeat) && (c.omit_failure == omit_failure));
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090058 }
59 };
60
61 // A list of collections of keywords that a command
62 // must have in order for it to fail.
63 std::vector<Criterion> match_criteria_;
64
65 // A log of commands issued with RunInMinijail during the test.
66 std::vector<std::vector<std::string>> commands_;
67
68 // The mock's implementation simply logs the command issued if
69 // successful.
70 int RunInMinijail(const std::vector<std::string>& argv) override;
71
72 // Given an ip/iptables command, return the command that undoes its effect.
73 std::vector<std::string> GetInverseCommand(
74 const std::vector<std::string>& command);
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090075};
76
77} // namespace patchpanel
78
79#endif // PATCHPANEL_MOCK_FIREWALL_H_