|
@ -54,6 +54,16 @@ func extractCertHashes(addr ma.Multiaddr) []string { |
|
|
return certHashesStr |
|
|
return certHashesStr |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func stripCertHashes(addr ma.Multiaddr) ma.Multiaddr { |
|
|
|
|
|
for { |
|
|
|
|
|
_, err := addr.ValueForProtocol(ma.P_CERTHASH) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return addr |
|
|
|
|
|
} |
|
|
|
|
|
addr, _ = ma.SplitLast(addr) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func TestTransport(t *testing.T) { |
|
|
func TestTransport(t *testing.T) { |
|
|
serverID, serverKey := newIdentity(t) |
|
|
serverID, serverKey := newIdentity(t) |
|
|
tr, err := libp2pwebtransport.New(serverKey, nil, network.NullResourceManager) |
|
|
tr, err := libp2pwebtransport.New(serverKey, nil, network.NullResourceManager) |
|
@ -323,15 +333,7 @@ func TestConnectionGaterDialing(t *testing.T) { |
|
|
defer ln.Close() |
|
|
defer ln.Close() |
|
|
|
|
|
|
|
|
connGater.EXPECT().InterceptSecured(network.DirOutbound, serverID, gomock.Any()).Do(func(_ network.Direction, _ peer.ID, addrs network.ConnMultiaddrs) { |
|
|
connGater.EXPECT().InterceptSecured(network.DirOutbound, serverID, gomock.Any()).Do(func(_ network.Direction, _ peer.ID, addrs network.ConnMultiaddrs) { |
|
|
expected := ln.Multiaddr() |
|
|
require.Equal(t, stripCertHashes(ln.Multiaddr()), addrs.RemoteMultiaddr()) |
|
|
for { |
|
|
|
|
|
_, err := expected.ValueForProtocol(ma.P_CERTHASH) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
break |
|
|
|
|
|
} |
|
|
|
|
|
expected, _ = ma.SplitLast(expected) |
|
|
|
|
|
} |
|
|
|
|
|
require.Equal(t, expected, addrs.RemoteMultiaddr()) |
|
|
|
|
|
}) |
|
|
}) |
|
|
_, key := newIdentity(t) |
|
|
_, key := newIdentity(t) |
|
|
cl, err := libp2pwebtransport.New(key, connGater, network.NullResourceManager) |
|
|
cl, err := libp2pwebtransport.New(key, connGater, network.NullResourceManager) |
|
@ -340,3 +342,29 @@ func TestConnectionGaterDialing(t *testing.T) { |
|
|
_, err = cl.Dial(context.Background(), ln.Multiaddr(), serverID) |
|
|
_, err = cl.Dial(context.Background(), ln.Multiaddr(), serverID) |
|
|
require.EqualError(t, err, "secured connection gated") |
|
|
require.EqualError(t, err, "secured connection gated") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestConnectionGaterInterceptAccept(t *testing.T) { |
|
|
|
|
|
ctrl := gomock.NewController(t) |
|
|
|
|
|
defer ctrl.Finish() |
|
|
|
|
|
connGater := NewMockConnectionGater(ctrl) |
|
|
|
|
|
|
|
|
|
|
|
serverID, serverKey := newIdentity(t) |
|
|
|
|
|
tr, err := libp2pwebtransport.New(serverKey, connGater, network.NullResourceManager) |
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
defer tr.(io.Closer).Close() |
|
|
|
|
|
ln, err := tr.Listen(ma.StringCast("/ip4/127.0.0.1/udp/0/quic/webtransport")) |
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
defer ln.Close() |
|
|
|
|
|
|
|
|
|
|
|
connGater.EXPECT().InterceptAccept(gomock.Any()).Do(func(addrs network.ConnMultiaddrs) { |
|
|
|
|
|
require.Equal(t, stripCertHashes(ln.Multiaddr()), addrs.LocalMultiaddr()) |
|
|
|
|
|
require.NotEqual(t, stripCertHashes(ln.Multiaddr()), addrs.RemoteMultiaddr()) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
_, key := newIdentity(t) |
|
|
|
|
|
cl, err := libp2pwebtransport.New(key, nil, network.NullResourceManager) |
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
defer cl.(io.Closer).Close() |
|
|
|
|
|
_, err = cl.Dial(context.Background(), ln.Multiaddr(), serverID) |
|
|
|
|
|
require.EqualError(t, err, "received status 403") |
|
|
|
|
|
} |
|
|