File: option_elapsedtime_test.go

package info (click to toggle)
golang-github-insomniacslk-dhcp 0.0~git20220915.043f172-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,168 kB
  • sloc: makefile: 6
file content (43 lines) | stat: -rw-r--r-- 1,206 bytes parent folder | download | duplicates (2)
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
package dhcpv6

import (
	"bytes"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestOptElapsedTime(t *testing.T) {
	opt, err := parseOptElapsedTime([]byte{0xaa, 0xbb})
	if err != nil {
		t.Fatal(err)
	}
	if elapsedTime := opt.ElapsedTime; elapsedTime != 0xaabb*10*time.Millisecond {
		t.Fatalf("Invalid elapsed time. Expected 0xaabb, got %v", elapsedTime)
	}
}

func TestOptElapsedTimeToBytes(t *testing.T) {
	opt := OptElapsedTime(0)
	expected := []byte{0, 0}
	if toBytes := opt.ToBytes(); !bytes.Equal(expected, toBytes) {
		t.Fatalf("Invalid ToBytes output. Expected %v, got %v", expected, toBytes)
	}
}

func TestOptElapsedTimeString(t *testing.T) {
	opt := OptElapsedTime(100 * time.Millisecond)
	expected := "ElapsedTime: 100ms"
	if optString := opt.String(); optString != expected {
		t.Fatalf("Invalid elapsed time string. Expected %v, got %v", expected, optString)
	}
}

func TestOptElapsedTimeParseInvalidOption(t *testing.T) {
	_, err := parseOptElapsedTime([]byte{0xaa})
	require.Error(t, err, "A short option should return an error")

	_, err = parseOptElapsedTime([]byte{0xaa, 0xbb, 0xcc})
	require.Error(t, err, "An option with too many bytes should return an error")
}