1/* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15*/ 16 17package server 18 19import ( 20 "testing" 21 22 "github.com/opencontainers/selinux/go-selinux" 23 "github.com/stretchr/testify/assert" 24 runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" 25) 26 27func TestInitSelinuxOpts(t *testing.T) { 28 if !selinux.GetEnabled() { 29 t.Skip("selinux is not enabled") 30 } 31 32 for desc, test := range map[string]struct { 33 selinuxOpt *runtime.SELinuxOption 34 processLabel string 35 mountLabel string 36 expectErr bool 37 }{ 38 "Should return empty strings for processLabel and mountLabel when selinuxOpt is nil": { 39 selinuxOpt: nil, 40 processLabel: ".*:c[0-9]{1,3},c[0-9]{1,3}", 41 mountLabel: ".*:c[0-9]{1,3},c[0-9]{1,3}", 42 }, 43 "Should overlay fields on processLabel when selinuxOpt has been initialized partially": { 44 selinuxOpt: &runtime.SELinuxOption{ 45 User: "", 46 Role: "user_r", 47 Type: "", 48 Level: "s0:c1,c2", 49 }, 50 processLabel: "system_u:user_r:(container_file_t|svirt_lxc_net_t):s0:c1,c2", 51 mountLabel: "system_u:object_r:(container_file_t|svirt_sandbox_file_t):s0:c1,c2", 52 }, 53 "Should be resolved correctly when selinuxOpt has been initialized completely": { 54 selinuxOpt: &runtime.SELinuxOption{ 55 User: "user_u", 56 Role: "user_r", 57 Type: "user_t", 58 Level: "s0:c1,c2", 59 }, 60 processLabel: "user_u:user_r:user_t:s0:c1,c2", 61 mountLabel: "user_u:object_r:(container_file_t|svirt_sandbox_file_t):s0:c1,c2", 62 }, 63 "Should be resolved correctly when selinuxOpt has been initialized with level=''": { 64 selinuxOpt: &runtime.SELinuxOption{ 65 User: "user_u", 66 Role: "user_r", 67 Type: "user_t", 68 Level: "", 69 }, 70 processLabel: "user_u:user_r:user_t:s0:c[0-9]{1,3},c[0-9]{1,3}", 71 mountLabel: "user_u:object_r:(container_file_t|svirt_sandbox_file_t):s0", 72 }, 73 "Should return error when the format of 'level' is not correct": { 74 selinuxOpt: &runtime.SELinuxOption{ 75 User: "user_u", 76 Role: "user_r", 77 Type: "user_t", 78 Level: "s0,c1,c2", 79 }, 80 expectErr: true, 81 }, 82 } { 83 t.Run(desc, func(t *testing.T) { 84 processLabel, mountLabel, err := initLabelsFromOpt(test.selinuxOpt) 85 if test.expectErr { 86 assert.Error(t, err) 87 } else { 88 assert.Regexp(t, test.processLabel, processLabel) 89 assert.Regexp(t, test.mountLabel, mountLabel) 90 } 91 }) 92 } 93} 94 95func TestCheckSelinuxLevel(t *testing.T) { 96 for desc, test := range map[string]struct { 97 level string 98 expectNoMatch bool 99 }{ 100 "s0": { 101 level: "s0", 102 }, 103 "s0-s0": { 104 level: "s0-s0", 105 }, 106 "s0:c0": { 107 level: "s0:c0", 108 }, 109 "s0:c0.c3": { 110 level: "s0:c0.c3", 111 }, 112 "s0:c0,c3": { 113 level: "s0:c0,c3", 114 }, 115 "s0-s0:c0,c3": { 116 level: "s0-s0:c0,c3", 117 }, 118 "s0-s0:c0,c3.c6": { 119 level: "s0-s0:c0,c3.c6", 120 }, 121 "s0-s0:c0,c3.c6,c8.c10": { 122 level: "s0-s0:c0,c3.c6,c8.c10", 123 }, 124 "s0-s0:c0,c3.c6,c8,c10": { 125 level: "s0-s0:c0,c3.c6", 126 }, 127 "s0,c0,c3": { 128 level: "s0,c0,c3", 129 expectNoMatch: true, 130 }, 131 "s0:c0.c3.c6": { 132 level: "s0:c0.c3.c6", 133 expectNoMatch: true, 134 }, 135 "s0-s0,c0,c3": { 136 level: "s0-s0,c0,c3", 137 expectNoMatch: true, 138 }, 139 "s0-s0:c0.c3.c6": { 140 level: "s0-s0:c0.c3.c6", 141 expectNoMatch: true, 142 }, 143 "s0-s0:c0,c3.c6.c8": { 144 level: "s0-s0:c0,c3.c6.c8", 145 expectNoMatch: true, 146 }, 147 } { 148 t.Run(desc, func(t *testing.T) { 149 err := checkSelinuxLevel(test.level) 150 if test.expectNoMatch { 151 assert.Error(t, err) 152 } else { 153 assert.NoError(t, err) 154 } 155 }) 156 } 157} 158