2015-02-11 01:32:22 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
2015-07-10 18:00:06 +00:00
|
|
|
"strconv"
|
2015-03-05 15:11:37 +00:00
|
|
|
"strings"
|
2015-02-11 01:32:22 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-07-23 01:16:20 +00:00
|
|
|
var (
|
|
|
|
// regexpTestcases is a unified set of testcases for
|
|
|
|
// TestValidateRepositoryName and TestRepositoryNameRegexp.
|
|
|
|
// Some of them are valid inputs for one and not the other.
|
|
|
|
regexpTestcases = []struct {
|
|
|
|
// input is the repository name or name component testcase
|
2015-02-11 01:32:22 +00:00
|
|
|
input string
|
2015-07-23 01:16:20 +00:00
|
|
|
// err is the error expected from ValidateRepositoryName, or nil
|
|
|
|
err error
|
|
|
|
// invalid should be true if the testcase is *not* expected to
|
|
|
|
// match RepositoryNameRegexp
|
|
|
|
invalid bool
|
2015-02-11 01:32:22 +00:00
|
|
|
}{
|
2015-07-10 18:00:06 +00:00
|
|
|
{
|
|
|
|
input: "",
|
|
|
|
err: ErrRepositoryNameEmpty,
|
|
|
|
},
|
2015-07-22 00:10:36 +00:00
|
|
|
{
|
|
|
|
input: "short",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "simple/name",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "library/ubuntu",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "docker/stevvooe/app",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "aa/aa/aa/aa/aa/aa/aa/aa/aa/bb/bb/bb/bb/bb/bb",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "aa/aa/bb/bb/bb",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a/a/a/b/b",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a/a/a/a/",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a//a/a",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a/aa",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "aa/a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a/aa/a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo.com/",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
2015-07-23 01:16:20 +00:00
|
|
|
// TODO: this testcase should be valid once we switch to
|
|
|
|
// the reference package.
|
2015-07-22 00:10:36 +00:00
|
|
|
input: "foo.com:8080/bar",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo.com/bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo.com/bar/baz",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo.com/bar/baz/quux",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "blog.foo.com/bar/baz",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "asdf",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "asdf$$^/aa",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "aa-a/aa",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "aa/aa",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a-a/a-a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a-/a/a/a",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
2015-07-23 01:16:20 +00:00
|
|
|
{
|
|
|
|
input: strings.Repeat("a", 255),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: strings.Repeat("a", 256),
|
|
|
|
err: ErrRepositoryNameLong,
|
|
|
|
},
|
2015-07-22 00:10:36 +00:00
|
|
|
{
|
|
|
|
input: "-foo/bar",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo/bar-",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo-/bar",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo/-bar",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "_foo/bar",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo/bar_",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "____/____",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "_docker/_docker",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "docker_/docker_",
|
2015-07-23 01:16:20 +00:00
|
|
|
err: ErrRepositoryNameComponentInvalid,
|
2015-07-22 00:10:36 +00:00
|
|
|
invalid: true,
|
|
|
|
},
|
2015-07-23 01:16:20 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestValidateRepositoryName tests the ValidateRepositoryName function,
|
|
|
|
// which uses RepositoryNameComponentAnchoredRegexp for validation
|
|
|
|
func TestValidateRepositoryName(t *testing.T) {
|
|
|
|
for _, testcase := range regexpTestcases {
|
|
|
|
failf := func(format string, v ...interface{}) {
|
|
|
|
t.Logf(strconv.Quote(testcase.input)+": "+format, v...)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ValidateRepositoryName(testcase.input); err != testcase.err {
|
|
|
|
if testcase.err != nil {
|
|
|
|
if err != nil {
|
|
|
|
failf("unexpected error for invalid repository: got %v, expected %v", err, testcase.err)
|
|
|
|
} else {
|
|
|
|
failf("expected invalid repository: %v", testcase.err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
// Wrong error returned.
|
|
|
|
failf("unexpected error validating repository name: %v, expected %v", err, testcase.err)
|
|
|
|
} else {
|
|
|
|
failf("unexpected error validating repository name: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepositoryNameRegexp(t *testing.T) {
|
|
|
|
for _, testcase := range regexpTestcases {
|
2015-07-22 00:10:36 +00:00
|
|
|
failf := func(format string, v ...interface{}) {
|
|
|
|
t.Logf(strconv.Quote(testcase.input)+": "+format, v...)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := RepositoryNameRegexp.FindString(testcase.input) == testcase.input
|
|
|
|
if matches == testcase.invalid {
|
|
|
|
if testcase.invalid {
|
|
|
|
failf("expected invalid repository name %s", testcase.input)
|
|
|
|
} else {
|
|
|
|
failf("expected valid repository name %s", testcase.input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|