github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/bootloader/androidboot_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 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  	"os"
    24  	"path/filepath"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/boot"
    29  	"github.com/snapcore/snapd/bootloader"
    30  	"github.com/snapcore/snapd/osutil"
    31  	"github.com/snapcore/snapd/snap"
    32  	"github.com/snapcore/snapd/snap/snapfile"
    33  	"github.com/snapcore/snapd/snap/snaptest"
    34  )
    35  
    36  type androidBootTestSuite struct {
    37  	baseBootenvTestSuite
    38  }
    39  
    40  var _ = Suite(&androidBootTestSuite{})
    41  
    42  func (s *androidBootTestSuite) SetUpTest(c *C) {
    43  	s.baseBootenvTestSuite.SetUpTest(c)
    44  
    45  	// the file needs to exist for androidboot object to be created
    46  	bootloader.MockAndroidBootFile(c, s.rootdir, 0644)
    47  }
    48  
    49  func (s *androidBootTestSuite) TestNewAndroidboot(c *C) {
    50  	// no files means bl is not present, but we can still create the bl object
    51  	c.Assert(os.RemoveAll(s.rootdir), IsNil)
    52  	a := bootloader.NewAndroidBoot(s.rootdir)
    53  	c.Assert(a, NotNil)
    54  	c.Assert(a.Name(), Equals, "androidboot")
    55  
    56  	present, err := a.Present()
    57  	c.Assert(err, IsNil)
    58  	c.Assert(present, Equals, false)
    59  
    60  	// now with files present, the bl is present
    61  	bootloader.MockAndroidBootFile(c, s.rootdir, 0644)
    62  	present, err = a.Present()
    63  	c.Assert(err, IsNil)
    64  	c.Assert(present, Equals, true)
    65  }
    66  
    67  func (s *androidBootTestSuite) TestSetGetBootVar(c *C) {
    68  	a := bootloader.NewAndroidBoot(s.rootdir)
    69  	bootVars := map[string]string{"snap_mode": boot.TryStatus}
    70  	a.SetBootVars(bootVars)
    71  
    72  	v, err := a.GetBootVars("snap_mode")
    73  	c.Assert(err, IsNil)
    74  	c.Check(v, HasLen, 1)
    75  	c.Check(v["snap_mode"], Equals, boot.TryStatus)
    76  }
    77  
    78  func (s *androidBootTestSuite) TestExtractKernelAssetsNoUnpacksKernel(c *C) {
    79  	a := bootloader.NewAndroidBoot(s.rootdir)
    80  
    81  	c.Assert(a, NotNil)
    82  
    83  	files := [][]string{
    84  		{"kernel.img", "I'm a kernel"},
    85  		{"initrd.img", "...and I'm an initrd"},
    86  		{"meta/kernel.yaml", "version: 4.2"},
    87  	}
    88  	si := &snap.SideInfo{
    89  		RealName: "ubuntu-kernel",
    90  		Revision: snap.R(42),
    91  	}
    92  	fn := snaptest.MakeTestSnapWithFiles(c, packageKernel, files)
    93  	snapf, err := snapfile.Open(fn)
    94  	c.Assert(err, IsNil)
    95  
    96  	info, err := snap.ReadInfoFromSnapFile(snapf, si)
    97  	c.Assert(err, IsNil)
    98  
    99  	err = a.ExtractKernelAssets(info, snapf)
   100  	c.Assert(err, IsNil)
   101  
   102  	// kernel is *not* here
   103  	kernimg := filepath.Join(s.rootdir, "boot", "androidboot", "ubuntu-kernel_42.snap", "kernel.img")
   104  	c.Assert(osutil.FileExists(kernimg), Equals, false)
   105  }