github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/testutil/syscallschecker_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  	"errors"
    24  
    25  	"gopkg.in/check.v1"
    26  
    27  	. "github.com/snapcore/snapd/testutil"
    28  )
    29  
    30  type syscallsCheckerSuite struct{}
    31  
    32  var _ = check.Suite(&syscallsCheckerSuite{})
    33  
    34  func (*syscallsCheckerSuite) TestSystemCallSequenceEqual(c *check.C) {
    35  	c.Assert([]CallResultError{}, SyscallsEqual, []CallResultError{})
    36  	c.Assert([]CallResultError{}, SyscallsEqual, []CallResultError(nil))
    37  	c.Assert([]CallResultError{{C: `foo`}}, SyscallsEqual, []CallResultError{{C: `foo`}})
    38  	c.Assert([]CallResultError{{C: `foo`}, {C: `bar`}}, SyscallsEqual, []CallResultError{{C: `foo`}, {C: `bar`}})
    39  	c.Assert([]CallResultError{{C: `foo`, R: 123}}, SyscallsEqual, []CallResultError{{C: `foo`, R: 123}})
    40  	c.Assert([]CallResultError{{C: `foo`, E: errors.New("bad")}}, SyscallsEqual, []CallResultError{{C: `foo`, E: errors.New("bad")}})
    41  
    42  	// Wrong argument types.
    43  	testCheck(c, SyscallsEqual, false, "left-hand-side argument must be of type []CallResultError",
    44  		true, []CallResultError{{C: `bar`}})
    45  	testCheck(c, SyscallsEqual, false, "right-hand-side argument must be of type []CallResultError",
    46  		[]CallResultError{{C: `bar`}}, true)
    47  	// Different system call operations.
    48  	testCheck(c, SyscallsEqual, false, "system call #0 differs in operation, actual `foo`, expected `bar`",
    49  		[]CallResultError{{C: `foo`}}, []CallResultError{{C: `bar`}})
    50  	// Different system call results.
    51  	testCheck(c, SyscallsEqual, false, "system call #0 `foo` differs in result, actual: 1, expected: 2",
    52  		[]CallResultError{{C: `foo`, R: 1}}, []CallResultError{{C: `foo`, R: 2}})
    53  	// Different system call errors.
    54  	testCheck(c, SyscallsEqual, false, "system call #0 `foo` differs in error, actual: barf, expected: bork",
    55  		[]CallResultError{{C: `foo`, E: errors.New("barf")}}, []CallResultError{{C: `foo`, E: errors.New("bork")}})
    56  	// Unexpected success with non-nil result.
    57  	testCheck(c, SyscallsEqual, false, "system call #0 `foo` unexpectedly succeeded, actual result: 1, expected error: broken",
    58  		[]CallResultError{{C: `foo`, R: 1}}, []CallResultError{{C: `foo`, E: errors.New("broken")}})
    59  	// Unexpected success with nil result.
    60  	testCheck(c, SyscallsEqual, false, "system call #0 `foo` unexpectedly succeeded, expected error: broken",
    61  		[]CallResultError{{C: `foo`}}, []CallResultError{{C: `foo`, E: errors.New("broken")}})
    62  	// Unexpected failure with expected non-nil result.
    63  	testCheck(c, SyscallsEqual, false, "system call #0 `foo` unexpectedly failed, actual error: broken, expected result: 1",
    64  		[]CallResultError{{C: `foo`, E: errors.New("broken")}}, []CallResultError{{C: `foo`, R: 1}})
    65  	// Unexpected failure with expected nil result.
    66  	testCheck(c, SyscallsEqual, false, "system call #0 `foo` unexpectedly failed, actual error: broken",
    67  		[]CallResultError{{C: `foo`, E: errors.New("broken")}}, []CallResultError{{C: `foo`}})
    68  	// More system calls than expected.
    69  	testCheck(c, SyscallsEqual, false, "system call #1 `bar` unexpectedly present, got 2 system call(s) but expected only 1",
    70  		[]CallResultError{{C: `foo`}, {C: `bar`}}, []CallResultError{{C: `foo`}})
    71  	testCheck(c, SyscallsEqual, false, "system call #0 `foo` unexpectedly present, got 2 system call(s) but expected only 0",
    72  		[]CallResultError{{C: `foo`}, {C: `bar`}}, []CallResultError{})
    73  	// Fewer system calls than expected.
    74  	testCheck(c, SyscallsEqual, false, "system call #1 `bar` unexpectedly absent, got only 1 system call(s) but expected 2",
    75  		[]CallResultError{{C: `foo`}}, []CallResultError{{C: `foo`}, {C: `bar`}})
    76  	testCheck(c, SyscallsEqual, false, "system call #0 `foo` unexpectedly absent, got only 0 system call(s) but expected 1",
    77  		[]CallResultError{}, []CallResultError{{C: `foo`}})
    78  }