github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+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  }
   106  
   107  func TestISCSIBoot(t *testing.T) {
   108  	for i, tt := range []struct {
   109  		message    *dhcpv4.DHCPv4
   110  		want       *net.TCPAddr
   111  		wantVolume string
   112  		err        error
   113  	}{
   114  		{
   115  			message: mustNew(t),
   116  			err:     ErrNoRootPath,
   117  		},
   118  		{
   119  			message: mustNew(t,
   120  				withNetbootInfo("pxelinux.0", "10.0.0.1"),
   121  			),
   122  			err: ErrNoRootPath,
   123  		},
   124  		{
   125  			message: mustNew(t,
   126  				dhcpv4.WithOption(dhcpv4.OptRootPath("iscsi:10.0.0.1::3260::iqn.2016-01.com.example:foo")),
   127  			),
   128  			want: &net.TCPAddr{
   129  				IP:   net.ParseIP("10.0.0.1"),
   130  				Port: 3260,
   131  			},
   132  			wantVolume: "iqn.2016-01.com.example:foo",
   133  		},
   134  		{
   135  			message: mustNew(t,
   136  				withNetbootInfo("iscsi:10.0.0.1::3260::iqn.2016-01.com.example:foo", ""),
   137  			),
   138  			want: &net.TCPAddr{
   139  				IP:   net.ParseIP("10.0.0.1"),
   140  				Port: 3260,
   141  			},
   142  			wantVolume: "iqn.2016-01.com.example:foo",
   143  		},
   144  		{
   145  			message: mustNew(t,
   146  				withNetbootInfo("iscsi:90.90.90.90::3261::iqn.2018-02.com.example:foo", ""),
   147  				dhcpv4.WithOption(dhcpv4.OptRootPath("iscsi:10.0.0.1::3260::iqn.2016-01.com.example:foo")),
   148  			),
   149  			want: &net.TCPAddr{
   150  				IP:   net.ParseIP("10.0.0.1"),
   151  				Port: 3260,
   152  			},
   153  			wantVolume: "iqn.2016-01.com.example:foo",
   154  		},
   155  	} {
   156  		t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) {
   157  			p := NewPacket4(nil, tt.message)
   158  			got, gotVolume, err := p.ISCSIBoot()
   159  			if err != tt.err {
   160  				t.Errorf("ISCSIBoot() = %v, want %v", err, tt.err)
   161  			}
   162  			if !reflect.DeepEqual(got, tt.want) || gotVolume != tt.wantVolume {
   163  				t.Errorf("ISCSIBoot() = (%s, %s), want (%s, %s)", got, gotVolume, tt.want, tt.wantVolume)
   164  			}
   165  		})
   166  	}
   167  }