github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/pkg/cmdline/cmdline_test.go (about)

     1  // Copyright 2018 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 cmdline
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestCmdline(t *testing.T) {
    13  
    14  	exampleCmdLine := `BOOT_IMAGE=/vmlinuz-4.11.2 ro ` +
    15  		`test-flag test2-flag=8 ` +
    16  		`uroot.initflags="systemd test-flag=3  test2-flag runlevel=2" ` +
    17  		`root=LABEL=/ biosdevname=0 net.ifnames=0 fsck.repair=yes ` +
    18  		`ipv6.autoconf=0 erst_disable nox2apic crashkernel=128M ` +
    19  		`systemd.unified_cgroup_hierarchy=1 cgroup_no_v1=all console=tty0 ` +
    20  		`console=ttyS0,115200 security=selinux selinux=1 enforcing=0`
    21  
    22  	exampleCmdLineNoInitFlags := `BOOT_IMAGE=/vmlinuz-4.11.2 ro ` +
    23  		`root=LABEL=/ biosdevname=0 net.ifnames=0 fsck.repair=yes ` +
    24  		`console=ttyS0,115200 security=selinux selinux=1 enforcing=0`
    25  
    26  	// Do this once, we'll over-write soon
    27  	once.Do(cmdLineOpener)
    28  	cmdLineReader := strings.NewReader(exampleCmdLine)
    29  	procCmdLine = parse(cmdLineReader)
    30  
    31  	if procCmdLine.Err != nil {
    32  		t.Errorf("procCmdLine threw an error: %v", procCmdLine.Err)
    33  	}
    34  
    35  	wantLen := len(exampleCmdLine)
    36  	if len(procCmdLine.Raw) != wantLen {
    37  		t.Errorf("procCmdLine.Raw wrong length: %v != %d",
    38  			len(procCmdLine.Raw), wantLen)
    39  	}
    40  
    41  	if len(FullCmdLine()) != wantLen {
    42  		t.Errorf("FullCmdLine() returned wrong length: %v != %d",
    43  			len(FullCmdLine()), wantLen)
    44  	}
    45  
    46  	if len(procCmdLine.AsMap) != 21 {
    47  		t.Errorf("procCmdLine.AsMap wrong length: %v != 21",
    48  			len(procCmdLine.AsMap))
    49  	}
    50  
    51  	if ContainsFlag("biosdevname") == false {
    52  		t.Error("couldn't find biosdevname in kernel flags")
    53  	}
    54  
    55  	if ContainsFlag("biosname") == true {
    56  		t.Error("could find biosname in kernel flags, but shouldn't")
    57  	}
    58  
    59  	if security, present := Flag("security"); !present || security != "selinux" {
    60  		t.Errorf("Flag 'security' is %v instead of 'selinux'", security)
    61  	}
    62  
    63  	initFlagMap := GetInitFlagMap()
    64  	if testflag, present := initFlagMap["test-flag"]; !present || testflag != "3" {
    65  		t.Errorf("init test-flag == %v instead of test-flag == 3\nMAP: %v", testflag, initFlagMap)
    66  	}
    67  
    68  	cmdLineReader = strings.NewReader(exampleCmdLineNoInitFlags)
    69  	procCmdLine = parse(cmdLineReader)
    70  	if initFlagMap = GetInitFlagMap(); len(initFlagMap) != 0 {
    71  		t.Errorf("initFlagMap should be empty, is actually %v", initFlagMap)
    72  	}
    73  
    74  }