gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/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 } 75 76 func TestCmdlineModules(t *testing.T) { 77 exampleCmdlineModules := `BOOT_IMAGE=/vmlinuz-4.11.2 ro ` + 78 `my_module.flag1=8 my-module.flag2-string=hello ` + 79 `otherMod.opt1=world otherMod.opt_2=22-22` 80 81 once.Do(cmdLineOpener) 82 cmdLineReader := strings.NewReader(exampleCmdlineModules) 83 procCmdLine = parse(cmdLineReader) 84 85 if procCmdLine.Err != nil { 86 t.Errorf("procCmdLine threw an error: %v", procCmdLine.Err) 87 } 88 89 // Check flags using contains to not rely on map iteration order 90 flags := FlagsForModule("my-module") 91 if !strings.Contains(flags, "flag1=8 ") || !strings.Contains(flags, "flag2_string=hello ") { 92 t.Errorf("my-module flags got: %v, want flag1=8 flag2_string=hello ", flags) 93 } 94 flags = FlagsForModule("my_module") 95 if !strings.Contains(flags, "flag1=8 ") || !strings.Contains(flags, "flag2_string=hello ") { 96 t.Errorf("my_module flags got: %v, want flag1=8 flag2_string=hello ", flags) 97 } 98 99 flags = FlagsForModule("otherMod") 100 if !strings.Contains(flags, "opt1=world ") || !strings.Contains(flags, "opt_2=22-22 ") { 101 t.Errorf("my_module flags got: %v, want opt1=world opt_2=22-22 ", flags) 102 } 103 }