github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/pkg/dhclient/dhcp4_test.go (about)

     1  // Copyright 2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package dhclient
     6  
     7  import (
     8  	"fmt"
     9  	"net"
    10  	"net/url"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/insomniacslk/dhcp/dhcpv4"
    15  )
    16  
    17  func withNetbootInfo(bootFileName, serverHostName string) dhcpv4.Modifier {
    18  	return func(m *dhcpv4.DHCPv4) {
    19  		m.BootFileName = bootFileName
    20  		m.ServerHostName = serverHostName
    21  	}
    22  }
    23  
    24  func mustNew(t *testing.T, modifiers ...dhcpv4.Modifier) *dhcpv4.DHCPv4 {
    25  	m, err := dhcpv4.New(modifiers...)
    26  	if err != nil {
    27  		t.Fatalf("New() = %v", err)
    28  	}
    29  	return m
    30  }
    31  
    32  func TestBoot(t *testing.T) {
    33  	for i, tt := range []struct {
    34  		message *dhcpv4.DHCPv4
    35  		want    *url.URL
    36  		err     error
    37  	}{
    38  		{
    39  			message: mustNew(t),
    40  			err:     ErrNoBootFile,
    41  		},
    42  		{
    43  			message: mustNew(t,
    44  				withNetbootInfo("pxelinux.0", "10.0.0.1"),
    45  			),
    46  			want: &url.URL{
    47  				Scheme: "tftp",
    48  				Host:   "10.0.0.1",
    49  				Path:   "pxelinux.0",
    50  			},
    51  		},
    52  		{
    53  			message: mustNew(t,
    54  				withNetbootInfo("pxelinux.0", ""),
    55  			),
    56  			err: ErrNoServerHostName,
    57  		},
    58  		{
    59  			message: mustNew(t,
    60  				withNetbootInfo("pxelinux.0", ""),
    61  				dhcpv4.WithServerIP(net.IP{10, 0, 0, 2}),
    62  			),
    63  			want: &url.URL{
    64  				Scheme: "tftp",
    65  				Host:   "10.0.0.2",
    66  				Path:   "pxelinux.0",
    67  			},
    68  		},
    69  		{
    70  			message: mustNew(t,
    71  				withNetbootInfo("pxelinux.0", ""),
    72  				dhcpv4.WithServerIP(net.IP{10, 0, 0, 2}),
    73  				dhcpv4.WithOption(dhcpv4.OptServerIdentifier(net.IP{10, 0, 0, 3})),
    74  			),
    75  			want: &url.URL{
    76  				Scheme: "tftp",
    77  				Host:   "10.0.0.3",
    78  				Path:   "pxelinux.0",
    79  			},
    80  		},
    81  		{
    82  			message: mustNew(t,
    83  				withNetbootInfo("pxelinux.0", "10.0.0.1"),
    84  				dhcpv4.WithServerIP(net.IP{10, 0, 0, 2}),
    85  				dhcpv4.WithOption(dhcpv4.OptServerIdentifier(net.IP{10, 0, 0, 3})),
    86  			),
    87  			want: &url.URL{
    88  				Scheme: "tftp",
    89  				Host:   "10.0.0.1",
    90  				Path:   "pxelinux.0",
    91  			},
    92  		},
    93  	} {
    94  		t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) {
    95  			p := NewPacket4(nil, tt.message)
    96  			got, err := p.Boot()
    97  			if err != tt.err {
    98  				t.Errorf("Boot() = %v, want %v", err, tt.err)
    99  			}
   100  			if !reflect.DeepEqual(got, tt.want) {
   101  				t.Errorf("Boot() = %s, want %s", got, tt.want)
   102  			}
   103  		})
   104  	}
   105  }