|
| 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 filesystem_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "runtime" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "gotest.tools/v3/assert" |
| 25 | + |
| 26 | + "github.com/containerd/nerdctl/v2/pkg/internal/filesystem" |
| 27 | +) |
| 28 | + |
| 29 | +func TestFilesystemRestrictions(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + |
| 32 | + invalid := []string{ |
| 33 | + "/", |
| 34 | + "/start", |
| 35 | + "mid/dle", |
| 36 | + "end/", |
| 37 | + ".", |
| 38 | + "..", |
| 39 | + "", |
| 40 | + fmt.Sprintf("A%0255s", "A"), |
| 41 | + } |
| 42 | + |
| 43 | + valid := []string{ |
| 44 | + fmt.Sprintf("A%0254s", "A"), |
| 45 | + "test", |
| 46 | + "test-hyphen", |
| 47 | + ".start.dot", |
| 48 | + "mid.dot", |
| 49 | + "∞", |
| 50 | + } |
| 51 | + |
| 52 | + if runtime.GOOS == "windows" { |
| 53 | + invalid = append(invalid, []string{ |
| 54 | + "\\start", |
| 55 | + "mid\\dle", |
| 56 | + "end\\", |
| 57 | + "\\", |
| 58 | + "\\.", |
| 59 | + "com².whatever", |
| 60 | + "lpT2", |
| 61 | + "Prn.", |
| 62 | + "nUl", |
| 63 | + "AUX", |
| 64 | + "A<A", |
| 65 | + "A>A", |
| 66 | + "A:A", |
| 67 | + "A\"A", |
| 68 | + "A|A", |
| 69 | + "A?A", |
| 70 | + "A*A", |
| 71 | + "end.dot.", |
| 72 | + "end.space ", |
| 73 | + }...) |
| 74 | + } |
| 75 | + |
| 76 | + for _, v := range invalid { |
| 77 | + err := filesystem.ValidatePathComponent(v) |
| 78 | + assert.ErrorIs(t, err, filesystem.ErrInvalidPath, v) |
| 79 | + } |
| 80 | + |
| 81 | + for _, v := range valid { |
| 82 | + err := filesystem.ValidatePathComponent(v) |
| 83 | + assert.NilError(t, err, v) |
| 84 | + } |
| 85 | +} |
0 commit comments