@@ -21,12 +21,15 @@ import (
21
21
"net"
22
22
"net/http"
23
23
"net/url"
24
+ "strconv"
24
25
"strings"
25
26
"testing"
26
27
27
- "github.com/netsec-ethz/scion-apps/pkg/appnet"
28
- "github.com/scionproto/scion/go/lib/addr"
29
- "github.com/scionproto/scion/go/lib/snet"
28
+ "github.com/stretchr/testify/assert"
29
+ "github.com/stretchr/testify/require"
30
+ "inet.af/netaddr"
31
+
32
+ "github.com/netsec-ethz/scion-apps/pkg/pan"
30
33
)
31
34
32
35
func TestMangleSCIONAddrURL (t * testing.T ) {
@@ -63,35 +66,15 @@ func TestMangleSCIONAddrURL(t *testing.T) {
63
66
if _ , _ , err := net .SplitHostPort (u .Host ); err != nil {
64
67
continue
65
68
}
66
- unmangled := appnet .UnmangleSCIONAddr (u .Host )
69
+ unmangled := pan .UnmangleSCIONAddr (u .Host )
67
70
if unmangled != tc .HostPort {
68
71
t .Fatalf ("UnmangleSCIONAddr('%s') returned different result, actual='%s', expected='%s'" , u .Host , unmangled , tc .HostPort )
69
72
}
70
73
}
71
74
}
72
75
}
73
76
74
- type mockResolver struct {
75
- table map [string ]string
76
- }
77
-
78
- func (r * mockResolver ) Resolve (name string ) (* snet.SCIONAddress , error ) {
79
- address , ok := r .table [name ]
80
- if ! ok {
81
- return nil , & appnet.HostNotFoundError {Host : name }
82
- } else {
83
- a , err := snet .ParseUDPAddr (address )
84
- if err != nil {
85
- panic (fmt .Sprintf ("test input must parse %s" , err ))
86
- }
87
- return & snet.SCIONAddress {IA : a .IA , Host : addr .HostFromIP (a .Host .IP )}, nil
88
- }
89
- }
90
-
91
77
func TestRoundTripper (t * testing.T ) {
92
-
93
- resolver := & mockResolver {map [string ]string {"host" : "1-ff00:0:1,[192.0.2.1]" }}
94
-
95
78
testCases := []struct {
96
79
HostPort string
97
80
Expected string
@@ -106,22 +89,37 @@ func TestRoundTripper(t *testing.T) {
106
89
}
107
90
108
91
urlPatterns := hostURLPatterns ()
92
+ errJustATest := errors .New ("just a test" )
109
93
110
94
// We replace the actual dial function of the roundtripper with this function that only
111
95
// checks wether the address can be successfully unmangled and resolved.
112
96
// expected will be set in the test loop, below
113
97
var expected string
114
98
testDial := func (ctx context.Context , network , addr string ) (net.Conn , error ) {
115
- unmangled := appnet .UnmangleSCIONAddr (addr )
116
- resolvedAddr , err := appnet .ResolveUDPAddrAt (unmangled , resolver )
117
- if err != nil {
118
- t .Fatalf ("unexpected error when resolving address '%s' in roundtripper: %s" , unmangled , err )
99
+ // The actual Dialer.DialContext does
100
+ // remote, err := pan.ResolveUDPAddr(pan.UnmangleSCIONAddr(addr))
101
+ // We mock pan.ResolveUDPAddr here; don't want to rely on hosts files etc
102
+ // for this test.
103
+ unmangled := pan .UnmangleSCIONAddr (addr )
104
+ if strings .HasPrefix (unmangled , "host" ) {
105
+ _ , err := pan .ParseUDPAddr (unmangled )
106
+ require .Error (t , err )
107
+ hostStr , portStr , err := net .SplitHostPort (unmangled )
108
+ require .NoError (t , err )
109
+ port , err := strconv .Atoi (portStr )
110
+ require .NoError (t , err )
111
+ require .Equal (t , hostStr , "host" )
112
+ hostIA , err := pan .ParseIA ("1-ff00:0:1" )
113
+ require .NoError (t , err )
114
+ hostIP := netaddr .MustParseIP ("192.0.2.1" )
115
+ remote := pan.UDPAddr {IA : hostIA , IP : hostIP , Port : uint16 (port )}
116
+ assert .Equal (t , expected , remote .String ())
117
+ } else {
118
+ remote , err := pan .ParseUDPAddr (unmangled )
119
+ require .NoError (t , err )
120
+ assert .Equal (t , expected , remote .String ())
119
121
}
120
- actual := resolvedAddr .String ()
121
- if actual != expected {
122
- t .Fatalf ("unexpected address resolved in roundtripper, actual='%s', expected='%s'" , actual , expected )
123
- }
124
- return nil , errors .New ("just a test" )
122
+ return nil , errJustATest
125
123
}
126
124
127
125
c := & http.Client {
@@ -136,11 +134,7 @@ func TestRoundTripper(t *testing.T) {
136
134
137
135
url := fmt .Sprintf (urlPattern , tc .HostPort )
138
136
_ , err := c .Get (MangleSCIONAddrURL (url ))
139
- if err == nil {
140
- panic ("unexpected success!" )
141
- } else if ! strings .Contains (err .Error (), "just a test" ) {
142
- t .Fatalf ("unexpected error: %s" , err )
143
- }
137
+ assert .ErrorIs (t , err , errJustATest )
144
138
}
145
139
}
146
140
}
0 commit comments