Debian Codegraph @

tests/iptables_test.rs

raw symbols SCIP
1 extern crate iptables;
2
3 use std::panic;
4
5 #[test]
6 fn test_new() {
7 nat(iptables::new(false).unwrap(), "NATNEW", "NATNEW2");
8 filter(iptables::new(false).unwrap(), "FILTERNEW");
9 }
10
11 #[test]
12 fn test_old() {
13 nat(
14 iptables::IPTables {
15 cmd: "iptables",
16 has_wait: false,
17 has_check: false,
18 is_numeric: false,
19 },
20 "NATOLD",
21 "NATOLD2",
22 );
23
24 filter(
25 iptables::IPTables {
26 cmd: "iptables",
27 has_wait: false,
28 has_check: false,
29 is_numeric: false,
30 },
31 "FILTEROLD",
32 );
33 }
34
35 fn nat(ipt: iptables::IPTables, old_name: &str, new_name: &str) {
36 assert!(ipt.new_chain("nat", old_name).is_ok());
37 assert!(ipt.rename_chain("nat", old_name, new_name).is_ok());
38 assert!(ipt.append("nat", new_name, "-j ACCEPT").is_ok());
39 assert!(ipt.exists("nat", new_name, "-j ACCEPT").unwrap());
40 assert!(ipt.delete("nat", new_name, "-j ACCEPT").is_ok());
41 assert!(ipt.insert("nat", new_name, "-j ACCEPT", 1).is_ok());
42 assert!(
43 ipt.append(
44 "nat",
45 new_name,
46 "-m comment --comment \"double-quoted comment\" -j ACCEPT"
47 )
48 .is_ok(),
49 );
50 assert!(
51 ipt.exists(
52 "nat",
53 new_name,
54 "-m comment --comment \"double-quoted comment\" -j ACCEPT"
55 )
56 .unwrap(),
57 );
58 assert!(
59 ipt.append(
60 "nat",
61 new_name,
62 "-m comment --comment 'single-quoted comment' -j ACCEPT"
63 )
64 .is_ok(),
65 );
66 // The following `exists`-check has to use double-quotes, since the iptables output (if it
67 // doesn't have the check-functionality) will use double quotes.
68 assert!(
69 ipt.exists(
70 "nat",
71 new_name,
72 "-m comment --comment \"single-quoted comment\" -j ACCEPT"
73 )
74 .unwrap(),
75 );
76 assert!(ipt.flush_chain("nat", new_name).is_ok());
77 assert!(!ipt.exists("nat", new_name, "-j ACCEPT").unwrap());
78 assert!(
79 ipt.execute("nat", &format!("-A {} -j ACCEPT", new_name))
80 .is_ok()
81 );
82 assert!(ipt.exists("nat", new_name, "-j ACCEPT").unwrap());
83 assert!(ipt.flush_chain("nat", new_name).is_ok());
84 assert!(ipt.chain_exists("nat", new_name).unwrap());
85 assert!(ipt.delete_chain("nat", new_name).is_ok());
86 assert!(!ipt.chain_exists("nat", new_name).unwrap());
87 }
88
89 fn filter(ipt: iptables::IPTables, name: &str) {
90 assert!(ipt.new_chain("filter", name).is_ok());
91 assert!(ipt.insert("filter", name, "-j ACCEPT", 1).is_ok());
92 assert!(ipt.replace("filter", name, "-j DROP", 1).is_ok());
93 assert!(ipt.exists("filter", name, "-j DROP").unwrap());
94 assert!(!ipt.exists("filter", name, "-j ACCEPT").unwrap());
95 assert!(ipt.delete("filter", name, "-j DROP").is_ok());
96 assert_eq!(ipt.list("filter", name).unwrap().len(), 1);
97 assert!(
98 ipt.execute("filter", &format!("-A {} -j ACCEPT", name))
99 .is_ok()
100 );
101 assert!(ipt.exists("filter", name, "-j ACCEPT").unwrap());
102 assert!(
103 ipt.append(
104 "filter",
105 name,
106 "-m comment --comment \"double-quoted comment\" -j ACCEPT"
107 )
108 .is_ok(),
109 );
110 assert!(
111 ipt.exists(
112 "filter",
113 name,
114 "-m comment --comment \"double-quoted comment\" -j ACCEPT"
115 )
116 .unwrap()
117 );
118 assert!(
119 ipt.append(
120 "filter",
121 name,
122 "-m comment --comment 'single-quoted comment' -j ACCEPT"
123 )
124 .is_ok(),
125 );
126 // The following `exists`-check has to use double-quotes, since the iptables output (if it
127 // doesn't have the check-functionality) will use double quotes.
128 assert!(
129 ipt.exists(
130 "filter",
131 name,
132 "-m comment --comment \"single-quoted comment\" -j ACCEPT"
133 )
134 .unwrap(),
135 );
136 assert!(ipt.flush_chain("filter", name).is_ok());
137 assert!(ipt.chain_exists("filter", name).unwrap());
138 assert!(ipt.delete_chain("filter", name).is_ok());
139 assert!(!ipt.chain_exists("filter", name).unwrap());
140 }
141
142 #[test]
143 fn test_get_policy() {
144 let ipt = iptables::new(false).unwrap();
145
146 // filter
147 assert!(ipt.get_policy("filter", "INPUT").is_ok());
148 assert!(ipt.get_policy("filter", "FORWARD").is_ok());
149 assert!(ipt.get_policy("filter", "OUTPUT").is_ok());
150 // mangle
151 assert!(ipt.get_policy("mangle", "PREROUTING").is_ok());
152 assert!(ipt.get_policy("mangle", "OUTPUT").is_ok());
153 assert!(ipt.get_policy("mangle", "INPUT").is_ok());
154 assert!(ipt.get_policy("mangle", "FORWARD").is_ok());
155 assert!(ipt.get_policy("mangle", "POSTROUTING").is_ok());
156 // nat
157 assert!(ipt.get_policy("nat", "PREROUTING").is_ok());
158 assert!(ipt.get_policy("nat", "POSTROUTING").is_ok());
159 assert!(ipt.get_policy("nat", "OUTPUT").is_ok());
160 // raw
161 assert!(ipt.get_policy("raw", "PREROUTING").is_ok());
162 assert!(ipt.get_policy("raw", "OUTPUT").is_ok());
163 // security
164 assert!(ipt.get_policy("security", "INPUT").is_ok());
165 assert!(ipt.get_policy("security", "OUTPUT").is_ok());
166 assert!(ipt.get_policy("security", "FORWARD").is_ok());
167
168 // Wrong table
169 assert!(ipt.get_policy("not_existant", "_").is_err());
170 // Wrong chain
171 assert!(ipt.get_policy("filter", "_").is_err());
172 }
173
174 #[test]
175 #[ignore]
176 fn test_set_policy() {
177 let ipt = iptables::new(false).unwrap();
178
179 // Since we can only set policies on built-in chains, we have to retain the policy of the chain
180 // before setting it, to restore it to its original state.
181 let current_policy = ipt.get_policy("mangle", "FORWARD").unwrap();
182
183 // If the following assertions fail or any other panic occurs, we still have to ensure not to
184 // change the policy for the user.
185 let result = panic::catch_unwind(|| {
186 assert!(ipt.set_policy("mangle", "FORWARD", "DROP").is_ok());
187 assert_eq!(ipt.get_policy("mangle", "FORWARD").unwrap(), "DROP");
188 });
189
190 // Reset the policy to the retained value
191 ipt.set_policy("mangle", "FORWARD", &current_policy)
192 .unwrap();
193
194 // "Rethrow" a potential caught panic
195 assert!(result.is_ok());
196 }