summaryrefslogtreecommitdiffhomepage
path: root/pkg/dhcp/dhcp_test.go
diff options
context:
space:
mode:
authorGoogler <noreply@google.com>2018-04-27 10:37:02 -0700
committerAdin Scannell <ascannell@google.com>2018-04-28 01:44:26 -0400
commitd02b74a5dcfed4bfc8f2f8e545bca4d2afabb296 (patch)
tree54f95eef73aee6bacbfc736fffc631be2605ed53 /pkg/dhcp/dhcp_test.go
parentf70210e742919f40aa2f0934a22f1c9ba6dada62 (diff)
Check in gVisor.
PiperOrigin-RevId: 194583126 Change-Id: Ica1d8821a90f74e7e745962d71801c598c652463
Diffstat (limited to 'pkg/dhcp/dhcp_test.go')
-rw-r--r--pkg/dhcp/dhcp_test.go113
1 files changed, 113 insertions, 0 deletions
diff --git a/pkg/dhcp/dhcp_test.go b/pkg/dhcp/dhcp_test.go
new file mode 100644
index 000000000..d56b93997
--- /dev/null
+++ b/pkg/dhcp/dhcp_test.go
@@ -0,0 +1,113 @@
+// Copyright 2016 The Netstack Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package dhcp
+
+import (
+ "context"
+ "strings"
+ "testing"
+ "time"
+
+ "gvisor.googlesource.com/gvisor/pkg/tcpip"
+ "gvisor.googlesource.com/gvisor/pkg/tcpip/buffer"
+ "gvisor.googlesource.com/gvisor/pkg/tcpip/link/channel"
+ "gvisor.googlesource.com/gvisor/pkg/tcpip/link/sniffer"
+ "gvisor.googlesource.com/gvisor/pkg/tcpip/network/ipv4"
+ "gvisor.googlesource.com/gvisor/pkg/tcpip/stack"
+ "gvisor.googlesource.com/gvisor/pkg/tcpip/transport/udp"
+)
+
+func TestDHCP(t *testing.T) {
+ const defaultMTU = 65536
+ id, linkEP := channel.New(256, defaultMTU, "")
+ if testing.Verbose() {
+ id = sniffer.New(id)
+ }
+
+ go func() {
+ for pkt := range linkEP.C {
+ v := make(buffer.View, len(pkt.Header)+len(pkt.Payload))
+ copy(v, pkt.Header)
+ copy(v[len(pkt.Header):], pkt.Payload)
+ vv := v.ToVectorisedView([1]buffer.View{})
+ linkEP.Inject(pkt.Proto, &vv)
+ }
+ }()
+
+ s := stack.New([]string{ipv4.ProtocolName}, []string{udp.ProtocolName})
+
+ const nicid tcpip.NICID = 1
+ if err := s.CreateNIC(nicid, id); err != nil {
+ t.Fatal(err)
+ }
+ if err := s.AddAddress(nicid, ipv4.ProtocolNumber, "\x00\x00\x00\x00"); err != nil {
+ t.Fatal(err)
+ }
+ if err := s.AddAddress(nicid, ipv4.ProtocolNumber, "\xff\xff\xff\xff"); err != nil {
+ t.Fatal(err)
+ }
+ const serverAddr = tcpip.Address("\xc0\xa8\x03\x01")
+ if err := s.AddAddress(nicid, ipv4.ProtocolNumber, serverAddr); err != nil {
+ t.Fatal(err)
+ }
+
+ s.SetRouteTable([]tcpip.Route{{
+ Destination: tcpip.Address(strings.Repeat("\x00", 4)),
+ Mask: tcpip.Address(strings.Repeat("\x00", 4)),
+ Gateway: "",
+ NIC: nicid,
+ }})
+
+ var clientAddrs = []tcpip.Address{"\xc0\xa8\x03\x02", "\xc0\xa8\x03\x03"}
+
+ serverCfg := Config{
+ ServerAddress: serverAddr,
+ SubnetMask: "\xff\xff\xff\x00",
+ Gateway: "\xc0\xa8\x03\xF0",
+ DomainNameServer: "\x08\x08\x08\x08",
+ LeaseLength: 24 * time.Hour,
+ }
+ serverCtx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ _, err := NewServer(serverCtx, s, clientAddrs, serverCfg)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ const clientLinkAddr0 = tcpip.LinkAddress("\x52\x11\x22\x33\x44\x52")
+ c0 := NewClient(s, nicid, clientLinkAddr0)
+ if err := c0.Request(context.Background(), ""); err != nil {
+ t.Fatal(err)
+ }
+ if got, want := c0.Address(), clientAddrs[0]; got != want {
+ t.Errorf("c.Addr()=%s, want=%s", got, want)
+ }
+ if err := c0.Request(context.Background(), ""); err != nil {
+ t.Fatal(err)
+ }
+ if got, want := c0.Address(), clientAddrs[0]; got != want {
+ t.Errorf("c.Addr()=%s, want=%s", got, want)
+ }
+
+ const clientLinkAddr1 = tcpip.LinkAddress("\x52\x11\x22\x33\x44\x53")
+ c1 := NewClient(s, nicid, clientLinkAddr1)
+ if err := c1.Request(context.Background(), ""); err != nil {
+ t.Fatal(err)
+ }
+ if got, want := c1.Address(), clientAddrs[1]; got != want {
+ t.Errorf("c.Addr()=%s, want=%s", got, want)
+ }
+
+ if err := c0.Request(context.Background(), ""); err != nil {
+ t.Fatal(err)
+ }
+ if got, want := c0.Address(), clientAddrs[0]; got != want {
+ t.Errorf("c.Addr()=%s, want=%s", got, want)
+ }
+
+ if got, want := c0.Config(), serverCfg; got != want {
+ t.Errorf("client config:\n\t%#+v\nwant:\n\t%#+v", got, want)
+ }
+}