gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/apparmor/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 apparmor
    21  
    22  import (
    23  	"os"
    24  
    25  	apparmor_sandbox "gitee.com/mysnapcore/mysnapd/sandbox/apparmor"
    26  	"gitee.com/mysnapcore/mysnapd/snap"
    27  	"gitee.com/mysnapcore/mysnapd/testutil"
    28  )
    29  
    30  var (
    31  	NsProfile                       = nsProfile
    32  	ProfileGlobs                    = profileGlobs
    33  	SnapConfineFromSnapProfile      = snapConfineFromSnapProfile
    34  	DefaultCoreRuntimeTemplateRules = defaultCoreRuntimeTemplateRules
    35  	DefaultOtherBaseTemplateRules   = defaultOtherBaseTemplateRules
    36  )
    37  
    38  func MockLoadProfiles(f func(fnames []string, cacheDir string, flags apparmor_sandbox.AaParserFlags) error) (restore func()) {
    39  	r := testutil.Backup(&loadProfiles)
    40  	loadProfiles = f
    41  	return r
    42  }
    43  
    44  func MockUnloadProfiles(f func(fnames []string, cacheDir string) error) (restore func()) {
    45  	r := testutil.Backup(&unloadProfiles)
    46  	unloadProfiles = f
    47  	return r
    48  }
    49  
    50  // MockIsRootWritableOverlay mocks the real implementation of osutil.IsRootWritableOverlay
    51  func MockIsRootWritableOverlay(new func() (string, error)) (restore func()) {
    52  	old := isRootWritableOverlay
    53  	isRootWritableOverlay = new
    54  	return func() {
    55  		isRootWritableOverlay = old
    56  	}
    57  }
    58  
    59  // MockProcSelfExe mocks the location of /proc/self/exe read by setupSnapConfineGeneratedPolicy.
    60  func MockProcSelfExe(symlink string) (restore func()) {
    61  	old := procSelfExe
    62  	procSelfExe = symlink
    63  	return func() {
    64  		os.Remove(procSelfExe)
    65  		procSelfExe = old
    66  	}
    67  }
    68  
    69  // MockTemplate replaces apprmor template.
    70  //
    71  // NOTE: The real apparmor template is long. For testing it is convenient for
    72  // replace it with a shorter snippet.
    73  func MockTemplate(fakeTemplate string) (restore func()) {
    74  	orig := defaultCoreRuntimeTemplate
    75  	defaultCoreRuntimeTemplate = fakeTemplate
    76  	return func() { defaultCoreRuntimeTemplate = orig }
    77  }
    78  
    79  // MockClassicTemplate replaces the classic apprmor template.
    80  func MockClassicTemplate(fakeTemplate string) (restore func()) {
    81  	orig := classicTemplate
    82  	classicTemplate = fakeTemplate
    83  	return func() { classicTemplate = orig }
    84  }
    85  
    86  // SetSpecScope sets the scope of a given specification
    87  func SetSpecScope(spec *Specification, securityTags []string) (restore func()) {
    88  	return spec.setScope(securityTags)
    89  }
    90  
    91  func MockKernelFeatures(f func() ([]string, error)) (resture func()) {
    92  	old := kernelFeatures
    93  	kernelFeatures = f
    94  	return func() {
    95  		kernelFeatures = old
    96  	}
    97  }
    98  
    99  func MockParserFeatures(f func() ([]string, error)) (resture func()) {
   100  	old := parserFeatures
   101  	parserFeatures = f
   102  	return func() {
   103  		parserFeatures = old
   104  	}
   105  }
   106  
   107  func (b *Backend) SetupSnapConfineReexec(info *snap.Info) error {
   108  	return b.setupSnapConfineReexec(info)
   109  }
   110  
   111  func (s *Specification) SnippetsForTag(tag string) []string {
   112  	return s.snippetsForTag(tag)
   113  }