|
| 1 | +package gating |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + ds "github.com/ipfs/go-datastore" |
| 7 | + dsSync "github.com/ipfs/go-datastore/sync" |
| 8 | + ma "github.com/multiformats/go-multiaddr" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func createTestDataStore() ds.Batching { |
| 14 | + return dsSync.MutexWrap(ds.NewMapDatastore()) |
| 15 | +} |
| 16 | +func TestGaterBlocking(t *testing.T) { |
| 17 | + store := createTestDataStore() |
| 18 | + gater, err := NewExtendedConnectionGater(store) |
| 19 | + assert.Nil(t, err, "%s", err) |
| 20 | + require.NotNil(t, &gater, "%s", &gater) |
| 21 | + |
| 22 | + gater = AddBlocking(gater, true) |
| 23 | + |
| 24 | + public, err := ma.NewMultiaddr("/ip4/1.1.1.1/udp/53") |
| 25 | + assert.Nil(t, err, "%s", err) |
| 26 | + allowed := gater.InterceptAddrDial("somePeer", public) |
| 27 | + assert.True(t, allowed, "%b", allowed) |
| 28 | + |
| 29 | + private, err := ma.NewMultiaddr("/ip4/192.168.1.1/tcp/80") |
| 30 | + assert.Nil(t, err, "%s", err) |
| 31 | + allowed = gater.InterceptAddrDial("somePeer", private) |
| 32 | + assert.False(t, allowed, "%b", allowed) |
| 33 | +} |
| 34 | + |
| 35 | +func TestGaterNotBlocking(t *testing.T) { |
| 36 | + store := createTestDataStore() |
| 37 | + gater, err := NewExtendedConnectionGater(store) |
| 38 | + assert.Nil(t, err, "%s", err) |
| 39 | + require.NotNil(t, &gater, "%s", &gater) |
| 40 | + |
| 41 | + public, err := ma.NewMultiaddr("/ip4/1.1.1.1/udp/53") |
| 42 | + assert.Nil(t, err, "%s", err) |
| 43 | + allowed := gater.InterceptAddrDial("somePeer", public) |
| 44 | + assert.True(t, allowed, "%b", allowed) |
| 45 | + |
| 46 | + private, err := ma.NewMultiaddr("/ip4/192.168.1.1/tcp/80") |
| 47 | + assert.Nil(t, err, "%s", err) |
| 48 | + allowed = gater.InterceptAddrDial("somePeer", private) |
| 49 | + assert.True(t, allowed, "%b", allowed) |
| 50 | +} |
0 commit comments