github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/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 "fmt" 24 "io/ioutil" 25 "path/filepath" 26 "regexp" 27 28 "gopkg.in/check.v1" 29 30 . "github.com/snapcore/snapd/testutil" 31 ) 32 33 type fileContentCheckerSuite struct{} 34 35 var _ = check.Suite(&fileContentCheckerSuite{}) 36 37 type myStringer struct{ str string } 38 39 func (m myStringer) String() string { return m.str } 40 41 func (s *fileContentCheckerSuite) TestFileEquals(c *check.C) { 42 d := c.MkDir() 43 content := "not-so-random-string" 44 filename := filepath.Join(d, "canary") 45 c.Assert(ioutil.WriteFile(filename, []byte(content), 0644), check.IsNil) 46 equalRefereceFilename := filepath.Join(d, "canary-reference-equal") 47 c.Assert(ioutil.WriteFile(equalRefereceFilename, []byte(content), 0644), check.IsNil) 48 notEqualRefereceFilename := filepath.Join(d, "canary-reference-not-equal") 49 c.Assert(ioutil.WriteFile(notEqualRefereceFilename, []byte("not-equal"), 0644), check.IsNil) 50 51 testInfo(c, FileEquals, "FileEquals", []string{"filename", "contents"}) 52 testCheck(c, FileEquals, true, "", filename, content) 53 testCheck(c, FileEquals, true, "", filename, []byte(content)) 54 testCheck(c, FileEquals, true, "", filename, myStringer{content}) 55 testCheck(c, FileEquals, true, "", filename, FileContentRef(filename)) 56 testCheck(c, FileEquals, true, "", filename, FileContentRef(equalRefereceFilename)) 57 58 twofer := content + content 59 testCheck(c, FileEquals, false, "Failed to match with file contents:\nnot-so-random-string", filename, twofer) 60 testCheck(c, FileEquals, false, "Failed to match with file contents:\n<binary data>", filename, []byte(twofer)) 61 testCheck(c, FileEquals, false, "Failed to match with file contents:\nnot-so-random-string", filename, myStringer{twofer}) 62 63 testCheck(c, FileEquals, false, `Cannot read file "": open : no such file or directory`, "", "") 64 testCheck(c, FileEquals, false, "Filename must be a string", 42, "") 65 testCheck(c, FileEquals, false, "Cannot compare file contents with something of type int", filename, 1) 66 testCheck(c, FileEquals, false, 67 fmt.Sprintf("Failed to match contents with reference file \"%s\":\nnot-so-random-string", notEqualRefereceFilename), 68 filename, FileContentRef(notEqualRefereceFilename)) 69 } 70 71 func (s *fileContentCheckerSuite) TestFileContains(c *check.C) { 72 d := c.MkDir() 73 content := "not-so-random-string" 74 filename := filepath.Join(d, "canary") 75 c.Assert(ioutil.WriteFile(filename, []byte(content), 0644), check.IsNil) 76 77 testInfo(c, FileContains, "FileContains", []string{"filename", "contents"}) 78 testCheck(c, FileContains, true, "", filename, content[1:]) 79 testCheck(c, FileContains, true, "", filename, []byte(content[1:])) 80 testCheck(c, FileContains, true, "", filename, myStringer{content[1:]}) 81 // undocumented 82 testCheck(c, FileContains, true, "", filename, regexp.MustCompile(".*")) 83 84 twofer := content + content 85 testCheck(c, FileContains, false, "Failed to match with file contents:\nnot-so-random-string", filename, twofer) 86 testCheck(c, FileContains, false, "Failed to match with file contents:\n<binary data>", filename, []byte(twofer)) 87 testCheck(c, FileContains, false, "Failed to match with file contents:\nnot-so-random-string", filename, myStringer{twofer}) 88 // undocumented 89 testCheck(c, FileContains, false, "Failed to match with file contents:\nnot-so-random-string", filename, regexp.MustCompile("^$")) 90 91 testCheck(c, FileContains, false, `Cannot read file "": open : no such file or directory`, "", "") 92 testCheck(c, FileContains, false, "Filename must be a string", 42, "") 93 testCheck(c, FileContains, false, "Cannot compare file contents with something of type int", filename, 1) 94 testCheck(c, FileContains, false, `Non-exact match with reference file is not supported`, 95 filename, FileContentRef(filename)) 96 } 97 98 func (s *fileContentCheckerSuite) TestFileMatches(c *check.C) { 99 d := c.MkDir() 100 content := "not-so-random-string" 101 filename := filepath.Join(d, "canary") 102 c.Assert(ioutil.WriteFile(filename, []byte(content), 0644), check.IsNil) 103 104 testInfo(c, FileMatches, "FileMatches", []string{"filename", "regex"}) 105 testCheck(c, FileMatches, true, "", filename, ".*") 106 testCheck(c, FileMatches, true, "", filename, "^"+regexp.QuoteMeta(content)+"$") 107 108 testCheck(c, FileMatches, false, "Failed to match with file contents:\nnot-so-random-string", filename, "^$") 109 testCheck(c, FileMatches, false, "Failed to match with file contents:\nnot-so-random-string", filename, "123"+regexp.QuoteMeta(content)) 110 111 testCheck(c, FileMatches, false, `Cannot read file "": open : no such file or directory`, "", "") 112 testCheck(c, FileMatches, false, "Filename must be a string", 42, ".*") 113 testCheck(c, FileMatches, false, "Regex must be a string", filename, 1) 114 testCheck(c, FileContains, false, `Non-exact match with reference file is not supported`, 115 filename, FileContentRef(filename)) 116 }