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"
|
2018-10-22 19:30:06 +02:00
|
|
|
"path/filepath"
|
2018-05-27 17:30:42 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2018-10-22 19:30:06 +02:00
|
|
|
"github.com/rtr7/router7/internal/testing/pcapreplayer"
|
2018-05-27 17:30:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDHCP4(t *testing.T) {
|
2018-10-22 19:30:06 +02:00
|
|
|
pcappath := os.Getenv("ROUTER7_PCAP_DIR")
|
|
|
|
if pcappath != "" {
|
|
|
|
pcappath = filepath.Join(pcappath, "dhcp4.pcap")
|
2018-05-27 17:30:42 +02:00
|
|
|
}
|
2018-10-22 19:30:06 +02:00
|
|
|
conn, err := pcapreplayer.NewDHCP4Conn("testdata/fiber7.pcap", pcappath)
|
2018-05-27 17:30:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-10-22 19:30:06 +02:00
|
|
|
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 },
|
2018-10-22 19:30:06 +02:00
|
|
|
connection: conn,
|
2018-06-02 10:15:50 +02:00
|
|
|
generateXID: func(b []byte) {
|
2018-05-27 17:30:42 +02:00
|
|
|
if got, want := len(b), 4; got != want {
|
2018-06-02 10:15:50 +02:00
|
|
|
t.Fatalf("github.com/d2g/dhcp4client request unexpected amount of bytes: got %d, want %d", got, want)
|
2018-05-27 17:30:42 +02:00
|
|
|
}
|
|
|
|
// TODO: read the transaction ID from the pcap file
|
|
|
|
copy(b, []byte{0x77, 0x08, 0xd7, 0x24})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|