|
1 | | -package connection_test |
| 1 | +package connection |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
| 5 | + "net" |
| 6 | + "reflect" |
4 | 7 | "strings" |
5 | 8 | "testing" |
6 | 9 |
|
7 | | - "github.com/m-lab/traceroute-caller/connection" |
| 10 | + "github.com/m-lab/go/rtx" |
| 11 | + "github.com/m-lab/tcp-info/inetdiag" |
8 | 12 | ) |
9 | 13 |
|
10 | 14 | func TestUUID(t *testing.T) { |
11 | | - conn := connection.Connection{Cookie: "1be3"} |
| 15 | + conn := Connection{Cookie: "1be3"} |
12 | 16 | tmp, err := conn.UUID() |
13 | 17 | s := strings.Split(tmp, "_") |
14 | 18 | if err != nil || s[len(s)-1] != "0000000000001BE3" { |
15 | 19 | t.Error("Make uuid from cookie incorrect") |
16 | 20 | } |
17 | 21 | } |
| 22 | + |
| 23 | +func TestSrcDestSwap(t *testing.T) { |
| 24 | + oldFunc := netInterfaceAddrs |
| 25 | + defer func() { netInterfaceAddrs = oldFunc }() |
| 26 | + |
| 27 | + netInterfaceAddrs = func() ([]net.Addr, error) { |
| 28 | + _, nw, err := net.ParseCIDR("127.0.0.1/8") |
| 29 | + rtx.Must(err, "could not parse test nw") |
| 30 | + ip1, err := net.ResolveIPAddr("ip6", "::1") |
| 31 | + rtx.Must(err, "Could not resolve ::1") |
| 32 | + ip2, err := net.ResolveIPAddr("ip4", "1.2.3.4") |
| 33 | + rtx.Must(err, "Could not resolve 1.2.3.4") |
| 34 | + return []net.Addr{ |
| 35 | + nw, |
| 36 | + ip1, |
| 37 | + ip2, |
| 38 | + }, nil |
| 39 | + } |
| 40 | + |
| 41 | + c, err := NewCreator() |
| 42 | + rtx.Must(err, "Could not use fake netInterfaceAddrs") |
| 43 | + |
| 44 | + tests := []struct { |
| 45 | + name string |
| 46 | + sockid inetdiag.SockID |
| 47 | + want Connection |
| 48 | + wantErr bool |
| 49 | + }{ |
| 50 | + { |
| 51 | + name: "From local to remote", |
| 52 | + sockid: inetdiag.SockID{ |
| 53 | + SrcIP: "1.2.3.4", |
| 54 | + SPort: 5, |
| 55 | + DstIP: "7.8.9.10", |
| 56 | + DPort: 11, |
| 57 | + Cookie: 0xc, |
| 58 | + }, |
| 59 | + want: Connection{ |
| 60 | + LocalIP: "1.2.3.4", |
| 61 | + LocalPort: 5, |
| 62 | + RemoteIP: "7.8.9.10", |
| 63 | + RemotePort: 11, |
| 64 | + Cookie: "c", |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "From remote to local", |
| 69 | + sockid: inetdiag.SockID{ |
| 70 | + DstIP: "1.2.3.4", |
| 71 | + DPort: 5, |
| 72 | + SrcIP: "7.8.9.10", |
| 73 | + SPort: 11, |
| 74 | + Cookie: 0xc, |
| 75 | + }, |
| 76 | + want: Connection{ |
| 77 | + LocalIP: "1.2.3.4", |
| 78 | + LocalPort: 5, |
| 79 | + RemoteIP: "7.8.9.10", |
| 80 | + RemotePort: 11, |
| 81 | + Cookie: "c", |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "Nonlocal connection", |
| 86 | + sockid: inetdiag.SockID{ |
| 87 | + DstIP: "13.14.15.16", |
| 88 | + DPort: 17, |
| 89 | + SrcIP: "7.8.9.10", |
| 90 | + SPort: 11, |
| 91 | + Cookie: 0xc, |
| 92 | + }, |
| 93 | + wantErr: true, |
| 94 | + }, |
| 95 | + } |
| 96 | + for _, tt := range tests { |
| 97 | + t.Run(tt.name, func(t *testing.T) { |
| 98 | + got, err := c.FromSockID(tt.sockid) |
| 99 | + if (err != nil) != tt.wantErr { |
| 100 | + t.Errorf("creator.FromSockID() error = %v, wantErr %v", err, tt.wantErr) |
| 101 | + return |
| 102 | + } |
| 103 | + if !reflect.DeepEqual(got, tt.want) { |
| 104 | + t.Errorf("creator.FromSockID() = %v, want %v", got, tt.want) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | + c = NewFakeCreator([]*net.IP{}) // Just call it to make sure it doesn't crash. |
| 110 | +} |
| 111 | + |
| 112 | +type fakeIP struct{} |
| 113 | + |
| 114 | +func (fakeIP) String() string { return "" } |
| 115 | +func (fakeIP) Network() string { return "" } |
| 116 | + |
| 117 | +func TestNewCreatorWithBadInterfaceAddrs(t *testing.T) { |
| 118 | + oldFunc := netInterfaceAddrs |
| 119 | + defer func() { netInterfaceAddrs = oldFunc }() |
| 120 | + |
| 121 | + netInterfaceAddrs = func() ([]net.Addr, error) { |
| 122 | + return nil, errors.New("error for testing") |
| 123 | + } |
| 124 | + |
| 125 | + _, err := NewCreator() |
| 126 | + if err == nil { |
| 127 | + t.Error("Should have had an error but was nil") |
| 128 | + } |
| 129 | + |
| 130 | + netInterfaceAddrs = func() ([]net.Addr, error) { |
| 131 | + return []net.Addr{fakeIP{}}, nil |
| 132 | + } |
| 133 | + |
| 134 | + _, err = NewCreator() |
| 135 | + if err == nil { |
| 136 | + t.Error("Should have had an error but was nil") |
| 137 | + } |
| 138 | +} |
0 commit comments