github.com/jlowellwofford/u-root@v1.0.0/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 uroot.uinitflags="a=3 skipfork test2-flag" ` + 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 if len(procCmdLine.Raw) != 393 { 36 t.Errorf("procCmdLine.Raw wrong length: %v != 417", 37 len(procCmdLine.Raw)) 38 } 39 40 if len(FullCmdLine()) != 393 { 41 t.Errorf("FullCmdLine() returned wrong length: %v != 417", 42 len(FullCmdLine())) 43 } 44 45 if len(procCmdLine.AsMap) != 22 { 46 t.Errorf("procCmdLine.Raw wrong length: %v != 22", 47 len(procCmdLine.AsMap)) 48 } 49 50 if ContainsFlag("biosdevname") == false { 51 t.Error("couldn't find biosdevname in kernel flags") 52 } 53 54 if ContainsFlag("biosname") == true { 55 t.Error("could find biosname in kernel flags, but shouldn't") 56 } 57 58 if security, present := Flag("security"); !present || security != "selinux" { 59 t.Errorf("Flag 'security' is %v instead of 'selinux'", security) 60 } 61 62 uinitFlagMap := GetUinitFlagMap() 63 64 if _, present := uinitFlagMap["skipfork"]; !present { 65 t.Errorf("Can't find 'skipfork' flag in uinit flags: present == %v", 66 present) 67 } 68 if _, present := uinitFlagMap["madeup"]; present { 69 t.Error("Should not find a 'madeup' flag in uinit flags") 70 } 71 72 initFlagMap := GetInitFlagMap() 73 if testflag, present := initFlagMap["test-flag"]; !present || testflag != "3" { 74 t.Errorf("init test-flag == %v instead of test-flag == 3\nMAP: %v", testflag, initFlagMap) 75 } 76 77 cmdLineReader = strings.NewReader(exampleCmdLineNoInitFlags) 78 procCmdLine = parse(cmdLineReader) 79 if initFlagMap = GetInitFlagMap(); len(initFlagMap) != 0 { 80 t.Errorf("initFlagMap should be empty, is actually %v", initFlagMap) 81 } 82 83 }