From 50c126ffcea3ecb1d52ed510ce57b4e30e8a6fee Mon Sep 17 00:00:00 2001 From: Christophe de Dinechin Date: Fri, 11 Sep 2020 12:59:53 +0200 Subject: [PATCH] annotations: Add unit test for checkPathIsInGlobs There are a few interesting corner cases to consider for this function. Fixes: #3004 Suggested-by: James O.D. Hunt Signed-off-by: Christophe de Dinechin --- virtcontainers/pkg/oci/utils_test.go | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/virtcontainers/pkg/oci/utils_test.go b/virtcontainers/pkg/oci/utils_test.go index 9ebef36e96..d42d9a69a4 100644 --- a/virtcontainers/pkg/oci/utils_test.go +++ b/virtcontainers/pkg/oci/utils_test.go @@ -944,3 +944,34 @@ func TestRegexpContains(t *testing.T) { assert.Equal(d.expected, matched, "%+v", d) } } + +func TestCheckPathIsInGlobs(t *testing.T) { + assert := assert.New(t) + + type testData struct { + globs []string + toMatch string + expected bool + } + + data := []testData{ + {[]string{}, "", false}, + {[]string{}, "nonempty", false}, + {[]string{"simple"}, "simple", false}, + {[]string{"simple"}, "some_simple_text", false}, + {[]string{"/bin/ls"}, "/bin/ls", true}, + {[]string{"/bin/ls", "/bin/false"}, "/bin/ls", true}, + {[]string{"/bin/ls", "/bin/false"}, "/bin/false", true}, + {[]string{"/bin/ls", "/bin/false"}, "/bin/bar", false}, + {[]string{"/bin/*ls*"}, "/bin/ls", true}, + {[]string{"/bin/*ls*"}, "/bin/false", true}, + {[]string{"bin/ls"}, "/bin/ls", false}, + {[]string{"./bin/ls"}, "/bin/ls", false}, + {[]string{"*/bin/ls"}, "/bin/ls", false}, + } + + for _, d := range data { + matched := checkPathIsInGlobs(d.globs, d.toMatch) + assert.Equal(d.expected, matched, "%+v", d) + } +}