github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/snapdtool/cmdutil_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-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 snapdtool_test 21 22 import ( 23 "fmt" 24 "io/ioutil" 25 "os" 26 "os/exec" 27 "path/filepath" 28 "strings" 29 30 . "gopkg.in/check.v1" 31 32 "github.com/snapcore/snapd/dirs" 33 "github.com/snapcore/snapd/osutil" 34 "github.com/snapcore/snapd/snapdtool" 35 ) 36 37 var truePath = osutil.LookPathDefault("true", "/bin/true") 38 39 type cmdutilSuite struct{} 40 41 var _ = Suite(&cmdutilSuite{}) 42 43 func (s *cmdutilSuite) SetUpTest(c *C) { 44 dirs.SetRootDir(c.MkDir()) 45 } 46 47 func (s *cmdutilSuite) TearDownTest(c *C) { 48 dirs.SetRootDir("") 49 } 50 51 func (s *cmdutilSuite) makeMockLdSoConf(c *C, root string) { 52 ldSoConf := filepath.Join(root, "/etc/ld.so.conf") 53 ldSoConfD := ldSoConf + ".d" 54 55 err := os.MkdirAll(filepath.Dir(ldSoConf), 0755) 56 c.Assert(err, IsNil) 57 err = os.MkdirAll(ldSoConfD, 0755) 58 c.Assert(err, IsNil) 59 60 err = ioutil.WriteFile(ldSoConf, []byte("include /etc/ld.so.conf.d/*.conf"), 0644) 61 c.Assert(err, IsNil) 62 63 ldSoConf1 := filepath.Join(ldSoConfD, "x86_64-linux-gnu.conf") 64 65 err = ioutil.WriteFile(ldSoConf1, []byte(` 66 # Multiarch support 67 /lib/x86_64-linux-gnu 68 /usr/lib/x86_64-linux-gnu`), 0644) 69 c.Assert(err, IsNil) 70 } 71 72 func (s *cmdutilSuite) TestCommandFromSystemSnap(c *C) { 73 for _, snap := range []string{"core", "snapd"} { 74 75 root := filepath.Join(dirs.SnapMountDir, snap, "current") 76 s.makeMockLdSoConf(c, root) 77 78 os.MkdirAll(filepath.Join(root, "/usr/bin"), 0755) 79 osutil.CopyFile(truePath, filepath.Join(root, "/usr/bin/xdelta3"), 0) 80 cmd, err := snapdtool.CommandFromSystemSnap("/usr/bin/xdelta3", "--some-xdelta-arg") 81 c.Assert(err, IsNil) 82 83 out, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("readelf -l %s |grep interpreter:|cut -f2 -d:|cut -f1 -d]", truePath)).Output() 84 c.Assert(err, IsNil) 85 interp := strings.TrimSpace(string(out)) 86 87 c.Check(cmd.Args, DeepEquals, []string{ 88 filepath.Join(root, interp), 89 "--library-path", 90 fmt.Sprintf("%s/lib/x86_64-linux-gnu:%s/usr/lib/x86_64-linux-gnu", root, root), 91 filepath.Join(root, "/usr/bin/xdelta3"), 92 "--some-xdelta-arg", 93 }) 94 } 95 } 96 97 func (s *cmdutilSuite) TestCommandFromCoreSymlinkCycle(c *C) { 98 root := filepath.Join(dirs.SnapMountDir, "/core/current") 99 s.makeMockLdSoConf(c, root) 100 101 os.MkdirAll(filepath.Join(root, "/usr/bin"), 0755) 102 osutil.CopyFile(truePath, filepath.Join(root, "/usr/bin/xdelta3"), 0) 103 104 out, err := exec.Command("/bin/sh", "-c", "readelf -l /bin/true |grep interpreter:|cut -f2 -d:|cut -f1 -d]").Output() 105 c.Assert(err, IsNil) 106 interp := strings.TrimSpace(string(out)) 107 108 coreInterp := filepath.Join(root, interp) 109 c.Assert(os.MkdirAll(filepath.Dir(coreInterp), 0755), IsNil) 110 c.Assert(os.Symlink(filepath.Base(coreInterp), coreInterp), IsNil) 111 112 _, err = snapdtool.CommandFromSystemSnap("/usr/bin/xdelta3", "--some-xdelta-arg") 113 c.Assert(err, ErrorMatches, "cannot run command from core: symlink cycle found") 114 }