github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/snap/snapfile/snapfile_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 snapfile_test
    21  
    22  import (
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  	"testing"
    27  
    28  	. "gopkg.in/check.v1"
    29  
    30  	"github.com/snapcore/snapd/dirs"
    31  	"github.com/snapcore/snapd/osutil"
    32  	"github.com/snapcore/snapd/snap"
    33  	"github.com/snapcore/snapd/snap/snapfile"
    34  	"github.com/snapcore/snapd/snap/squashfs"
    35  	"github.com/snapcore/snapd/testutil"
    36  )
    37  
    38  func TestSnapfileTest(t *testing.T) { TestingT(t) }
    39  
    40  type snapFileTestSuite struct {
    41  	testutil.BaseTest
    42  }
    43  
    44  var _ = Suite(&snapFileTestSuite{})
    45  
    46  func (s *snapFileTestSuite) SetUpTest(c *C) {
    47  	dirs.SetRootDir(c.MkDir())
    48  
    49  	restore := osutil.MockMountInfo("")
    50  	s.AddCleanup(restore)
    51  }
    52  
    53  func (s *snapFileTestSuite) TestOpenSquashfs(c *C) {
    54  	// make a squashfs snap and try to open it with just the filename, then
    55  	// install it somewhere
    56  	tmp := c.MkDir()
    57  	err := os.MkdirAll(filepath.Join(tmp, "meta"), 0755)
    58  	c.Assert(err, IsNil)
    59  
    60  	// our regular snap.yaml
    61  	err = ioutil.WriteFile(filepath.Join(tmp, "meta", "snap.yaml"), []byte("name: foo"), 0644)
    62  	c.Assert(err, IsNil)
    63  
    64  	// build it
    65  	dir := c.MkDir()
    66  	snFilename := filepath.Join(dir, "foo.snap")
    67  	buildSn := squashfs.New(snFilename)
    68  	err = buildSn.Build(tmp, &squashfs.BuildOpts{SnapType: "app"})
    69  	c.Assert(err, IsNil)
    70  
    71  	sn, err := snapfile.Open(snFilename)
    72  	c.Assert(err, IsNil)
    73  
    74  	targetPath := filepath.Join(c.MkDir(), "target.snap")
    75  	mountDir := c.MkDir()
    76  	// we should have copied it
    77  	didNothing, err := sn.Install(targetPath, mountDir, nil)
    78  	c.Assert(err, IsNil)
    79  	c.Assert(didNothing, Equals, false)
    80  	c.Check(osutil.FileExists(targetPath), Equals, true)
    81  
    82  	r, err := sn.RandomAccessFile("meta/snap.yaml")
    83  	c.Assert(err, IsNil)
    84  	defer r.Close()
    85  
    86  	b := make([]byte, 5)
    87  	n, err := r.ReadAt(b, 4)
    88  	c.Assert(err, IsNil)
    89  	c.Assert(n, Equals, 5)
    90  	c.Check(string(b), Equals, ": foo")
    91  }
    92  
    93  func (s *snapFileTestSuite) TestOpenSnapdir(c *C) {
    94  	// make a snapdir snap and try to open it with just the filename, then
    95  	// install it somewhere
    96  	tmp := c.MkDir()
    97  	err := os.MkdirAll(filepath.Join(tmp, "meta"), 0755)
    98  	c.Assert(err, IsNil)
    99  
   100  	// our regular snap.yaml
   101  	err = ioutil.WriteFile(filepath.Join(tmp, "meta", "snap.yaml"), []byte("name: foo"), 0644)
   102  	c.Assert(err, IsNil)
   103  
   104  	sn, err := snapfile.Open(tmp)
   105  	c.Assert(err, IsNil)
   106  
   107  	targetPath := filepath.Join(c.MkDir(), "target.snap")
   108  	mountDir := c.MkDir()
   109  	// we should have copied it
   110  	didNothing, err := sn.Install(targetPath, mountDir, nil)
   111  	c.Assert(err, IsNil)
   112  	c.Assert(didNothing, Equals, false)
   113  	c.Check(osutil.FileExists(targetPath), Equals, true)
   114  
   115  	r, err := sn.RandomAccessFile("meta/snap.yaml")
   116  	c.Assert(err, IsNil)
   117  	defer r.Close()
   118  
   119  	b := make([]byte, 5)
   120  	n, err := r.ReadAt(b, 4)
   121  	c.Assert(err, IsNil)
   122  	c.Assert(n, Equals, 5)
   123  	c.Check(string(b), Equals, ": foo")
   124  }
   125  
   126  func (s *snapFileTestSuite) TestOpenSnapdirUnsupportedFormat(c *C) {
   127  	// make a file with garbage data
   128  	tmp := c.MkDir()
   129  	fn := filepath.Join(tmp, "some-format")
   130  	err := ioutil.WriteFile(fn, []byte("not-a-real-header"), 0644)
   131  	c.Assert(err, IsNil)
   132  
   133  	_, err = snapfile.Open(fn)
   134  	c.Assert(err, FitsTypeOf, snap.NotSnapError{})
   135  }
   136  
   137  func (s *snapFileTestSuite) TestOpenSnapdirFileNoExists(c *C) {
   138  	dir := c.MkDir()
   139  	_, err := snapfile.Open(filepath.Join(dir, "garbage"))
   140  	c.Assert(err, FitsTypeOf, snap.NotSnapError{})
   141  }
   142  
   143  func (s *snapFileTestSuite) TestFileOpenForSnapDirErrors(c *C) {
   144  	// no snap.yaml file
   145  	_, err := snapfile.Open(c.MkDir())
   146  	c.Assert(err, FitsTypeOf, snap.NotSnapError{})
   147  	c.Assert(err, ErrorMatches, `"/.*" is not a snap or snapdir`)
   148  }