-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirr_test.go
More file actions
77 lines (69 loc) · 1.54 KB
/
Copy pathirr_test.go
File metadata and controls
77 lines (69 loc) · 1.54 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
package main
import (
"bufio"
"fmt"
"net"
"reflect"
"strings"
"testing"
)
func stubIRR(t *testing.T, replies map[string]string) *irrClient {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ln.Close() })
go func() {
for {
conn, err := ln.Accept()
if err != nil {
return
}
go func() {
defer conn.Close()
s := bufio.NewScanner(conn)
for s.Scan() {
q := s.Text()
if !strings.HasPrefix(q, "!i") {
continue
}
name := strings.TrimSuffix(strings.TrimPrefix(q, "!i"), ",1")
members, ok := replies[name]
if !ok {
fmt.Fprint(conn, "D\n")
continue
}
fmt.Fprintf(conn, "A%d\n%s\nC\n", len(members)+1, members)
}
}()
}
}()
c := newIRR()
c.server = ln.Addr().String()
return c
}
func TestIsASSet(t *testing.T) {
for name, want := range map[string]bool{
"AS-AKAMAI": true,
"AS1234:AS-CUSTOMER": true,
"AS203923": false,
"AS45090": false,
} {
if got := isASSet(name); got != want {
t.Errorf("isASSet(%q) = %v, want %v", name, got, want)
}
}
}
func TestResolveASNs(t *testing.T) {
c := stubIRR(t, map[string]string{"AS-TEST": "AS1 AS2 AS3"})
got, err := c.resolveASNs([]string{"AS-TEST", "AS3", "AS9"})
if err != nil {
t.Fatal(err)
}
if want := []string{"AS1", "AS2", "AS3", "AS9"}; !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
if _, err := c.resolveASNs([]string{"AS-MISSING"}); err == nil {
t.Error("expected error for unknown as-set")
}
}