1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
// Copyright 2018-present the CoreDHCP Authors. All rights reserved
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
package rangeplugin
import (
"io/ioutil"
"net"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var leasefile string = `02:00:00:00:00:00 10.0.0.0 2000-01-01T00:00:00Z
02:00:00:00:00:01 10.0.0.1 2000-01-01T00:00:00Z
02:00:00:00:00:02 10.0.0.2 2000-01-01T00:00:00Z
02:00:00:00:00:03 10.0.0.3 2000-01-01T00:00:00Z
02:00:00:00:00:04 10.0.0.4 2000-01-01T00:00:00Z
02:00:00:00:00:05 10.0.0.5 2000-01-01T00:00:00Z
`
var expire = time.Date(2000, 01, 01, 00, 00, 00, 00, time.UTC)
var records = []struct {
mac string
ip *Record
}{
{"02:00:00:00:00:00", &Record{net.IPv4(10, 0, 0, 0), expire}},
{"02:00:00:00:00:01", &Record{net.IPv4(10, 0, 0, 1), expire}},
{"02:00:00:00:00:02", &Record{net.IPv4(10, 0, 0, 2), expire}},
{"02:00:00:00:00:03", &Record{net.IPv4(10, 0, 0, 3), expire}},
{"02:00:00:00:00:04", &Record{net.IPv4(10, 0, 0, 4), expire}},
{"02:00:00:00:00:05", &Record{net.IPv4(10, 0, 0, 5), expire}},
}
func TestLoadRecords(t *testing.T) {
parsedRec, err := loadRecords(strings.NewReader(leasefile))
if err != nil {
t.Fatalf("Failed to load records from file: %v", err)
}
mapRec := make(map[string]*Record)
for _, rec := range records {
mapRec[rec.mac] = rec.ip
}
assert.Equal(t, mapRec, parsedRec, "Loaded records differ from what's in the file")
}
func TestWriteRecords(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "coredhcptest")
if err != nil {
t.Skipf("Could not setup file-based test: %v", err)
}
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()
pl := PluginState{}
if err := pl.registerBackingFile(tmpfile.Name()); err != nil {
t.Fatalf("Could not setup file")
}
defer pl.leasefile.Close()
for _, rec := range records {
hwaddr, err := net.ParseMAC(rec.mac)
if err != nil {
// bug in testdata
panic(err)
}
if err := pl.saveIPAddress(hwaddr, rec.ip); err != nil {
t.Errorf("Failed to save ip for %s: %v", hwaddr, err)
}
}
if _, err := tmpfile.Seek(0, 0); err != nil {
t.Fatal(err)
}
written, err := ioutil.ReadAll(tmpfile)
if err != nil {
t.Fatalf("Could not read back temp file")
}
assert.Equal(t, leasefile, string(written), "Data written to the file doesn't match records")
}
|