github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/testutil/filecontentchecker_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2015-2018 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 testutil_test
    21  
    22  import (
    23  	"io/ioutil"
    24  	"path/filepath"
    25  	"regexp"
    26  
    27  	"gopkg.in/check.v1"
    28  
    29  	. "github.com/snapcore/snapd/testutil"
    30  )
    31  
    32  type fileContentCheckerSuite struct{}
    33  
    34  var _ = check.Suite(&fileContentCheckerSuite{})
    35  
    36  type myStringer struct{ str string }
    37  
    38  func (m myStringer) String() string { return m.str }
    39  
    40  func (s *fileContentCheckerSuite) TestFileEquals(c *check.C) {
    41  	d := c.MkDir()
    42  	content := "not-so-random-string"
    43  	filename := filepath.Join(d, "canary")
    44  	c.Assert(ioutil.WriteFile(filename, []byte(content), 0644), check.IsNil)
    45  
    46  	testInfo(c, FileEquals, "FileEquals", []string{"filename", "contents"})
    47  	testCheck(c, FileEquals, true, "", filename, content)
    48  	testCheck(c, FileEquals, true, "", filename, []byte(content))
    49  	testCheck(c, FileEquals, true, "", filename, myStringer{content})
    50  
    51  	twofer := content + content
    52  	testCheck(c, FileEquals, false, "Failed to match with file contents:\nnot-so-random-string", filename, twofer)
    53  	testCheck(c, FileEquals, false, "Failed to match with file contents:\n<binary data>", filename, []byte(twofer))
    54  	testCheck(c, FileEquals, false, "Failed to match with file contents:\nnot-so-random-string", filename, myStringer{twofer})
    55  
    56  	testCheck(c, FileEquals, false, `Cannot read file "": open : no such file or directory`, "", "")
    57  	testCheck(c, FileEquals, false, "Filename must be a string", 42, "")
    58  	testCheck(c, FileEquals, false, "Cannot compare file contents with something of type int", filename, 1)
    59  }
    60  
    61  func (s *fileContentCheckerSuite) TestFileContains(c *check.C) {
    62  	d := c.MkDir()
    63  	content := "not-so-random-string"
    64  	filename := filepath.Join(d, "canary")
    65  	c.Assert(ioutil.WriteFile(filename, []byte(content), 0644), check.IsNil)
    66  
    67  	testInfo(c, FileContains, "FileContains", []string{"filename", "contents"})
    68  	testCheck(c, FileContains, true, "", filename, content[1:])
    69  	testCheck(c, FileContains, true, "", filename, []byte(content[1:]))
    70  	testCheck(c, FileContains, true, "", filename, myStringer{content[1:]})
    71  	// undocumented
    72  	testCheck(c, FileContains, true, "", filename, regexp.MustCompile(".*"))
    73  
    74  	twofer := content + content
    75  	testCheck(c, FileContains, false, "Failed to match with file contents:\nnot-so-random-string", filename, twofer)
    76  	testCheck(c, FileContains, false, "Failed to match with file contents:\n<binary data>", filename, []byte(twofer))
    77  	testCheck(c, FileContains, false, "Failed to match with file contents:\nnot-so-random-string", filename, myStringer{twofer})
    78  	// undocumented
    79  	testCheck(c, FileContains, false, "Failed to match with file contents:\nnot-so-random-string", filename, regexp.MustCompile("^$"))
    80  
    81  	testCheck(c, FileContains, false, `Cannot read file "": open : no such file or directory`, "", "")
    82  	testCheck(c, FileContains, false, "Filename must be a string", 42, "")
    83  	testCheck(c, FileContains, false, "Cannot compare file contents with something of type int", filename, 1)
    84  }
    85  
    86  func (s *fileContentCheckerSuite) TestFileMatches(c *check.C) {
    87  	d := c.MkDir()
    88  	content := "not-so-random-string"
    89  	filename := filepath.Join(d, "canary")
    90  	c.Assert(ioutil.WriteFile(filename, []byte(content), 0644), check.IsNil)
    91  
    92  	testInfo(c, FileMatches, "FileMatches", []string{"filename", "regex"})
    93  	testCheck(c, FileMatches, true, "", filename, ".*")
    94  	testCheck(c, FileMatches, true, "", filename, "^"+regexp.QuoteMeta(content)+"$")
    95  
    96  	testCheck(c, FileMatches, false, "Failed to match with file contents:\nnot-so-random-string", filename, "^$")
    97  	testCheck(c, FileMatches, false, "Failed to match with file contents:\nnot-so-random-string", filename, "123"+regexp.QuoteMeta(content))
    98  
    99  	testCheck(c, FileMatches, false, `Cannot read file "": open : no such file or directory`, "", "")
   100  	testCheck(c, FileMatches, false, "Filename must be a string", 42, ".*")
   101  	testCheck(c, FileMatches, false, "Regex must be a string", filename, 1)
   102  }