blob: 1722fdab687cdbcab43422a8c5a11f0ec4304da5 [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;
21 ~MockFirewall() = default;
22
Tom Hughes249adef2020-08-24 18:21:52 -070023 int SetRunInMinijailFailCriterion(const std::vector<std::string>& keywords,
24 bool repeat,
25 bool omit_failure);
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090026 int GetRunInMinijailCriterionMatchCount(int id);
27 bool CheckCommandsUndone();
28 int CountActiveCommands();
29 void ResetStoredCommands();
30
31 // Check if the current command matches a failure rule,
32 // if the failure rule is not a repeat rule, remove it
33 // from the match criteria.
34 bool MatchAndUpdate(const std::vector<std::string>& argv);
35
36 // Returns all commands issued with RunInMinijail during the test.
37 std::vector<std::string> GetAllCommands();
38
39 private:
40 struct Criterion {
41 std::vector<std::string> keywords;
42 // If false, remove the criterion after it's matched.
43 bool repeat;
44 // If false, treat matching commands as failures, otherwise,
45 // omit failing.
46 bool omit_failure;
47 // Count the number of times the criterion is matched.
48 int match_count;
49
50 // Don't take into account match_count.
51 bool operator==(const Criterion& c) const {
Tom Hughes249adef2020-08-24 18:21:52 -070052 return (
53 std::equal(keywords.begin(), keywords.end(), c.keywords.begin()) &&
54 (repeat == c.repeat) && (c.omit_failure == omit_failure));
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090055 }
56 };
57
58 // A list of collections of keywords that a command
59 // must have in order for it to fail.
60 std::vector<Criterion> match_criteria_;
61
62 // A log of commands issued with RunInMinijail during the test.
63 std::vector<std::vector<std::string>> commands_;
64
65 // The mock's implementation simply logs the command issued if
66 // successful.
67 int RunInMinijail(const std::vector<std::string>& argv) override;
68
69 // Given an ip/iptables command, return the command that undoes its effect.
70 std::vector<std::string> GetInverseCommand(
71 const std::vector<std::string>& command);
72
73 DISALLOW_COPY_AND_ASSIGN(MockFirewall);
74};
75
76} // namespace patchpanel
77
78#endif // PATCHPANEL_MOCK_FIREWALL_H_