github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/bootloader/assets/genasset/main_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2020 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 main_test 21 22 import ( 23 "bytes" 24 "fmt" 25 "io/ioutil" 26 "os" 27 "os/exec" 28 "path/filepath" 29 "testing" 30 "time" 31 32 . "gopkg.in/check.v1" 33 34 generate "github.com/snapcore/snapd/bootloader/assets/genasset" 35 "github.com/snapcore/snapd/testutil" 36 ) 37 38 // Hook up check.v1 into the "go test" runner 39 func Test(t *testing.T) { TestingT(t) } 40 41 type generateAssetsTestSuite struct { 42 testutil.BaseTest 43 } 44 45 var _ = Suite(&generateAssetsTestSuite{}) 46 47 func (s *generateAssetsTestSuite) SetUpTest(c *C) { 48 s.BaseTest.SetUpTest(c) 49 } 50 51 func mockArgs(args []string) (restore func()) { 52 old := os.Args 53 os.Args = args 54 return func() { 55 os.Args = old 56 } 57 } 58 59 func (s *generateAssetsTestSuite) TestArgs(c *C) { 60 generate.ResetArgs() 61 restore := mockArgs([]string{"self", "-in", "ok", "-out", "ok", "-name", "assetname"}) 62 defer restore() 63 c.Assert(generate.ParseArgs(), IsNil) 64 // no input file 65 generate.ResetArgs() 66 restore = mockArgs([]string{"self", "-out", "ok", "-name", "assetname"}) 67 defer restore() 68 c.Assert(generate.ParseArgs(), ErrorMatches, "input file not provided") 69 // no output file 70 restore = mockArgs([]string{"self", "-in", "in", "-name", "assetname"}) 71 defer restore() 72 generate.ResetArgs() 73 c.Assert(generate.ParseArgs(), ErrorMatches, "output file not provided") 74 // no name 75 generate.ResetArgs() 76 restore = mockArgs([]string{"self", "-in", "in", "-out", "out"}) 77 defer restore() 78 c.Assert(generate.ParseArgs(), ErrorMatches, "asset name not provided") 79 } 80 81 func (s *generateAssetsTestSuite) TestSimpleAsset(c *C) { 82 d := c.MkDir() 83 err := ioutil.WriteFile(filepath.Join(d, "in"), []byte("this is a\n"+ 84 "multiline asset \"'``\nwith chars\n"), 0644) 85 c.Assert(err, IsNil) 86 err = generate.Run("asset-name", filepath.Join(d, "in"), filepath.Join(d, "out")) 87 c.Assert(err, IsNil) 88 data, err := ioutil.ReadFile(filepath.Join(d, "out")) 89 c.Assert(err, IsNil) 90 91 const exp = `// -*- Mode: Go; indent-tabs-mode: t -*- 92 93 /* 94 * Copyright (C) %d Canonical Ltd 95 * 96 * This program is free software: you can redistribute it and/or modify 97 * it under the terms of the GNU General Public License version 3 as 98 * published by the Free Software Foundation. 99 * 100 * This program is distributed in the hope that it will be useful, 101 * but WITHOUT ANY WARRANTY; without even the implied warranty of 102 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 103 * GNU General Public License for more details. 104 * 105 * You should have received a copy of the GNU General Public License 106 * along with this program. If not, see <http://www.gnu.org/licenses/>. 107 * 108 */ 109 110 package assets 111 112 // Code generated from %s DO NOT EDIT 113 114 func init() { 115 registerInternal("asset-name", []byte{ 116 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x0a, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x6c, 117 0x69, 0x6e, 0x65, 0x20, 0x61, 0x73, 0x73, 0x65, 0x74, 0x20, 0x22, 0x27, 0x60, 0x60, 0x0a, 0x77, 118 0x69, 0x74, 0x68, 0x20, 0x63, 0x68, 0x61, 0x72, 0x73, 0x0a, 119 }) 120 } 121 ` 122 c.Check(string(data), Equals, fmt.Sprintf(exp, time.Now().Year(), filepath.Join(d, "in"))) 123 } 124 125 func (s *generateAssetsTestSuite) TestGoFmtClean(c *C) { 126 _, err := exec.LookPath("gofmt") 127 if err != nil { 128 c.Skip("gofmt is missing") 129 } 130 131 d := c.MkDir() 132 err = ioutil.WriteFile(filepath.Join(d, "in"), []byte("this is a\n"+ 133 "multiline asset \"'``\nuneven chars\n"), 0644) 134 c.Assert(err, IsNil) 135 err = generate.Run("asset-name", filepath.Join(d, "in"), filepath.Join(d, "out")) 136 c.Assert(err, IsNil) 137 138 cmd := exec.Command("gofmt", "-l", "-d", filepath.Join(d, "out")) 139 out, err := cmd.CombinedOutput() 140 c.Assert(err, IsNil) 141 c.Assert(out, HasLen, 0, Commentf("output file is not gofmt clean: %s", string(out))) 142 } 143 144 func (s *generateAssetsTestSuite) TestRunErrors(c *C) { 145 d := c.MkDir() 146 err := generate.Run("asset-name", filepath.Join(d, "missing"), filepath.Join(d, "out")) 147 c.Assert(err, ErrorMatches, "cannot open input file: open .*/missing: no such file or directory") 148 149 err = ioutil.WriteFile(filepath.Join(d, "in"), []byte("this is a\n"+ 150 "multiline asset \"'``\nuneven chars\n"), 0644) 151 c.Assert(err, IsNil) 152 153 err = generate.Run("asset-name", filepath.Join(d, "in"), filepath.Join(d, "does-not-exist", "out")) 154 c.Assert(err, ErrorMatches, `cannot open output file: open .*/does-not-exist/out\..*: no such file or directory`) 155 156 } 157 158 func (s *generateAssetsTestSuite) TestFormatLines(c *C) { 159 out := generate.FormatLines(bytes.Repeat([]byte{1}, 12)) 160 c.Check(out, DeepEquals, []string{ 161 "0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,", 162 }) 163 out = generate.FormatLines(bytes.Repeat([]byte{1}, 16)) 164 c.Check(out, DeepEquals, []string{ 165 "0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,", 166 }) 167 out = generate.FormatLines(bytes.Repeat([]byte{1}, 17)) 168 c.Check(out, DeepEquals, []string{ 169 "0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,", 170 "0x01,", 171 }) 172 out = generate.FormatLines(bytes.Repeat([]byte{1}, 1)) 173 c.Check(out, DeepEquals, []string{ 174 "0x01,", 175 }) 176 }