@@ -110,6 +110,94 @@ func TestControl_GetHostInfoByVpnIp(t *testing.T) {
110
110
})
111
111
}
112
112
113
+ func TestListHostMapHostsIter (t * testing.T ) {
114
+ l := logrus .New ()
115
+ hm := newHostMap (l , netip.Prefix {})
116
+ hm .preferredRanges .Store (& []netip.Prefix {})
117
+
118
+ hosts := []struct {
119
+ vpnIp netip.Addr
120
+ remoteAddr netip.AddrPort
121
+ localIndexId uint32
122
+ remoteIndexId uint32
123
+ }{
124
+ {vpnIp : netip .MustParseAddr ("0.0.0.2" ), remoteAddr : netip .MustParseAddrPort ("0.0.0.101:4445" ), localIndexId : 202 , remoteIndexId : 201 },
125
+ {vpnIp : netip .MustParseAddr ("0.0.0.3" ), remoteAddr : netip .MustParseAddrPort ("0.0.0.102:4446" ), localIndexId : 203 , remoteIndexId : 202 },
126
+ {vpnIp : netip .MustParseAddr ("0.0.0.4" ), remoteAddr : netip .MustParseAddrPort ("0.0.0.103:4447" ), localIndexId : 204 , remoteIndexId : 203 },
127
+ }
128
+
129
+ for _ , h := range hosts {
130
+ hm .unlockedAddHostInfo (& HostInfo {
131
+ remote : h .remoteAddr ,
132
+ ConnectionState : & ConnectionState {
133
+ peerCert : nil ,
134
+ },
135
+ localIndexId : h .localIndexId ,
136
+ remoteIndexId : h .remoteIndexId ,
137
+ vpnIp : h .vpnIp ,
138
+ }, & Interface {})
139
+ }
140
+
141
+ iter := listHostMapHostsIter (hm )
142
+ var results []ControlHostInfo
143
+
144
+ for h := range iter {
145
+ results = append (results , * h )
146
+ }
147
+
148
+ assert .Equal (t , len (hosts ), len (results ), "expected number of hosts in iterator" )
149
+ for i , h := range hosts {
150
+ assert .Equal (t , h .vpnIp , results [i ].VpnIp )
151
+ assert .Equal (t , h .localIndexId , results [i ].LocalIndex )
152
+ assert .Equal (t , h .remoteIndexId , results [i ].RemoteIndex )
153
+ assert .Equal (t , h .remoteAddr , results [i ].CurrentRemote )
154
+ }
155
+ }
156
+
157
+ func TestListHostMapIndexesIter (t * testing.T ) {
158
+ l := logrus .New ()
159
+ hm := newHostMap (l , netip.Prefix {})
160
+ hm .preferredRanges .Store (& []netip.Prefix {})
161
+
162
+ hosts := []struct {
163
+ vpnIp netip.Addr
164
+ remoteAddr netip.AddrPort
165
+ localIndexId uint32
166
+ remoteIndexId uint32
167
+ }{
168
+ {vpnIp : netip .MustParseAddr ("0.0.0.2" ), remoteAddr : netip .MustParseAddrPort ("0.0.0.101:4445" ), localIndexId : 202 , remoteIndexId : 201 },
169
+ {vpnIp : netip .MustParseAddr ("0.0.0.3" ), remoteAddr : netip .MustParseAddrPort ("0.0.0.102:4446" ), localIndexId : 203 , remoteIndexId : 202 },
170
+ {vpnIp : netip .MustParseAddr ("0.0.0.4" ), remoteAddr : netip .MustParseAddrPort ("0.0.0.103:4447" ), localIndexId : 204 , remoteIndexId : 203 },
171
+ }
172
+
173
+ for _ , h := range hosts {
174
+ hm .unlockedAddHostInfo (& HostInfo {
175
+ remote : h .remoteAddr ,
176
+ ConnectionState : & ConnectionState {
177
+ peerCert : nil ,
178
+ },
179
+ localIndexId : h .localIndexId ,
180
+ remoteIndexId : h .remoteIndexId ,
181
+ vpnIp : h .vpnIp ,
182
+ }, & Interface {})
183
+ }
184
+
185
+ iter := listHostMapIndexesIter (hm )
186
+ var results []ControlHostInfo
187
+
188
+ for h := range iter {
189
+ results = append (results , * h )
190
+ }
191
+
192
+ assert .Equal (t , len (hosts ), len (results ), "expected number of hosts in iterator" )
193
+ for i , h := range hosts {
194
+ assert .Equal (t , h .vpnIp , results [i ].VpnIp )
195
+ assert .Equal (t , h .localIndexId , results [i ].LocalIndex )
196
+ assert .Equal (t , h .remoteIndexId , results [i ].RemoteIndex )
197
+ assert .Equal (t , h .remoteAddr , results [i ].CurrentRemote )
198
+ }
199
+ }
200
+
113
201
func assertFields (t * testing.T , expected []string , actualStruct interface {}) {
114
202
val := reflect .ValueOf (actualStruct ).Elem ()
115
203
fields := make ([]string , val .NumField ())
0 commit comments