1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*use std::{
fs,
io::{self, BufReader, Cursor, Read},
};
use host_utils::App;
fn block_valid_hosts(args: Vec<(&str, Vec<&str>, &str)>) {
let mut stdout = io::sink();
let mut stderr = io::sink();
for (index, (etc_hosts, args, want_etc_hosts)) in args.into_iter().enumerate() {
let mut app =
App::new::<BufReader<fs::File>>(etc_hosts, None, &mut stdout, &mut stderr).unwrap();
app.add_block(args);
let mut etc_hosts_file = Cursor::new(Vec::<u8>::new());
let mut db = Cursor::new(Vec::<u8>::new());
app.save::<Cursor<Vec<u8>>, Cursor<Vec<u8>>>(&mut etc_hosts_file, &mut db)
.unwrap();
db.set_position(0);
etc_hosts_file.set_position(0);
let mut got_hosts = String::new();
etc_hosts_file.read_to_string(&mut got_hosts).unwrap();
if got_hosts.as_str() != want_etc_hosts {
eprintln!("Test Faild Index: {index}");
}
assert_eq!(got_hosts.as_str(), want_etc_hosts);
}
}
#[test]
fn test_block_valid_host() {
let tests = vec![
(
"",
vec![],
r#"#host-rs-beg#
#host-rs-end#
#r-host-rs-beg#
#r-host-rs-end#
"#,
),
(
"",
vec!["example.com"],
r#"#host-rs-beg#
0.0.0.0 example.com
#host-rs-end#
#r-host-rs-beg#
#r-host-rs-end#
"#,
),
(
"",
vec!["example.com", "github.com"],
r#"#host-rs-beg#
0.0.0.0 example.com
0.0.0.0 github.com
#host-rs-end#
#r-host-rs-beg#
#r-host-rs-end#
"#,
),
(
r#"#r-host-rs-beg#
#r-host-rs-end#
#host-rs-beg#
#host-rs-end#
"#,
vec![],
r#"#host-rs-beg#
#host-rs-end#
#r-host-rs-beg#
#r-host-rs-end#
"#,
),
(
r#"127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters"#,
vec![
"example.com",
"test.in",
"120.54.33.10",
"33.70.100.45",
"www.abc.xyz",
],
r#"127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
#host-rs-beg#
0.0.0.0 120.54.33.10
0.0.0.0 33.70.100.45
0.0.0.0 example.com
0.0.0.0 test.in
0.0.0.0 www.abc.xyz
#host-rs-end#
#r-host-rs-beg#
#r-host-rs-end#
"#,
),
(
r#"#host-rs-beg#
#host-rs-end#
127.0.0.1 localhost"#,
vec!["example.com", "abc.com", "xyz.com"],
r#"127.0.0.1 localhost
#host-rs-beg#
0.0.0.0 abc.com
0.0.0.0 example.com
0.0.0.0 xyz.com
#host-rs-end#
#r-host-rs-beg#
#r-host-rs-end#
"#,
),
(
r#"#host-rs-beg#
#host-rs-end#
127.0.0.1 localhost
# comment
#r-host-rs-beg#
#r-host-rs-end#""#,
vec!["example.com", "abc.com", "xyz.com"],
r#"127.0.0.1 localhost
# comment
#host-rs-beg#
0.0.0.0 abc.com
0.0.0.0 example.com
0.0.0.0 xyz.com
#host-rs-end#
#r-host-rs-beg#
#r-host-rs-end#
"#,
),
(
r#"127.0.0.1 localhost
#r-host-rs-beg#
#r-host-rs-end#
#host-rs-beg#
0.0.0.0 xyz.in
0.0.0.0 helloworld.com
0.0.0.0 github.com
#host-rs-end#
"#,
vec!["example.com", "www.google.com"],
r#"127.0.0.1 localhost
#host-rs-beg#
0.0.0.0 example.com
0.0.0.0 github.com
0.0.0.0 helloworld.com
0.0.0.0 www.google.com
0.0.0.0 xyz.in
#host-rs-end#
#r-host-rs-beg#
#r-host-rs-end#
"#,
),
];
block_valid_hosts(tests);
}*/