router7/internal/dhcp4/dhcp4_test.go

74 lines
1.8 KiB
Go
Raw Normal View History

2018-06-28 13:39:48 +02:00
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2018-05-27 17:30:42 +02:00
package dhcp4
import (
"net"
"os"
"path/filepath"
2018-05-27 17:30:42 +02:00
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/rtr7/router7/internal/testing/pcapreplayer"
2018-05-27 17:30:42 +02:00
)
func TestDHCP4(t *testing.T) {
pcappath := os.Getenv("ROUTER7_PCAP_DIR")
if pcappath != "" {
pcappath = filepath.Join(pcappath, "dhcp4.pcap")
2018-05-27 17:30:42 +02:00
}
conn, err := pcapreplayer.NewDHCP4Conn("testdata/fiber7.pcap", pcappath)
2018-05-27 17:30:42 +02:00
if err != nil {
t.Fatal(err)
}
defer conn.Close()
2018-05-27 17:30:42 +02:00
mac, err := net.ParseMAC("d8:58:d7:00:4e:df")
if err != nil {
t.Fatal(err)
}
now := time.Now()
c := Client{
hardwareAddr: mac,
timeNow: func() time.Time { return now },
connection: conn,
generateXID: func() uint32 {
2018-05-27 17:30:42 +02:00
// TODO: read the transaction ID from the pcap file
return 0x7708d724
2018-05-27 17:30:42 +02:00
},
}
c.ObtainOrRenew()
if err := c.Err(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
got := c.Config()
want := Config{
RenewAfter: now.Add(13*time.Minute + 24*time.Second),
ClientIP: "85.195.207.62",
SubnetMask: "255.255.255.128",
Router: "85.195.207.1",
DNS: []string{
"77.109.128.2",
"213.144.129.20",
},
}
if diff := cmp.Diff(got, want); diff != "" {
t.Fatalf("unexpected config: diff (-got +want):\n%s", diff)
}
}