summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/stack/stack_test.go
diff options
context:
space:
mode:
authorIan Gudger <igudger@google.com>2019-01-29 16:22:34 -0800
committerShentubot <shentubot@google.com>2019-01-29 16:23:30 -0800
commitff1c3bb0b577a4ea55a64de39415a8d31142b741 (patch)
tree14bf8211ede352cdc4bae3ac8fd21dd9fc617c7c /pkg/tcpip/stack/stack_test.go
parent3c5f8dfd4b0a7157343bacb62620e4f11612c3b7 (diff)
Fix NIC endpoint forwarding.
Also adds a test for regular NIC forwarding. PiperOrigin-RevId: 231495279 Change-Id: Ic7edec249568e9ad0280cea77eac14478c9073e1
Diffstat (limited to 'pkg/tcpip/stack/stack_test.go')
-rw-r--r--pkg/tcpip/stack/stack_test.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/pkg/tcpip/stack/stack_test.go b/pkg/tcpip/stack/stack_test.go
index 391319f35..163fadded 100644
--- a/pkg/tcpip/stack/stack_test.go
+++ b/pkg/tcpip/stack/stack_test.go
@@ -1039,6 +1039,45 @@ func TestGetMainNICAddressAddRemove(t *testing.T) {
}
}
+func TestNICForwarding(t *testing.T) {
+ // Create a stack with the fake network protocol, two NICs, each with
+ // an address.
+ s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
+ s.SetForwarding(true)
+
+ id1, linkEP1 := channel.New(10, defaultMTU, "")
+ if err := s.CreateNIC(1, id1); err != nil {
+ t.Fatalf("CreateNIC #1 failed: %v", err)
+ }
+ if err := s.AddAddress(1, fakeNetNumber, "\x01"); err != nil {
+ t.Fatalf("AddAddress #1 failed: %v", err)
+ }
+
+ id2, linkEP2 := channel.New(10, defaultMTU, "")
+ if err := s.CreateNIC(2, id2); err != nil {
+ t.Fatalf("CreateNIC #2 failed: %v", err)
+ }
+ if err := s.AddAddress(2, fakeNetNumber, "\x02"); err != nil {
+ t.Fatalf("AddAddress #2 failed: %v", err)
+ }
+
+ // Route all packets to address 3 to NIC 2.
+ s.SetRouteTable([]tcpip.Route{
+ {"\x03", "\xff", "\x00", 2},
+ })
+
+ // Send a packet to address 3.
+ buf := buffer.NewView(30)
+ buf[0] = 3
+ linkEP1.Inject(fakeNetNumber, buf.ToVectorisedView())
+
+ select {
+ case <-linkEP2.C:
+ default:
+ t.Fatal("Packet not forwarded")
+ }
+}
+
func init() {
stack.RegisterNetworkProtocolFactory("fakeNet", func() stack.NetworkProtocol {
return &fakeNetworkProtocol{}