Skip to content

Commit

Permalink
Return error in vnet.NewNet()
Browse files Browse the repository at this point in the history
Return error in vnet.NewNet()
  • Loading branch information
stv0g committed Nov 19, 2022
1 parent 20b7857 commit 49d07d1
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 56 deletions.
6 changes: 5 additions & 1 deletion examples/vnet-udpproxy/main.go
Expand Up @@ -27,9 +27,13 @@ func main() {
}

// Create a network and add to router, for example, for client.
clientNetwork := vnet.NewNet(&vnet.NetConfig{
clientNetwork, err := vnet.NewNet(&vnet.NetConfig{
StaticIP: "10.0.0.11",
})
if err != nil {
panic(err)
}

if err = router.AddNet(clientNetwork); err != nil {
panic(err)
}
Expand Down
10 changes: 8 additions & 2 deletions vnet/delay_filter_test.go
Expand Up @@ -12,7 +12,10 @@ func TestDelayFilter(t *testing.T) {
t.Run("schedulesOnePacketAtATime", func(t *testing.T) {
nic := newMockNIC(t)
df, err := NewDelayFilter(nic, 10*time.Millisecond)
assert.NoError(t, err)
if !assert.NoError(t, err, "should succeed") {
return
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go df.Run(ctx)
Expand Down Expand Up @@ -55,7 +58,10 @@ func TestDelayFilter(t *testing.T) {
t.Run("schedulesSubsequentManyPackets", func(t *testing.T) {
nic := newMockNIC(t)
df, err := NewDelayFilter(nic, 10*time.Millisecond)
assert.NoError(t, err)
if !assert.NoError(t, err, "should succeed") {
return
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go df.Run(ctx)
Expand Down
12 changes: 9 additions & 3 deletions vnet/loss_filter_test.go
Expand Up @@ -56,7 +56,9 @@ func TestLossFilter(t *testing.T) {
mnic := newMockNIC(t)

f, err := NewLossFilter(mnic, 100)
assert.NoError(t, err)
if !assert.NoError(t, err, "should succeed") {
return
}

f.onInboundChunk(&chunkUDP{})
})
Expand All @@ -65,7 +67,9 @@ func TestLossFilter(t *testing.T) {
mnic := newMockNIC(t)

f, err := NewLossFilter(mnic, 0)
assert.NoError(t, err)
if !assert.NoError(t, err, "should succeed") {
return
}

packets := 100
received := 0
Expand All @@ -84,7 +88,9 @@ func TestLossFilter(t *testing.T) {
mnic := newMockNIC(t)

f, err := NewLossFilter(mnic, 50)
assert.NoError(t, err)
if !assert.NoError(t, err, "should succeed") {
return
}

packets := 1000
received := 0
Expand Down
4 changes: 2 additions & 2 deletions vnet/net.go
Expand Up @@ -547,7 +547,7 @@ type NetConfig struct {
// By design, it always have lo0 and eth0 interfaces.
// The lo0 has the address 127.0.0.1 assigned by default.
// IP address for eth0 will be assigned when this Net is added to a router.
func NewNet(config *NetConfig) *Net {
func NewNet(config *NetConfig) (*Net, error) {
lo0 := transport.NewInterface(net.Interface{
Index: 1,
MTU: 16384,
Expand Down Expand Up @@ -584,7 +584,7 @@ func NewNet(config *NetConfig) *Net {
interfaces: []*transport.Interface{lo0, eth0},
staticIPs: staticIPs,
udpConns: newUDPConnMap(),
}
}, nil
}

// DialTCP acts like Dial for TCP networks.
Expand Down
108 changes: 85 additions & 23 deletions vnet/net_test.go
Expand Up @@ -15,7 +15,11 @@ func TestNetVirtual(t *testing.T) {
log := logging.NewDefaultLoggerFactory().NewLogger("test")

t.Run("tnet.Interfaces", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

intfs, err := nw.Interfaces()
assert.Equal(t, 2, len(intfs), "should be one tnet.Interface")
assert.NoError(t, err, "should succeed")
Expand Down Expand Up @@ -64,7 +68,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("tnet.InterfaceByName", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

intfs, err := nw.Interfaces()
assert.Equal(t, 2, len(intfs), "should be one tnet.Interface")
Expand Down Expand Up @@ -109,7 +116,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("hasIPAddr", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

intfs, err := nw.Interfaces()
assert.Equal(t, 2, len(intfs), "should be one tnet.Interface")
Expand All @@ -136,7 +146,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("getAllIPAddrs", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

intfs, err := nw.Interfaces()
assert.Equal(t, 2, len(intfs), "should be one tnet.Interface")
Expand All @@ -160,7 +173,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("assignPort()", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

addr := demoIP
start := 1000
Expand Down Expand Up @@ -206,7 +222,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("determineSourceIP()", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

intfs, err := nw.Interfaces()
assert.Equal(t, 2, len(intfs), "should be one tnet.Interface")
Expand Down Expand Up @@ -247,7 +266,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("ResolveUDPAddr", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

udpAddr, err := nw.ResolveUDPAddr(udp, "localhost:1234")
if !assert.NoError(t, err, "should succeed") {
Expand All @@ -258,7 +280,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("UDPLoopback", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

conn, err := nw.ListenPacket(udp, "127.0.0.1:0")
assert.NoError(t, err, "should succeed")
Expand All @@ -281,7 +306,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("ListenPacket random port", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

conn, err := nw.ListenPacket(udp, "127.0.0.1:0")
assert.NoError(t, err, "should succeed")
Expand All @@ -295,7 +323,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("ListenPacket specific port", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

conn, err := nw.ListenPacket(udp, "127.0.0.1:50916")
assert.NoError(t, err, "should succeed")
Expand All @@ -309,7 +340,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("ListenUDP random port", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

srcAddr := &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
Expand All @@ -326,7 +360,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("ListenUDP specific port", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

srcAddr := &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
Expand All @@ -344,7 +381,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("Dial (UDP) lo0", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

conn, err := nw.Dial(udp, "127.0.0.1:1234")
assert.NoError(t, err, "should succeed")
Expand All @@ -369,9 +409,11 @@ func TestNetVirtual(t *testing.T) {
LoggerFactory: loggerFactory,
})
assert.NoError(t, err, "should succeed")
assert.NotNil(t, wan, "should succeed")

nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

assert.NoError(t, wan.AddNet(nw), "should succeed")

Expand All @@ -393,7 +435,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("DialUDP", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

locAddr := &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Expand Down Expand Up @@ -433,7 +478,10 @@ func TestNetVirtual(t *testing.T) {
err = wan.AddHost("test.pion.ly", "30.31.32.33")
assert.NoError(t, err, "should succeed")

nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

assert.NoError(t, wan.AddNet(nw), "should succeed")

Expand All @@ -455,7 +503,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("Loopback", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

conn, err := nw.ListenPacket(udp, "127.0.0.1:50916")
assert.NoError(t, err, "should succeed")
Expand Down Expand Up @@ -524,16 +575,21 @@ func TestNetVirtual(t *testing.T) {
LoggerFactory: loggerFactory,
})
assert.NoError(t, err, "should succeed")
assert.NotNil(t, wan, "should succeed")

net1 := NewNet(&NetConfig{})
net1, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

err = wan.AddNet(net1)
assert.NoError(t, err, "should succeed")
ip1, err := getIPAddr(net1)
assert.NoError(t, err, "should succeed")

net2 := NewNet(&NetConfig{})
net2, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

err = wan.AddNet(net2)
assert.NoError(t, err, "should succeed")
Expand Down Expand Up @@ -618,7 +674,10 @@ func TestNetVirtual(t *testing.T) {
})

t.Run("Dialer", func(t *testing.T) {
nw := NewNet(&NetConfig{})
nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
}

dialer := nw.CreateDialer(&net.Dialer{
LocalAddr: &net.UDPAddr{
Expand Down Expand Up @@ -655,12 +714,15 @@ func TestNetVirtual(t *testing.T) {
assert.NoError(t, err, "should succeed")
assert.NotNil(t, wan, "should succeed")

net1 := NewNet(&NetConfig{
net1, err := NewNet(&NetConfig{
StaticIPs: []string{
demoIP,
"1.2.3.5",
},
})
if !assert.NoError(t, err, "should succeed") {
return
}

err = wan.AddNet(net1)
assert.NoError(t, err, "should succeed")
Expand Down

0 comments on commit 49d07d1

Please sign in to comment.