github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/snap-seccomp/versioninfo_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2019 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 main_test 21 22 import ( 23 "fmt" 24 "strings" 25 26 "github.com/mvo5/libseccomp-golang" 27 . "gopkg.in/check.v1" 28 29 main "github.com/snapcore/snapd/cmd/snap-seccomp" 30 "github.com/snapcore/snapd/osutil" 31 ) 32 33 type versionInfoSuite struct{} 34 35 var _ = Suite(&versionInfoSuite{}) 36 37 func (s *versionInfoSuite) TestVersionInfo(c *C) { 38 buildID, err := osutil.MyBuildID() 39 c.Assert(err, IsNil) 40 41 m, i, p := seccomp.GetLibraryVersion() 42 prefix := fmt.Sprintf("%s %d.%d.%d ", buildID, m, i, p) 43 suffix := fmt.Sprintf(" %s", main.GoSeccompFeatures()) 44 45 defaultVi, err := main.VersionInfo() 46 c.Assert(err, IsNil) 47 48 // $ echo -n 'read\nwrite\n' | sha256sum 49 // 88b06efcea4b5946cebd4b0674b93744de328339de5d61b75db858119054ff93 - 50 readWriteHash := "88b06efcea4b5946cebd4b0674b93744de328339de5d61b75db858119054ff93" 51 52 c.Check(strings.HasPrefix(defaultVi, prefix), Equals, true) 53 c.Check(strings.HasSuffix(defaultVi, suffix), Equals, true) 54 c.Assert(len(defaultVi) > len(prefix)+len(suffix), Equals, true) 55 hash := defaultVi[len(prefix) : len(defaultVi)-len(suffix)] 56 c.Check(len(hash), Equals, len(readWriteHash)) 57 c.Check(hash, Not(Equals), readWriteHash) 58 59 restore := main.MockSeccompSyscalls([]string{"read", "write"}) 60 defer restore() 61 62 vi, err := main.VersionInfo() 63 c.Assert(err, IsNil) 64 c.Check(vi, Equals, prefix+readWriteHash+suffix) 65 66 // pretend it's only 'read' now 67 readHash := "15fd60c6f5c6804626177d178f3dba849a41f4a1878b2e7e7e3ed38a194dc82b" 68 restore = main.MockSeccompSyscalls([]string{"read"}) 69 defer restore() 70 71 vi, err = main.VersionInfo() 72 c.Assert(err, IsNil) 73 c.Check(vi, Equals, prefix+readHash+suffix) 74 }