github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/snap/naming/tag_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2020 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package naming_test 21 22 import ( 23 . "gopkg.in/check.v1" 24 25 "github.com/snapcore/snapd/snap/naming" 26 ) 27 28 type tagSuite struct{} 29 30 var _ = Suite(&tagSuite{}) 31 32 func (s *tagSuite) TestParseSecurityTag(c *C) { 33 // valid snap names, snap instances, app names and hook names are accepted. 34 tag, err := naming.ParseSecurityTag("snap.pkg.app") 35 c.Assert(err, IsNil) 36 c.Check(tag.String(), Equals, "snap.pkg.app") 37 c.Check(tag.InstanceName(), Equals, "pkg") 38 c.Check(tag.(naming.AppSecurityTag).AppName(), Equals, "app") 39 40 tag, err = naming.ParseSecurityTag("snap.pkg_key.app") 41 c.Assert(err, IsNil) 42 c.Check(tag.String(), Equals, "snap.pkg_key.app") 43 c.Check(tag.InstanceName(), Equals, "pkg_key") 44 c.Check(tag.(naming.AppSecurityTag).AppName(), Equals, "app") 45 46 tag, err = naming.ParseSecurityTag("snap.pkg.hook.configure") 47 c.Assert(err, IsNil) 48 c.Check(tag.String(), Equals, "snap.pkg.hook.configure") 49 c.Check(tag.InstanceName(), Equals, "pkg") 50 c.Check(tag.(naming.HookSecurityTag).HookName(), Equals, "configure") 51 52 tag, err = naming.ParseSecurityTag("snap.pkg_key.hook.configure") 53 c.Assert(err, IsNil) 54 c.Check(tag.String(), Equals, "snap.pkg_key.hook.configure") 55 c.Check(tag.InstanceName(), Equals, "pkg_key") 56 c.Check(tag.(naming.HookSecurityTag).HookName(), Equals, "configure") 57 58 // invalid format is rejected 59 _, err = naming.ParseSecurityTag("snap.pkg.app.surprise") 60 c.Check(err, ErrorMatches, "invalid security tag") 61 _, err = naming.ParseSecurityTag("snap.pkg_key.app.surprise") 62 c.Check(err, ErrorMatches, "invalid security tag") 63 64 // invalid snap and app names are rejected. 65 _, err = naming.ParseSecurityTag("snap._.app") 66 c.Check(err, ErrorMatches, "invalid security tag") 67 _, err = naming.ParseSecurityTag("snap.pkg._") 68 c.Check(err, ErrorMatches, "invalid security tag") 69 70 // invalid number of components are rejected. 71 _, err = naming.ParseSecurityTag("snap.pkg.hook.surprise.") 72 c.Check(err, ErrorMatches, "invalid security tag") 73 _, err = naming.ParseSecurityTag("snap.pkg.hook.") 74 c.Check(err, ErrorMatches, "invalid security tag") 75 tag, err = naming.ParseSecurityTag("snap.pkg.hook") 76 c.Assert(err, IsNil) // Perhaps somewhat unexpectedly, this tag is valid. 77 c.Check(tag.(naming.AppSecurityTag).AppName(), Equals, "hook") 78 _, err = naming.ParseSecurityTag("snap.pkg.app.surprise") 79 c.Check(err, ErrorMatches, "invalid security tag") 80 _, err = naming.ParseSecurityTag("snap.pkg.") 81 c.Check(err, ErrorMatches, "invalid security tag") 82 _, err = naming.ParseSecurityTag("snap.pkg") 83 c.Check(err, ErrorMatches, "invalid security tag") 84 _, err = naming.ParseSecurityTag("snap.") 85 c.Check(err, ErrorMatches, "invalid security tag") 86 _, err = naming.ParseSecurityTag("snap") 87 c.Check(err, ErrorMatches, "invalid security tag") 88 89 // things that are not snap.* tags 90 _, err = naming.ParseSecurityTag("foo.bar.froz") 91 c.Check(err, ErrorMatches, "invalid security tag") 92 } 93 94 func (s *tagSuite) TestParseAppSecurityTag(c *C) { 95 // Invalid security tags cannot be parsed. 96 tag, err := naming.ParseAppSecurityTag("potato") 97 c.Assert(err, ErrorMatches, "invalid security tag") 98 c.Assert(tag, IsNil) 99 100 // App security tags can be parsed. 101 tag, err = naming.ParseAppSecurityTag("snap.pkg.app") 102 c.Assert(err, IsNil) 103 c.Check(tag.String(), Equals, "snap.pkg.app") 104 c.Check(tag.InstanceName(), Equals, "pkg") 105 c.Check(tag.AppName(), Equals, "app") 106 107 // Hook security tags are not app security tags. 108 tag, err = naming.ParseAppSecurityTag("snap.pkg.hook.configure") 109 c.Assert(err, ErrorMatches, `"snap.pkg.hook.configure" is not an app security tag`) 110 c.Assert(tag, IsNil) 111 } 112 113 func (s *tagSuite) TestParseHookSecurityTag(c *C) { 114 // Invalid security tags cannot be parsed. 115 tag, err := naming.ParseHookSecurityTag("potato") 116 c.Assert(err, ErrorMatches, "invalid security tag") 117 c.Assert(tag, IsNil) 118 119 // Hook security tags can be parsed. 120 tag, err = naming.ParseHookSecurityTag("snap.pkg.hook.configure") 121 c.Assert(err, IsNil) 122 c.Check(tag.String(), Equals, "snap.pkg.hook.configure") 123 c.Check(tag.InstanceName(), Equals, "pkg") 124 c.Check(tag.HookName(), Equals, "configure") 125 126 // App security tags are not hook security tags. 127 tag, err = naming.ParseHookSecurityTag("snap.pkg.app") 128 c.Assert(err, ErrorMatches, `"snap.pkg.app" is not a hook security tag`) 129 c.Assert(tag, IsNil) 130 }