github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/bootloader/asset_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 bootloader_test 21 22 import ( 23 "bytes" 24 "io/ioutil" 25 "os" 26 "path/filepath" 27 28 . "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/bootloader" 31 "github.com/snapcore/snapd/bootloader/assets" 32 ) 33 34 type configAssetTestSuite struct { 35 baseBootenvTestSuite 36 } 37 38 var _ = Suite(&configAssetTestSuite{}) 39 40 func (s *configAssetTestSuite) TestTrivialFromConfigAssert(c *C) { 41 e, err := bootloader.EditionFromConfigAsset(bytes.NewBufferString(`# Snapd-Boot-Config-Edition: 321 42 next line 43 one after that`)) 44 c.Assert(err, IsNil) 45 c.Assert(e, Equals, uint(321)) 46 47 e, err = bootloader.EditionFromConfigAsset(bytes.NewBufferString(`# Snapd-Boot-Config-Edition: 932 48 # Snapd-Boot-Config-Edition: 321 49 one after that`)) 50 c.Assert(err, IsNil) 51 c.Assert(e, Equals, uint(932)) 52 53 e, err = bootloader.EditionFromConfigAsset(bytes.NewBufferString(`# Snapd-Boot-Config-Edition: 1234 54 one after that 55 # Snapd-Boot-Config-Edition: 321 56 `)) 57 c.Assert(err, IsNil) 58 c.Assert(e, Equals, uint(1234)) 59 } 60 61 func (s *configAssetTestSuite) TestTrivialFromFile(c *C) { 62 d := c.MkDir() 63 p := filepath.Join(d, "foo") 64 ioutil.WriteFile(p, []byte(`# Snapd-Boot-Config-Edition: 123 65 this is some 66 this too`), 0644) 67 e, err := bootloader.EditionFromDiskConfigAsset(p) 68 c.Assert(err, IsNil) 69 c.Assert(e, Equals, uint(123)) 70 } 71 72 func (s *configAssetTestSuite) TestRealConfig(c *C) { 73 grubConfig := assets.Internal("grub.cfg") 74 c.Assert(grubConfig, NotNil) 75 e, err := bootloader.EditionFromConfigAsset(bytes.NewReader(grubConfig)) 76 c.Assert(err, IsNil) 77 c.Assert(e, Equals, uint(1)) 78 } 79 80 func (s *configAssetTestSuite) TestNoConfig(c *C) { 81 _, err := bootloader.EditionFromConfigAsset(bytes.NewReader(nil)) 82 c.Assert(err, ErrorMatches, "cannot read config asset: unexpected EOF") 83 } 84 85 func (s *configAssetTestSuite) TestNoFile(c *C) { 86 d := c.MkDir() 87 p := filepath.Join(d, "foo") 88 // file does not exist 89 _, err := bootloader.EditionFromDiskConfigAsset(p) 90 c.Assert(err, ErrorMatches, "no edition") 91 } 92 93 func (s *configAssetTestSuite) TestUnreadableFile(c *C) { 94 // root has DAC override 95 if os.Geteuid() == 0 { 96 c.Skip("test case cannot be correctly executed by root") 97 } 98 d := c.MkDir() 99 p := filepath.Join(d, "foo") 100 err := ioutil.WriteFile(p, []byte("foo"), 0000) 101 c.Assert(err, IsNil) 102 _, err = bootloader.EditionFromDiskConfigAsset(p) 103 c.Assert(err, ErrorMatches, "cannot load existing config asset: .*/foo: permission denied") 104 } 105 106 func (s *configAssetTestSuite) TestNoEdition(c *C) { 107 _, err := bootloader.EditionFromConfigAsset(bytes.NewReader([]byte(`this is some script 108 without edition header 109 `))) 110 c.Assert(err, ErrorMatches, "no edition") 111 } 112 113 func (s *configAssetTestSuite) TestBadEdition(c *C) { 114 _, err := bootloader.EditionFromConfigAsset(bytes.NewReader([]byte(`# Snapd-Boot-Config-Edition: random 115 data follows 116 `))) 117 c.Assert(err, ErrorMatches, `cannot parse asset edition: .* parsing "random": invalid syntax`) 118 } 119 120 func (s *configAssetTestSuite) TestConfigAssetFrom(c *C) { 121 script := []byte(`# Snapd-Boot-Config-Edition: 123 122 data follows 123 `) 124 bs, err := bootloader.ConfigAssetFrom(script) 125 c.Assert(err, IsNil) 126 c.Assert(bs, NotNil) 127 c.Assert(bs.Edition(), Equals, uint(123)) 128 c.Assert(bs.Raw(), DeepEquals, script) 129 }