blob: b35fc62524c9def2fb3eeba1a4608a5714dfe59e [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
23 int SetRunInMinijailFailCriterion(
24 const std::vector<std::string>& keywords, bool repeat, bool omit_failure);
25 int GetRunInMinijailCriterionMatchCount(int id);
26 bool CheckCommandsUndone();
27 int CountActiveCommands();
28 void ResetStoredCommands();
29
30 // Check if the current command matches a failure rule,
31 // if the failure rule is not a repeat rule, remove it
32 // from the match criteria.
33 bool MatchAndUpdate(const std::vector<std::string>& argv);
34
35 // Returns all commands issued with RunInMinijail during the test.
36 std::vector<std::string> GetAllCommands();
37
38 private:
39 struct Criterion {
40 std::vector<std::string> keywords;
41 // If false, remove the criterion after it's matched.
42 bool repeat;
43 // If false, treat matching commands as failures, otherwise,
44 // omit failing.
45 bool omit_failure;
46 // Count the number of times the criterion is matched.
47 int match_count;
48
49 // Don't take into account match_count.
50 bool operator==(const Criterion& c) const {
51 return (std::equal(keywords.begin(), keywords.end(),
52 c.keywords.begin()) &&
53 (repeat == c.repeat) &&
54 (c.omit_failure == omit_failure));
55 }
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_