Repository navigation
Expand file tree
/
Copy pathcclogconv_test.go
More file actions
176 lines (143 loc) · 4.81 KB
/
Copy pathcclogconv_test.go
File metadata and controls
176 lines (143 loc) · 4.81 KB
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
173
174
175
176
package cclogconv
import (
"bytes"
"io/ioutil"
"os"
"strings"
"testing"
)
func TestRun_parseError(t *testing.T) {
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
cl := CCLogConv{Out: outStream, Err: errStream}
args := strings.Split("cclogconv --broken", " ")
exitCode := cl.Run(args[1:])
if exitCode != ExitCodeError {
t.Errorf("expected %d to eq %d", exitCode, ExitCodeError)
}
expected := "flag provided but not defined: -broken"
if !strings.Contains(errStream.String(), expected) {
t.Fatalf("expected %q to contain %q", errStream.String(), expected)
}
}
func TestRun_nFlagError(t *testing.T) {
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
cl := CCLogConv{Out: outStream, Err: errStream}
args := strings.Split("cclogconv -n", " ")
exitCode := cl.Run(args[1:])
if exitCode != ExitCodeError {
t.Errorf("expected %d to eq %d", exitCode, ExitCodeError)
}
expected := "n option must be used with cc option."
if !strings.Contains(errStream.String(), expected) {
t.Fatalf("expected %q to contain %q", errStream.String(), expected)
}
}
func TestRun_vFlagError(t *testing.T) {
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
cl := CCLogConv{Out: outStream, Err: errStream}
args := strings.Split("cclogconv -v", " ")
exitCode := cl.Run(args[1:])
if exitCode != ExitCodeError {
t.Errorf("expected %d to eq %d", exitCode, ExitCodeError)
}
expected := "v option must be used with cc option."
if !strings.Contains(errStream.String(), expected) {
t.Fatalf("expected %q to contain %q", errStream.String(), expected)
}
}
func TestRun_filter_noflags(t *testing.T) {
out := new(bytes.Buffer)
in := bytes.NewBufferString("foo bar 182.22.59.229 aaa bbb\nboo moo 98.138.219.231 ccc def\n")
filter := filter{
in: in,
out: out,
}
err := filter.start("test/tmp/GeoLite2-Country_20181113/GeoLite2-Country.mmdb", "", false, false)
if err != nil {
t.Errorf("expected %v to eq nil", err)
}
expected := []byte("foo bar JP 182.22.59.229 aaa bbb\nboo moo US 98.138.219.231 ccc def\n")
if bytes.Compare(expected, out.Bytes()) != 0 {
t.Errorf("expected \n%#v to eq \n%#v", out.Bytes(), expected)
}
}
func TestRun_filter_with_ccOpt(t *testing.T) {
out := new(bytes.Buffer)
in := bytes.NewBufferString("foo bar 182.22.59.229 aaa bbb\nboo moo 98.138.219.231 ccc def\n")
filter := filter{
in: in,
out: out,
}
err := filter.start("test/tmp/GeoLite2-Country_20181113/GeoLite2-Country.mmdb", "JP", false, false)
if err != nil {
t.Errorf("expected %v to eq nil", err)
}
expected := []byte("foo bar JP 182.22.59.229 aaa bbb\n")
if bytes.Compare(expected, out.Bytes()) != 0 {
t.Errorf("expected \n%#v to eq \n%#v", out.Bytes(), expected)
}
}
func TestRun_filter_with_ccOpt_nFlag(t *testing.T) {
out := new(bytes.Buffer)
in := bytes.NewBufferString("foo bar 182.22.59.229 aaa bbb\nboo moo 98.138.219.231 ccc def\n")
filter := filter{
in: in,
out: out,
}
err := filter.start("test/tmp/GeoLite2-Country_20181113/GeoLite2-Country.mmdb", "JP", true, false)
if err != nil {
t.Errorf("expected %v to eq nil", err)
}
expected := []byte("foo bar 182.22.59.229 aaa bbb\n")
if bytes.Compare(expected, out.Bytes()) != 0 {
t.Errorf("expected \n%#v to eq \n%#v", out.Bytes(), expected)
}
}
func TestRun_filter_with_ccOpt_vFlag(t *testing.T) {
out := new(bytes.Buffer)
in := bytes.NewBufferString("foo bar 182.22.59.229 aaa bbb\nboo moo 98.138.219.231 ccc def\n")
filter := filter{
in: in,
out: out,
}
err := filter.start("test/tmp/GeoLite2-Country_20181113/GeoLite2-Country.mmdb", "JP", false, true)
if err != nil {
t.Errorf("expected %v to eq nil", err)
}
expected := []byte("boo moo US 98.138.219.231 ccc def\n")
if bytes.Compare(expected, out.Bytes()) != 0 {
t.Errorf("expected \n%#v to eq \n%#v", out.Bytes(), expected)
}
}
func TestRun_filter_with_ccOpt_nFlag_vFlag(t *testing.T) {
out := new(bytes.Buffer)
in := bytes.NewBufferString("foo bar 182.22.59.229 aaa bbb\nboo moo 98.138.219.231 ccc def\n")
filter := filter{
in: in,
out: out,
}
err := filter.start("test/tmp/GeoLite2-Country_20181113/GeoLite2-Country.mmdb", "JP", true, true)
if err != nil {
t.Errorf("expected %v to eq nil", err)
}
expected := []byte("boo moo 98.138.219.231 ccc def\n")
if bytes.Compare(expected, out.Bytes()) != 0 {
t.Errorf("expected \n%#v to eq \n%#v", out.Bytes(), expected)
}
}
func TestRun_filter_manyLines(t *testing.T) {
out := new(bytes.Buffer)
in, _ := os.Open("test/tmp/test.log")
filter := filter{
in: in,
out: out,
}
err := filter.start("test/tmp/GeoLite2-Country_20181113/GeoLite2-Country.mmdb", "JP", false, false)
if err != nil {
t.Errorf("expected %v to eq nil", err)
}
expected, _ := ioutil.ReadFile("test/tmp/expected_JP.txt")
if bytes.Compare(expected, out.Bytes()) != 0 {
t.Errorf("expected \n%#v to eq \n%#v", out.Bytes(), expected)
}
}