github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/strutil/cmdline_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2015 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package strutil_test 21 22 import ( 23 . "gopkg.in/check.v1" 24 25 "github.com/snapcore/snapd/strutil" 26 ) 27 28 type cmdlineTestSuite struct{} 29 30 var _ = Suite(&cmdlineTestSuite{}) 31 32 func (s *cmdlineTestSuite) TestSplitKernelCommandLine(c *C) { 33 for idx, tc := range []struct { 34 cmd string 35 exp []string 36 errStr string 37 }{ 38 {cmd: ``, exp: nil}, 39 {cmd: `foo bar baz`, exp: []string{"foo", "bar", "baz"}}, 40 {cmd: `foo=" many spaces " bar`, exp: []string{`foo=" many spaces "`, "bar"}}, 41 {cmd: `foo="1$2"`, exp: []string{`foo="1$2"`}}, 42 {cmd: `foo=1$2`, exp: []string{`foo=1$2`}}, 43 {cmd: `foo= bar`, exp: []string{"foo=", "bar"}}, 44 {cmd: `foo=""`, exp: []string{`foo=""`}}, 45 {cmd: ` cpu=1,2,3 mem=0x2000;0x4000:$2 `, exp: []string{"cpu=1,2,3", "mem=0x2000;0x4000:$2"}}, 46 {cmd: "isolcpus=1,2,10-20,100-2000:2/25", exp: []string{"isolcpus=1,2,10-20,100-2000:2/25"}}, 47 // something more realistic 48 { 49 cmd: `BOOT_IMAGE=/vmlinuz-linux root=/dev/mapper/linux-root rw quiet loglevel=3 rd.udev.log_priority=3 vt.global_cursor_default=0 rd.luks.uuid=1a273f76-3118-434b-8597-a3b12a59e017 rd.luks.uuid=775e4582-33c1-423b-ac19-f734e0d5e21c rd.luks.options=discard,timeout=0 root=/dev/mapper/linux-root apparmor=1 security=apparmor`, 50 exp: []string{ 51 "BOOT_IMAGE=/vmlinuz-linux", 52 "root=/dev/mapper/linux-root", 53 "rw", "quiet", 54 "loglevel=3", 55 "rd.udev.log_priority=3", 56 "vt.global_cursor_default=0", 57 "rd.luks.uuid=1a273f76-3118-434b-8597-a3b12a59e017", 58 "rd.luks.uuid=775e4582-33c1-423b-ac19-f734e0d5e21c", 59 "rd.luks.options=discard,timeout=0", 60 "root=/dev/mapper/linux-root", 61 "apparmor=1", 62 "security=apparmor", 63 }, 64 }, 65 // this is actually ok, eg. rd.luks.options=discard,timeout=0 66 {cmd: `a=b=`, exp: []string{"a=b="}}, 67 // bad quoting, or otherwise malformed command line 68 {cmd: `foo="1$2`, errStr: "unbalanced quoting"}, 69 {cmd: `"foo"`, errStr: "unexpected quoting"}, 70 {cmd: `foo"foo"`, errStr: "unexpected quoting"}, 71 {cmd: `foo=foo"`, errStr: "unexpected quoting"}, 72 {cmd: `foo="a""b"`, errStr: "unexpected quoting"}, 73 {cmd: `foo="a foo="b`, errStr: "unexpected argument"}, 74 {cmd: `foo="a"="b"`, errStr: "unexpected assignment"}, 75 {cmd: `=`, errStr: "unexpected assignment"}, 76 {cmd: `a =`, errStr: "unexpected assignment"}, 77 {cmd: `="foo"`, errStr: "unexpected assignment"}, 78 {cmd: `a==`, errStr: "unexpected assignment"}, 79 {cmd: `foo ==a`, errStr: "unexpected assignment"}, 80 } { 81 c.Logf("%v: cmd: %q", idx, tc.cmd) 82 out, err := strutil.KernelCommandLineSplit(tc.cmd) 83 if tc.errStr != "" { 84 c.Assert(err, ErrorMatches, tc.errStr) 85 c.Check(out, IsNil) 86 } else { 87 c.Assert(err, IsNil) 88 c.Check(out, DeepEquals, tc.exp) 89 } 90 } 91 }