gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/testutil/symlinktargetchecker_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 testutil_test 21 22 import ( 23 "os" 24 "path/filepath" 25 "regexp" 26 27 "gopkg.in/check.v1" 28 29 . "github.com/snapcore/snapd/testutil" 30 ) 31 32 type symlinkTargetCheckerSuite struct { 33 d string 34 symlink string 35 target string 36 } 37 38 var _ = check.Suite(&symlinkTargetCheckerSuite{}) 39 40 func (s *symlinkTargetCheckerSuite) SetUpTest(c *check.C) { 41 s.d = c.MkDir() 42 s.symlink = filepath.Join(s.d, "symlink") 43 s.target = "target" 44 c.Assert(os.Symlink(s.target, s.symlink), check.IsNil) 45 } 46 47 func (s *symlinkTargetCheckerSuite) TestSymlinkTargetEquals(c *check.C) { 48 testInfo(c, SymlinkTargetEquals, "SymlinkTargetEquals", []string{"filename", "target"}) 49 testCheck(c, SymlinkTargetEquals, true, "", s.symlink, s.target) 50 51 testCheck(c, SymlinkTargetEquals, false, "Failed to match with symbolic link target:\ntarget", s.symlink, "not-target") 52 testCheck(c, SymlinkTargetEquals, false, `Cannot read symbolic link: readlink missing: no such file or directory`, "missing", "") 53 testCheck(c, SymlinkTargetEquals, false, "Filename must be a string", 42, "") 54 testCheck(c, SymlinkTargetEquals, false, "Cannot compare symbolic link target with something of type int", s.symlink, 1) 55 } 56 57 func (s *symlinkTargetCheckerSuite) TestSymlinkTargetContains(c *check.C) { 58 testInfo(c, SymlinkTargetContains, "SymlinkTargetContains", []string{"filename", "target"}) 59 testCheck(c, SymlinkTargetContains, true, "", s.symlink, s.target[1:]) 60 testCheck(c, SymlinkTargetContains, true, "", s.symlink, regexp.MustCompile(".*")) 61 62 testCheck(c, SymlinkTargetContains, false, "Failed to match with symbolic link target:\ntarget", s.symlink, "not-target") 63 testCheck(c, SymlinkTargetContains, false, "Failed to match with symbolic link target:\ntarget", s.symlink, regexp.MustCompile("^$")) 64 testCheck(c, SymlinkTargetContains, false, `Cannot read symbolic link: readlink missing: no such file or directory`, "missing", "") 65 testCheck(c, SymlinkTargetContains, false, "Filename must be a string", 42, "") 66 testCheck(c, SymlinkTargetContains, false, "Cannot compare symbolic link target with something of type int", s.symlink, 1) 67 } 68 69 func (s *symlinkTargetCheckerSuite) TestSymlinkTargetMatches(c *check.C) { 70 testInfo(c, SymlinkTargetMatches, "SymlinkTargetMatches", []string{"filename", "regex"}) 71 testCheck(c, SymlinkTargetMatches, true, "", s.symlink, ".*") 72 testCheck(c, SymlinkTargetMatches, true, "", s.symlink, "^"+regexp.QuoteMeta(s.target)+"$") 73 74 testCheck(c, SymlinkTargetMatches, false, "Failed to match with symbolic link target:\ntarget", s.symlink, "^$") 75 testCheck(c, SymlinkTargetMatches, false, "Failed to match with symbolic link target:\ntarget", s.symlink, "123"+regexp.QuoteMeta(s.target)) 76 testCheck(c, SymlinkTargetMatches, false, `Cannot read symbolic link: readlink missing: no such file or directory`, "missing", "") 77 testCheck(c, SymlinkTargetMatches, false, "Filename must be a string", 42, ".*") 78 testCheck(c, SymlinkTargetMatches, false, "Regex must be a string", s.symlink, 1) 79 }