summaryrefslogtreecommitdiff
path: root/snap
diff options
authorPawel Stolowski <stolowski@gmail.com>2019-08-02 13:56:16 +0200
committerPawel Stolowski <stolowski@gmail.com>2019-08-02 13:56:16 +0200
commitee76069e04656b88e2c78638a3dc2fb278db8d22 (patch)
treefbde1750eaa4618d7f63a35ea8ad0654faf091c1 /snap
parent381b45e27773eff87bca4f8ed9b96d027bfb8ee5 (diff)
Prevent duplicated snap name and snap files when parsing seed.yaml.
Diffstat (limited to 'snap')
-rw-r--r--snap/seed_yaml.go12
-rw-r--r--snap/seed_yaml_test.go34
2 files changed, 46 insertions, 0 deletions
diff --git a/snap/seed_yaml.go b/snap/seed_yaml.go
index 60f1724573..1a591cec47 100644
--- a/snap/seed_yaml.go
+++ b/snap/seed_yaml.go
@@ -70,6 +70,8 @@ func ReadSeedYaml(fn string) (*Seed, error) {
return nil, fmt.Errorf("%s: cannot unmarshal %q: %s", errPrefix, yamlData, err)
}
+ seenNames := make(map[string]bool, len(seed.Snaps))
+ seenFiles := make(map[string]bool, len(seed.Snaps))
// validate
for _, sn := range seed.Snaps {
if sn == nil {
@@ -89,6 +91,16 @@ func ReadSeedYaml(fn string) (*Seed, error) {
if strings.Contains(sn.File, "/") {
return nil, fmt.Errorf("%s: %q must be a filename, not a path", errPrefix, sn.File)
}
+
+ // make sure names and file names are unique
+ if seenNames[sn.Name] {
+ return nil, fmt.Errorf("%s: snap name %q not unique", errPrefix, sn.Name)
+ }
+ seenNames[sn.Name] = true
+ if seenFiles[sn.File] {
+ return nil, fmt.Errorf("%s: snap file %q for snap %q not unique", errPrefix, sn.File, sn.Name)
+ }
+ seenFiles[sn.File] = true
}
return &seed, nil
diff --git a/snap/seed_yaml_test.go b/snap/seed_yaml_test.go
index b067663357..670bafe5ca 100644
--- a/snap/seed_yaml_test.go
+++ b/snap/seed_yaml_test.go
@@ -82,6 +82,40 @@ func (s *seedYamlTestSuite) TestNoPathAllowed(c *C) {
c.Assert(err, ErrorMatches, `cannot read seed yaml: "foo/bar.snap" must be a filename, not a path`)
}
+func (s *seedYamlTestSuite) TestDuplicatedSnapName(c *C) {
+ fn := filepath.Join(c.MkDir(), "seed.yaml")
+ err := ioutil.WriteFile(fn, []byte(`
+snaps:
+ - name: foo
+ channel: stable
+ file: foo_1.0_all.snap
+ - name: foo
+ channel: edge
+ file: bar_1.0_all.snap
+`), 0644)
+ c.Assert(err, IsNil)
+
+ _, err = snap.ReadSeedYaml(fn)
+ c.Assert(err, ErrorMatches, `cannot read seed yaml: snap name "foo" not unique`)
+}
+
+func (s *seedYamlTestSuite) TestDuplicatedSnapFile(c *C) {
+ fn := filepath.Join(c.MkDir(), "seed.yaml")
+ err := ioutil.WriteFile(fn, []byte(`
+snaps:
+ - name: foo
+ channel: stable
+ file: foo_1.0_all.snap
+ - name: bar
+ channel: edge
+ file: foo_1.0_all.snap
+`), 0644)
+ c.Assert(err, IsNil)
+
+ _, err = snap.ReadSeedYaml(fn)
+ c.Assert(err, ErrorMatches, `cannot read seed yaml: snap file "foo_1.0_all.snap" for snap "bar" not unique`)
+}
+
func (s *seedYamlTestSuite) TestValidateChannelUnhappy(c *C) {
fn := filepath.Join(c.MkDir(), "seed.yaml")
err := ioutil.WriteFile(fn, []byte(`