github.com/stulluk/snapd@v0.0.0-20210611110309-f6d5d5bd24b0/kernel/validate_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020Canonical 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 kernel_test
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/kernel"
    30  )
    31  
    32  type validateKernelSuite struct {
    33  	dir string
    34  }
    35  
    36  var _ = Suite(&validateKernelSuite{})
    37  
    38  func (s *validateKernelSuite) SetUpTest(c *C) {
    39  	s.dir = c.MkDir()
    40  }
    41  
    42  func (s *validateKernelSuite) TestValidateMissingContentFile(c *C) {
    43  	var kernelYaml = `
    44  assets:
    45    dtbs:
    46      edition: 1
    47      content:
    48        - foo
    49  `
    50  	mockKernelRoot := makeMockKernel(c, kernelYaml, nil)
    51  
    52  	err := kernel.Validate(mockKernelRoot)
    53  	c.Assert(err, ErrorMatches, `asset "dtbs": content "foo" source path does not exist`)
    54  }
    55  
    56  func (s *validateKernelSuite) TestValidateMissingContentDir(c *C) {
    57  	var kernelYaml = `
    58  assets:
    59    dtbs:
    60      edition: 1
    61      content:
    62        - dir/
    63  `
    64  	mockKernelRoot := makeMockKernel(c, kernelYaml, map[string]string{"dir": ""})
    65  
    66  	err := kernel.Validate(mockKernelRoot)
    67  	c.Assert(err, ErrorMatches, `asset "dtbs": content "dir/" is not a directory`)
    68  }
    69  
    70  func (s *validateKernelSuite) TestValidateHappy(c *C) {
    71  	var kernelYaml = `
    72  assets:
    73    dtbs:
    74      edition: 1
    75      content:
    76        - foo
    77        - dir/
    78  `
    79  	mockKernelRoot := makeMockKernel(c, kernelYaml, map[string]string{
    80  		"foo": "",
    81  	})
    82  	err := os.MkdirAll(filepath.Join(mockKernelRoot, "dir"), 0755)
    83  	c.Assert(err, IsNil)
    84  
    85  	err = kernel.Validate(mockKernelRoot)
    86  	c.Assert(err, IsNil)
    87  }
    88  
    89  func (s *validateKernelSuite) TestValidateHappyNoKernelYaml(c *C) {
    90  	emptyDir := c.MkDir()
    91  	err := kernel.Validate(emptyDir)
    92  	c.Assert(err, IsNil)
    93  }
    94  
    95  func (s *validateKernelSuite) TestValidateBadContent(c *C) {
    96  	var kernelYamlFmt = `
    97  assets:
    98    dtbs:
    99      edition: 1
   100      content:
   101        - %s
   102  `
   103  	for _, tc := range []string{
   104  		"../",
   105  		"/foo/../bar/..",
   106  		"..",
   107  		"//",
   108  	} {
   109  		mockKernelRoot := makeMockKernel(c, fmt.Sprintf(kernelYamlFmt, tc), nil)
   110  
   111  		err := kernel.Validate(mockKernelRoot)
   112  		c.Assert(err, ErrorMatches, fmt.Sprintf(`asset "dtbs": invalid content %q`, tc))
   113  	}
   114  }