|
1 | 1 | package user |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "io" |
| 5 | + "reflect" |
4 | 6 | "strings" |
5 | 7 | "testing" |
6 | 8 | ) |
@@ -92,3 +94,259 @@ this is just some garbage data |
92 | 94 | t.Fatalf("Expected groups[1] to be 4 - adm - 3 members, got %v - %v - %v", groups[1].Gid, groups[1].Name, len(groups[1].List)) |
93 | 95 | } |
94 | 96 | } |
| 97 | + |
| 98 | +func TestValidGetExecUser(t *testing.T) { |
| 99 | + const passwdContent = ` |
| 100 | +root:x:0:0:root user:/root:/bin/bash |
| 101 | +adm:x:42:43:adm:/var/adm:/bin/false |
| 102 | +this is just some garbage data |
| 103 | +` |
| 104 | + const groupContent = ` |
| 105 | +root:x:0:root |
| 106 | +adm:x:43: |
| 107 | +grp:x:1234:root,adm |
| 108 | +this is just some garbage data |
| 109 | +` |
| 110 | + defaultExecUser := ExecUser{ |
| 111 | + Uid: 8888, |
| 112 | + Gid: 8888, |
| 113 | + Sgids: []int{8888}, |
| 114 | + Home: "/8888", |
| 115 | + } |
| 116 | + |
| 117 | + tests := []struct { |
| 118 | + ref string |
| 119 | + expected ExecUser |
| 120 | + }{ |
| 121 | + { |
| 122 | + ref: "root", |
| 123 | + expected: ExecUser{ |
| 124 | + Uid: 0, |
| 125 | + Gid: 0, |
| 126 | + Sgids: []int{0, 1234}, |
| 127 | + Home: "/root", |
| 128 | + }, |
| 129 | + }, |
| 130 | + { |
| 131 | + ref: "adm", |
| 132 | + expected: ExecUser{ |
| 133 | + Uid: 42, |
| 134 | + Gid: 43, |
| 135 | + Sgids: []int{1234}, |
| 136 | + Home: "/var/adm", |
| 137 | + }, |
| 138 | + }, |
| 139 | + { |
| 140 | + ref: "root:adm", |
| 141 | + expected: ExecUser{ |
| 142 | + Uid: 0, |
| 143 | + Gid: 43, |
| 144 | + Sgids: defaultExecUser.Sgids, |
| 145 | + Home: "/root", |
| 146 | + }, |
| 147 | + }, |
| 148 | + { |
| 149 | + ref: "adm:1234", |
| 150 | + expected: ExecUser{ |
| 151 | + Uid: 42, |
| 152 | + Gid: 1234, |
| 153 | + Sgids: defaultExecUser.Sgids, |
| 154 | + Home: "/var/adm", |
| 155 | + }, |
| 156 | + }, |
| 157 | + { |
| 158 | + ref: "42:1234", |
| 159 | + expected: ExecUser{ |
| 160 | + Uid: 42, |
| 161 | + Gid: 1234, |
| 162 | + Sgids: defaultExecUser.Sgids, |
| 163 | + Home: "/var/adm", |
| 164 | + }, |
| 165 | + }, |
| 166 | + { |
| 167 | + ref: "1337:1234", |
| 168 | + expected: ExecUser{ |
| 169 | + Uid: 1337, |
| 170 | + Gid: 1234, |
| 171 | + Sgids: defaultExecUser.Sgids, |
| 172 | + Home: defaultExecUser.Home, |
| 173 | + }, |
| 174 | + }, |
| 175 | + { |
| 176 | + ref: "1337", |
| 177 | + expected: ExecUser{ |
| 178 | + Uid: 1337, |
| 179 | + Gid: defaultExecUser.Gid, |
| 180 | + Sgids: defaultExecUser.Sgids, |
| 181 | + Home: defaultExecUser.Home, |
| 182 | + }, |
| 183 | + }, |
| 184 | + { |
| 185 | + ref: "", |
| 186 | + expected: ExecUser{ |
| 187 | + Uid: defaultExecUser.Uid, |
| 188 | + Gid: defaultExecUser.Gid, |
| 189 | + Sgids: defaultExecUser.Sgids, |
| 190 | + Home: defaultExecUser.Home, |
| 191 | + }, |
| 192 | + }, |
| 193 | + } |
| 194 | + |
| 195 | + for _, test := range tests { |
| 196 | + passwd := strings.NewReader(passwdContent) |
| 197 | + group := strings.NewReader(groupContent) |
| 198 | + |
| 199 | + execUser, err := GetExecUser(test.ref, &defaultExecUser, passwd, group) |
| 200 | + if err != nil { |
| 201 | + t.Logf("got unexpected error when parsing '%s': %s", test.ref, err.Error()) |
| 202 | + t.Fail() |
| 203 | + continue |
| 204 | + } |
| 205 | + |
| 206 | + if !reflect.DeepEqual(test.expected, *execUser) { |
| 207 | + t.Logf("got: %#v", execUser) |
| 208 | + t.Logf("expected: %#v", test.expected) |
| 209 | + t.Fail() |
| 210 | + continue |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +func TestInvalidGetExecUser(t *testing.T) { |
| 216 | + const passwdContent = ` |
| 217 | +root:x:0:0:root user:/root:/bin/bash |
| 218 | +adm:x:42:43:adm:/var/adm:/bin/false |
| 219 | +this is just some garbage data |
| 220 | +` |
| 221 | + const groupContent = ` |
| 222 | +root:x:0:root |
| 223 | +adm:x:43: |
| 224 | +grp:x:1234:root,adm |
| 225 | +this is just some garbage data |
| 226 | +` |
| 227 | + |
| 228 | + tests := []string{ |
| 229 | + // No such user/group. |
| 230 | + "notuser", |
| 231 | + "notuser:notgroup", |
| 232 | + "root:notgroup", |
| 233 | + "notuser:adm", |
| 234 | + "8888:notgroup", |
| 235 | + "notuser:8888", |
| 236 | + |
| 237 | + // Invalid user/group values. |
| 238 | + "-1:0", |
| 239 | + "0:-3", |
| 240 | + "-5:-2", |
| 241 | + } |
| 242 | + |
| 243 | + for _, test := range tests { |
| 244 | + passwd := strings.NewReader(passwdContent) |
| 245 | + group := strings.NewReader(groupContent) |
| 246 | + |
| 247 | + execUser, err := GetExecUser(test, nil, passwd, group) |
| 248 | + if err == nil { |
| 249 | + t.Logf("got unexpected success when parsing '%s': %#v", test, execUser) |
| 250 | + t.Fail() |
| 251 | + continue |
| 252 | + } |
| 253 | + } |
| 254 | +} |
| 255 | + |
| 256 | +func TestGetExecUserNilSources(t *testing.T) { |
| 257 | + const passwdContent = ` |
| 258 | +root:x:0:0:root user:/root:/bin/bash |
| 259 | +adm:x:42:43:adm:/var/adm:/bin/false |
| 260 | +this is just some garbage data |
| 261 | +` |
| 262 | + const groupContent = ` |
| 263 | +root:x:0:root |
| 264 | +adm:x:43: |
| 265 | +grp:x:1234:root,adm |
| 266 | +this is just some garbage data |
| 267 | +` |
| 268 | + |
| 269 | + defaultExecUser := ExecUser{ |
| 270 | + Uid: 8888, |
| 271 | + Gid: 8888, |
| 272 | + Sgids: []int{8888}, |
| 273 | + Home: "/8888", |
| 274 | + } |
| 275 | + |
| 276 | + tests := []struct { |
| 277 | + ref string |
| 278 | + passwd, group bool |
| 279 | + expected ExecUser |
| 280 | + }{ |
| 281 | + { |
| 282 | + ref: "", |
| 283 | + passwd: false, |
| 284 | + group: false, |
| 285 | + expected: ExecUser{ |
| 286 | + Uid: 8888, |
| 287 | + Gid: 8888, |
| 288 | + Sgids: []int{8888}, |
| 289 | + Home: "/8888", |
| 290 | + }, |
| 291 | + }, |
| 292 | + { |
| 293 | + ref: "root", |
| 294 | + passwd: true, |
| 295 | + group: false, |
| 296 | + expected: ExecUser{ |
| 297 | + Uid: 0, |
| 298 | + Gid: 0, |
| 299 | + Sgids: []int{8888}, |
| 300 | + Home: "/root", |
| 301 | + }, |
| 302 | + }, |
| 303 | + { |
| 304 | + ref: "0", |
| 305 | + passwd: false, |
| 306 | + group: false, |
| 307 | + expected: ExecUser{ |
| 308 | + Uid: 0, |
| 309 | + Gid: 8888, |
| 310 | + Sgids: []int{8888}, |
| 311 | + Home: "/8888", |
| 312 | + }, |
| 313 | + }, |
| 314 | + { |
| 315 | + ref: "0:0", |
| 316 | + passwd: false, |
| 317 | + group: false, |
| 318 | + expected: ExecUser{ |
| 319 | + Uid: 0, |
| 320 | + Gid: 0, |
| 321 | + Sgids: []int{8888}, |
| 322 | + Home: "/8888", |
| 323 | + }, |
| 324 | + }, |
| 325 | + } |
| 326 | + |
| 327 | + for _, test := range tests { |
| 328 | + var passwd, group io.Reader |
| 329 | + |
| 330 | + if test.passwd { |
| 331 | + passwd = strings.NewReader(passwdContent) |
| 332 | + } |
| 333 | + |
| 334 | + if test.group { |
| 335 | + group = strings.NewReader(groupContent) |
| 336 | + } |
| 337 | + |
| 338 | + execUser, err := GetExecUser(test.ref, &defaultExecUser, passwd, group) |
| 339 | + if err != nil { |
| 340 | + t.Logf("got unexpected error when parsing '%s': %s", test.ref, err.Error()) |
| 341 | + t.Fail() |
| 342 | + continue |
| 343 | + } |
| 344 | + |
| 345 | + if !reflect.DeepEqual(test.expected, *execUser) { |
| 346 | + t.Logf("got: %#v", execUser) |
| 347 | + t.Logf("expected: %#v", test.expected) |
| 348 | + t.Fail() |
| 349 | + continue |
| 350 | + } |
| 351 | + } |
| 352 | +} |
0 commit comments