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