|
| 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 | + |
| 17 | +package oci |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + specs "github.com/opencontainers/runtime-spec/specs-go" |
| 24 | +) |
| 25 | + |
| 26 | +func TestAddCaps(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + var s specs.Spec |
| 30 | + |
| 31 | + if err := WithAddedCapabilities([]string{"CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + for i, cl := range [][]string{ |
| 35 | + s.Process.Capabilities.Bounding, |
| 36 | + s.Process.Capabilities.Effective, |
| 37 | + s.Process.Capabilities.Permitted, |
| 38 | + s.Process.Capabilities.Inheritable, |
| 39 | + } { |
| 40 | + if !capsContain(cl, "CAP_CHOWN") { |
| 41 | + t.Errorf("cap list %d does not contain added cap", i) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestDropCaps(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + |
| 49 | + var s specs.Spec |
| 50 | + |
| 51 | + if err := WithAllKnownCapabilities(context.Background(), nil, nil, &s); err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + if err := WithDroppedCapabilities([]string{"CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | + |
| 58 | + for i, cl := range [][]string{ |
| 59 | + s.Process.Capabilities.Bounding, |
| 60 | + s.Process.Capabilities.Effective, |
| 61 | + s.Process.Capabilities.Permitted, |
| 62 | + s.Process.Capabilities.Inheritable, |
| 63 | + } { |
| 64 | + if capsContain(cl, "CAP_CHOWN") { |
| 65 | + t.Errorf("cap list %d contains dropped cap", i) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Add all capabilities back and drop a different cap. |
| 70 | + if err := WithAllKnownCapabilities(context.Background(), nil, nil, &s); err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + if err := WithDroppedCapabilities([]string{"CAP_FOWNER"})(context.Background(), nil, nil, &s); err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + |
| 77 | + for i, cl := range [][]string{ |
| 78 | + s.Process.Capabilities.Bounding, |
| 79 | + s.Process.Capabilities.Effective, |
| 80 | + s.Process.Capabilities.Permitted, |
| 81 | + s.Process.Capabilities.Inheritable, |
| 82 | + } { |
| 83 | + if capsContain(cl, "CAP_FOWNER") { |
| 84 | + t.Errorf("cap list %d contains dropped cap", i) |
| 85 | + } |
| 86 | + if !capsContain(cl, "CAP_CHOWN") { |
| 87 | + t.Errorf("cap list %d doesn't contain non-dropped cap", i) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Drop all duplicated caps. |
| 92 | + if err := WithCapabilities([]string{"CAP_CHOWN", "CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil { |
| 93 | + t.Fatal(err) |
| 94 | + } |
| 95 | + if err := WithDroppedCapabilities([]string{"CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil { |
| 96 | + t.Fatal(err) |
| 97 | + } |
| 98 | + for i, cl := range [][]string{ |
| 99 | + s.Process.Capabilities.Bounding, |
| 100 | + s.Process.Capabilities.Effective, |
| 101 | + s.Process.Capabilities.Permitted, |
| 102 | + s.Process.Capabilities.Inheritable, |
| 103 | + } { |
| 104 | + if len(cl) != 0 { |
| 105 | + t.Errorf("cap list %d is not empty", i) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments