github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/seccomp/export_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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 seccomp
    21  
    22  import (
    23  	seccomp_compiler "github.com/snapcore/snapd/sandbox/seccomp"
    24  )
    25  
    26  // MockTemplate replaces seccomp template.
    27  //
    28  // NOTE: The real seccomp template is long. For testing it is convenient for
    29  // replace it with a shorter snippet.
    30  func MockTemplate(fakeTemplate []byte) (restore func()) {
    31  	orig := defaultTemplate
    32  	origBarePrivDropSyscalls := barePrivDropSyscalls
    33  	defaultTemplate = fakeTemplate
    34  	barePrivDropSyscalls = ""
    35  	return func() {
    36  		defaultTemplate = orig
    37  		barePrivDropSyscalls = origBarePrivDropSyscalls
    38  	}
    39  }
    40  
    41  func MockKernelFeatures(f func() []string) (resture func()) {
    42  	old := kernelFeatures
    43  	kernelFeatures = f
    44  	return func() {
    45  		kernelFeatures = old
    46  	}
    47  }
    48  
    49  func MockRequiresSocketcall(f func(string) bool) (restore func()) {
    50  	old := requiresSocketcall
    51  	requiresSocketcall = f
    52  	return func() {
    53  		requiresSocketcall = old
    54  	}
    55  }
    56  
    57  func MockDpkgKernelArchitecture(f func() string) (restore func()) {
    58  	old := dpkgKernelArchitecture
    59  	dpkgKernelArchitecture = f
    60  	return func() {
    61  		dpkgKernelArchitecture = old
    62  	}
    63  }
    64  
    65  func MockReleaseInfoId(s string) (restore func()) {
    66  	old := releaseInfoId
    67  	releaseInfoId = s
    68  	return func() {
    69  		releaseInfoId = old
    70  	}
    71  }
    72  
    73  func MockReleaseInfoVersionId(s string) (restore func()) {
    74  	old := releaseInfoVersionId
    75  	releaseInfoVersionId = s
    76  	return func() {
    77  		releaseInfoVersionId = old
    78  	}
    79  }
    80  
    81  func MockSeccompCompilerLookup(f func(string) (string, error)) (restore func()) {
    82  	old := seccompCompilerLookup
    83  	seccompCompilerLookup = f
    84  	return func() {
    85  		seccompCompilerLookup = old
    86  	}
    87  }
    88  
    89  func (b *Backend) VersionInfo() seccomp_compiler.VersionInfo {
    90  	return b.versionInfo
    91  }
    92  
    93  var (
    94  	RequiresSocketcall = requiresSocketcall
    95  
    96  	GlobalProfileLE = globalProfileLE
    97  	GlobalProfileBE = globalProfileBE
    98  	IsBigEndian     = isBigEndian
    99  
   100  	ParallelCompile = parallelCompile
   101  )