-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocli_test.go
67 lines (52 loc) · 1.32 KB
/
geocli_test.go
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
package main
import (
"io/ioutil"
"log"
"os"
"testing"
)
type teststruct struct {
name string
have string
want string
}
var tests = []teststruct{
{"soleil", "193.51.24.1", "193.51.24.1 [soleil.uvsq.fr.] France, FR, local"},
{"network", "193.51.24.0", "193.51.24.0 [unknown] France, FR, local"},
{"Private ip", "192.168.0.0", "192.168.0.0 [unknown] , local"},
{"not local", "101.68.211.3", "101.68.211.3 [unknown] China, CN"},
{"litterals", "should not work", ""}, // produce log errors
}
func Test_parse(t *testing.T) {
db := initdb()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseandprint(tt.have, db); got != tt.want {
t.Errorf("parseandprint(%s) => |%v| != expected |%v|", tt.have, got, tt.want)
}
})
}
db.Close()
}
func Test_readandprintbulk(t *testing.T) {
content := []byte("193.51.24.1")
tmpfile, err := ioutil.TempFile("", "testfile_tmp")
if err != nil {
log.Fatal(err)
}
db := initdb()
defer os.Remove(tmpfile.Name()) // clean up
if _, err := tmpfile.Write(content); err != nil {
log.Fatal(err)
}
if _, err := tmpfile.Seek(0, 0); err != nil {
log.Fatal(err)
}
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }() // Restore original Stdin
os.Stdin = tmpfile
readandprintbulk(db)
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}
}