| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
|
| 6 |
. "gopkg.in/check.v1" |
| 7 |
) |
| 8 |
|
| 9 |
type ModulesSuite struct{} |
| 10 |
|
| 11 |
var _ = Suite(&ModulesSuite{}) |
| 12 |
|
| 13 |
func (s *ModulesSuite) TestValidateMissingURL(c *C) { |
| 14 |
m := &Submodule{Name: "foo", Path: "foo"} |
| 15 |
c.Assert(m.Validate(), Equals, ErrModuleEmptyURL) |
| 16 |
} |
| 17 |
|
| 18 |
func (s *ModulesSuite) TestValidateBadPath(c *C) { |
| 19 |
input := []string{ |
| 20 |
`..`, |
| 21 |
`../`, |
| 22 |
`../bar`, |
| 23 |
|
| 24 |
`/..`, |
| 25 |
`/../bar`, |
| 26 |
|
| 27 |
`foo/..`, |
| 28 |
`foo/../`, |
| 29 |
`foo/../bar`, |
| 30 |
} |
| 31 |
|
| 32 |
for _, p := range input { |
| 33 |
m := &Submodule{ |
| 34 |
Name: "ok", |
| 35 |
Path: p, |
| 36 |
URL: "https://example.com/", |
| 37 |
} |
| 38 |
c.Assert(m.Validate(), Equals, ErrModuleBadPath) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func (s *ModulesSuite) TestValidateMissingName(c *C) { |
| 43 |
m := &Submodule{Name: "ok", URL: "bar"} |
| 44 |
c.Assert(m.Validate(), Equals, ErrModuleEmptyPath) |
| 45 |
} |
| 46 |
|
| 47 |
func (s *ModulesSuite) TestValidateBadName(c *C) { |
| 48 |
input := []string{ |
| 49 |
// Plain shapes the parser must reject regardless of OS. |
| 50 |
"", |
| 51 |
".", |
| 52 |
"..", |
| 53 |
"../x", |
| 54 |
"a/../../b", |
| 55 |
"/abs", |
| 56 |
`C:\win`, |
| 57 |
"x\x00y", |
| 58 |
"x/", |
| 59 |
"/x", |
| 60 |
`.\..\foo`, |
| 61 |
"modules/../escape", |
| 62 |
|
| 63 |
// HFS+ ignores certain Unicode code points during path |
| 64 |
// normalisation, so these all resolve to ".." on macOS. |
| 65 |
".\u200c.", // ZWNJ between dots |
| 66 |
"\u200c..", // leading ZWNJ |
| 67 |
"..\u200c", // trailing ZWNJ |
| 68 |
"\u200c.\u200d.\u200e", // ZWNJ + ZWJ + LRM |
| 69 |
"a/.\u200c./b", // hidden ".." mid-path |
| 70 |
|
| 71 |
// NTFS strips trailing spaces, dots, and an alternate-data |
| 72 |
// -stream suffix during canonicalisation, so these all |
| 73 |
// resolve to ".." on Windows. |
| 74 |
".. ", |
| 75 |
".. ", |
| 76 |
"....", |
| 77 |
".. .", |
| 78 |
"..::$INDEX_ALLOCATION", |
| 79 |
"..:foo", |
| 80 |
"a/.. /b", |
| 81 |
} |
| 82 |
for _, n := range input { |
| 83 |
m := &Submodule{ |
| 84 |
Name: n, |
| 85 |
Path: "ok", |
| 86 |
URL: "https://example.com/", |
| 87 |
} |
| 88 |
// Validate wraps the sentinel with the offending name |
| 89 |
// (canonical-Git wording: "ignoring suspicious submodule |
| 90 |
// name: <name>"), so use errors.Is. |
| 91 |
c.Assert(errors.Is(m.Validate(), ErrModuleBadName), Equals, true, |
| 92 |
Commentf("name %q", n)) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func (s *ModulesSuite) TestValidateGoodName(c *C) { |
| 97 |
for _, n := range []string{"foo", "lib-foo", "deps/x", "x.y"} { |
| 98 |
m := &Submodule{ |
| 99 |
Name: n, |
| 100 |
Path: "ok", |
| 101 |
URL: "https://example.com/", |
| 102 |
} |
| 103 |
c.Assert(m.Validate(), IsNil, Commentf("name %q", n)) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func (s *ModulesSuite) TestMarshal(c *C) { |
| 108 |
input := []byte(`[submodule "qux"] |
| 109 |
path = qux |
| 110 |
url = baz |
| 111 |
branch = bar |
| 112 |
`) |
| 113 |
|
| 114 |
cfg := NewModules() |
| 115 |
cfg.Submodules["qux"] = &Submodule{Path: "qux", URL: "baz", Branch: "bar"} |
| 116 |
|
| 117 |
output, err := cfg.Marshal() |
| 118 |
c.Assert(err, IsNil) |
| 119 |
c.Assert(output, DeepEquals, input) |
| 120 |
} |
| 121 |
|
| 122 |
func (s *ModulesSuite) TestUnmarshal(c *C) { |
| 123 |
input := []byte(`[submodule "qux"] |
| 124 |
path = qux |
| 125 |
url = https://github.com/foo/qux.git |
| 126 |
[submodule "foo/bar"] |
| 127 |
path = foo/bar |
| 128 |
url = https://github.com/foo/bar.git |
| 129 |
branch = dev |
| 130 |
[submodule "suspicious"] |
| 131 |
path = ../../foo/bar |
| 132 |
url = https://github.com/foo/bar.git |
| 133 |
[submodule ".."] |
| 134 |
path = deps/x |
| 135 |
url = https://github.com/foo/bar.git |
| 136 |
`) |
| 137 |
|
| 138 |
cfg := NewModules() |
| 139 |
err := cfg.Unmarshal(input) |
| 140 |
c.Assert(err, IsNil) |
| 141 |
|
| 142 |
// The "suspicious" entry is dropped because of its `..` path, |
| 143 |
// and the `..` entry is dropped because of its suspicious name |
| 144 |
// (canonical Git's "ignoring suspicious submodule name" rule). |
| 145 |
c.Assert(cfg.Submodules, HasLen, 2) |
| 146 |
c.Assert(cfg.Submodules["qux"].Name, Equals, "qux") |
| 147 |
c.Assert(cfg.Submodules["qux"].URL, Equals, "https://github.com/foo/qux.git") |
| 148 |
c.Assert(cfg.Submodules["foo/bar"].Name, Equals, "foo/bar") |
| 149 |
c.Assert(cfg.Submodules["foo/bar"].URL, Equals, "https://github.com/foo/bar.git") |
| 150 |
c.Assert(cfg.Submodules["foo/bar"].Branch, Equals, "dev") |
| 151 |
_, hasDotDot := cfg.Submodules[".."] |
| 152 |
c.Assert(hasDotDot, Equals, false) |
| 153 |
} |
| 154 |
|
| 155 |
func (s *ModulesSuite) TestUnmarshalMarshal(c *C) { |
| 156 |
input := []byte(`[submodule "foo/bar"] |
| 157 |
path = foo/bar |
| 158 |
url = https://github.com/foo/bar.git |
| 159 |
ignore = all |
| 160 |
`) |
| 161 |
|
| 162 |
cfg := NewModules() |
| 163 |
err := cfg.Unmarshal(input) |
| 164 |
c.Assert(err, IsNil) |
| 165 |
|
| 166 |
output, err := cfg.Marshal() |
| 167 |
c.Assert(err, IsNil) |
| 168 |
c.Assert(string(output), DeepEquals, string(input)) |
| 169 |
} |