Android-cuttlefish cvd tool
fake_nftables.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ALLOCD_TEST_FAKE_NFTABLES_H_
18#define ALLOCD_TEST_FAKE_NFTABLES_H_
19
20#include <stdint.h>
21
22#include <functional>
23#include <map>
24#include <string>
25#include <string_view>
26#include <utility>
27#include <vector>
28
29#include "allocd/net/nftables.h"
31
32namespace cuttlefish {
33
34// A stateful, in-memory Nftables test double.
35// Statefulness is needed for testing behaviour around handles.
36class FakeNftables : public Nftables {
37 public:
38 Result<void> EnsureTable(std::string_view family,
39 std::string_view table) override;
40 Result<void> DeleteTable(std::string_view family,
41 std::string_view table) override;
42 Result<void> EnsureChain(std::string_view family, std::string_view table,
43 std::string_view chain,
44 std::string_view content) override;
45 Result<uint64_t> AddRule(std::string_view family, std::string_view table,
46 std::string_view chain, std::string_view content,
47 std::string_view comment) override;
48 Result<void> DeleteRule(std::string_view family, std::string_view table,
49 std::string_view chain, uint64_t handle) override;
50 Result<void> DeleteRulesByComment(std::string_view family,
51 std::string_view table,
52 std::string_view chain,
53 std::string_view comment) override;
54
55 bool HasTable(std::string_view family, std::string_view table) const;
56 bool HasChain(std::string_view family, std::string_view table,
57 std::string_view chain) const;
58 bool HasRuleWithComment(std::string_view family, std::string_view table,
59 std::string_view chain,
60 std::string_view comment) const;
61 int RuleCount(std::string_view family, std::string_view table,
62 std::string_view chain) const;
63
64 private:
65 struct Rule {
66 uint64_t handle;
67 std::string content;
68 std::string comment;
69 };
70 struct Table {
71 std::map<std::string, std::vector<Rule>, std::less<>> chains;
72 uint64_t next_handle = 1;
73 };
74 using TableKey = std::pair<std::string, std::string>;
75
76 const Table* FindTable(std::string_view family, std::string_view table) const;
77 Table* FindTable(std::string_view family, std::string_view table);
78
79 std::map<TableKey, Table> tables_;
80};
81
82} // namespace cuttlefish
83
84#endif // ALLOCD_TEST_FAKE_NFTABLES_H_
Definition: fake_nftables.h:36
Result< void > DeleteRule(std::string_view family, std::string_view table, std::string_view chain, uint64_t handle) override
Definition: fake_nftables.cc:86
Result< void > DeleteRulesByComment(std::string_view family, std::string_view table, std::string_view chain, std::string_view comment) override
Definition: fake_nftables.cc:101
bool HasRuleWithComment(std::string_view family, std::string_view table, std::string_view chain, std::string_view comment) const
Definition: fake_nftables.cc:131
std::pair< std::string, std::string > TableKey
Definition: fake_nftables.h:74
const Table * FindTable(std::string_view family, std::string_view table) const
Definition: fake_nftables.cc:30
std::map< TableKey, Table > tables_
Definition: fake_nftables.h:79
int RuleCount(std::string_view family, std::string_view table, std::string_view chain) const
Definition: fake_nftables.cc:147
Result< void > EnsureTable(std::string_view family, std::string_view table) override
Definition: fake_nftables.cc:42
bool HasTable(std::string_view family, std::string_view table) const
Definition: fake_nftables.cc:120
bool HasChain(std::string_view family, std::string_view table, std::string_view chain) const
Definition: fake_nftables.cc:125
Result< void > EnsureChain(std::string_view family, std::string_view table, std::string_view chain, std::string_view content) override
Definition: fake_nftables.cc:59
Result< uint64_t > AddRule(std::string_view family, std::string_view table, std::string_view chain, std::string_view content, std::string_view comment) override
Definition: fake_nftables.cc:69
Result< void > DeleteTable(std::string_view family, std::string_view table) override
Definition: fake_nftables.cc:48
Definition: nftables.h:28
Definition: alloc_driver.h:20
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
Definition: fake_nftables.h:65
std::string content
Definition: fake_nftables.h:67
std::string comment
Definition: fake_nftables.h:68
uint64_t handle
Definition: fake_nftables.h:66
Definition: fake_nftables.h:70
std::map< std::string, std::vector< Rule >, std::less<> > chains
Definition: fake_nftables.h:71
uint64_t next_handle
Definition: fake_nftables.h:72