-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommand_spec.rb
158 lines (133 loc) · 4.82 KB
/
command_spec.rb
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
require File.join(File.dirname(__FILE__), "spec_helper")
require File.join(File.dirname(__FILE__), "..", "detect_http_attack")
describe "DetectHttpAttack" do
describe 'command arguments' do
let(:fixture_file) { File.join(File.dirname(__FILE__), "fixtures", "command_spec.log") }
context 'when args specified -h' do
it "show usage" do
usage = <<EOS
Usage: rspec [options]
-ltsv Log type is LTSV
-n notify when detecting attack
-s COUNT Specify minimum sequential count
-i SECONDS Specify maximum interval seconds
-f CONFFILE Specify configuration file
EOS
test {
begin
DetectHttpAttack.main ["-h"]
rescue SystemExit => e
end
}.should eq(usage)
end
end
context 'when args specified -s 2 -i 2' do
it "detect attack minimum 2 times for each interval within 2 sec." do
output = <<EOS
\e[36m\e[1m10.0.0.1\e[0m\t\e[35m2\e[0m\t\e[32mAgent/5.0\e[0m
2013-12-18T06:25:00+09:00\t200\t/path1\t-
2013-12-18T06:25:02+09:00\t200\t/path3\t-
EOS
test(fixture_file) {
DetectHttpAttack.main ["-s", "2", "-i", "2"]
}.should eq(output)
end
end
context 'when args specified -s 2 -i 4' do
it "detect attack minimum 2 times for each interval within 4 sec." do
output = <<EOS
\e[36m\e[1m10.0.0.1\e[0m\t\e[35m3\e[0m\t\e[32mAgent/5.0\e[0m
2013-12-18T06:25:00+09:00\t200\t/path1\t-
2013-12-18T06:25:02+09:00\t200\t/path3\t-
2013-12-18T06:25:06+09:00\t200\t/path4\t-
EOS
test(fixture_file) {
DetectHttpAttack.main ["-s", "2", "-i", "4"]
}.should eq(output)
end
end
context 'when args specified -s 3 -i 4' do
it "detect attack minimum 3 times for each interval within 4 sec." do
output = <<EOS
\e[36m\e[1m10.0.0.1\e[0m\t\e[35m3\e[0m\t\e[32mAgent/5.0\e[0m
2013-12-18T06:25:00+09:00\t200\t/path1\t-
2013-12-18T06:25:02+09:00\t200\t/path3\t-
2013-12-18T06:25:06+09:00\t200\t/path4\t-
EOS
test(fixture_file) {
DetectHttpAttack.main ["-s", "3", "-i", "4"]
}.should eq(output)
end
end
context 'when args specified -n -s 3 -i 4' do
it "detect and notify attack minimum 3 times for each interval within 4 sec." do
output = <<EOS
\e[36m\e[1m10.0.0.1\e[0m\t\e[35m3\e[0m\t\e[32mAgent/5.0\e[0m
2013-12-18T06:25:00+09:00\t200\t/path1\t-
2013-12-18T06:25:02+09:00\t200\t/path3\t-
2013-12-18T06:25:06+09:00\t200\t/path4\t-
EOS
outerr = <<EOS
\e[31m\e[1m10.0.0.1\e[0m\t\e[33m\e[1m3\e[0m\tAgent/5.0
2013-12-18T06:25:06+09:00\t200\t/path4\t-
EOS
stdout, stderr = test_with_err(fixture_file) {
DetectHttpAttack.main ["-n", "-s", "3", "-i", "4"]
}
stdout.should eq(output)
stderr.should eq(outerr)
end
end
context 'when args specified -f custom conf file' do
let(:conf_file) { File.join(File.dirname(__FILE__), "fixtures", "command_spec.conf") }
it 'should success without error' do
output = <<EOS
\e[36m\e[1m10.1.0.1\e[0m\t\e[35m1\e[0m\t\e[32mAgent/5.0\e[0m
20131218062501\t200\t/path2\t-
EOS
test(fixture_file) {
DetectHttpAttack.main ["-f", conf_file, "-s", "1"]
}.should eq(output)
end
end
let(:fixture_ltsv_file) { File.join(File.dirname(__FILE__), "fixtures", "command_spec.ltsv") }
context 'when args specified -ltsv -s 2 -i 2' do
it "detect attack minimum 2 times for each interval within 2 sec." do
output = <<EOS
\e[36m\e[1m10.0.0.1\e[0m\t\e[35m2\e[0m\t\e[32mAgent/5.0\e[0m
2013-12-18T06:25:00+09:00\t200\t/path1\t-
2013-12-18T06:25:02+09:00\t200\t/path3\t-
EOS
test(fixture_ltsv_file) {
DetectHttpAttack.main ["-ltsv", "-s", "2", "-i", "2"]
}.should eq(output)
end
end
context 'when args specified -s 2 -ltsv -i 4' do
it "detect attack minimum 2 times for each interval within 4 sec." do
output = <<EOS
\e[36m\e[1m10.0.0.1\e[0m\t\e[35m3\e[0m\t\e[32mAgent/5.0\e[0m
2013-12-18T06:25:00+09:00\t200\t/path1\t-
2013-12-18T06:25:02+09:00\t200\t/path3\t-
2013-12-18T06:25:06+09:00\t200\t/path4\t-
EOS
test(fixture_ltsv_file) {
DetectHttpAttack.main ["-s", "2", "-ltsv", "-i", "4"]
}.should eq(output)
end
end
context 'when args specified -s 3 -i 4 -ltsv' do
it "detect attack minimum 3 times for each interval within 4 sec." do
output = <<EOS
\e[36m\e[1m10.0.0.1\e[0m\t\e[35m3\e[0m\t\e[32mAgent/5.0\e[0m
2013-12-18T06:25:00+09:00\t200\t/path1\t-
2013-12-18T06:25:02+09:00\t200\t/path3\t-
2013-12-18T06:25:06+09:00\t200\t/path4\t-
EOS
test(fixture_ltsv_file) {
DetectHttpAttack.main ["-s", "3", "-i", "4", "-ltsv"]
}.should eq(output)
end
end
end
end