github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/snap/types_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2015 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 snap 21 22 import ( 23 "encoding/json" 24 "fmt" 25 "gopkg.in/yaml.v2" 26 27 . "gopkg.in/check.v1" 28 ) 29 30 type typeSuite struct{} 31 32 var _ = Suite(&typeSuite{}) 33 34 func (s *typeSuite) TestJSONerr(c *C) { 35 var t Type 36 err := json.Unmarshal([]byte("false"), &t) 37 c.Assert(err, NotNil) 38 } 39 40 func (s *typeSuite) TestJsonMarshalTypes(c *C) { 41 out, err := json.Marshal(TypeApp) 42 c.Assert(err, IsNil) 43 c.Check(string(out), Equals, "\"app\"") 44 45 out, err = json.Marshal(TypeGadget) 46 c.Assert(err, IsNil) 47 c.Check(string(out), Equals, "\"gadget\"") 48 49 out, err = json.Marshal(TypeOS) 50 c.Assert(err, IsNil) 51 c.Check(string(out), Equals, "\"os\"") 52 53 out, err = json.Marshal(TypeKernel) 54 c.Assert(err, IsNil) 55 c.Check(string(out), Equals, "\"kernel\"") 56 57 out, err = json.Marshal(TypeBase) 58 c.Assert(err, IsNil) 59 c.Check(string(out), Equals, "\"base\"") 60 61 out, err = json.Marshal(TypeSnapd) 62 c.Assert(err, IsNil) 63 c.Check(string(out), Equals, "\"snapd\"") 64 } 65 66 func (s *typeSuite) TestJsonUnmarshalTypes(c *C) { 67 var st Type 68 69 err := json.Unmarshal([]byte("\"application\""), &st) 70 c.Assert(err, IsNil) 71 c.Check(st, Equals, TypeApp) 72 73 err = json.Unmarshal([]byte("\"app\""), &st) 74 c.Assert(err, IsNil) 75 c.Check(st, Equals, TypeApp) 76 77 err = json.Unmarshal([]byte("\"gadget\""), &st) 78 c.Assert(err, IsNil) 79 c.Check(st, Equals, TypeGadget) 80 81 err = json.Unmarshal([]byte("\"os\""), &st) 82 c.Assert(err, IsNil) 83 c.Check(st, Equals, TypeOS) 84 85 err = json.Unmarshal([]byte("\"kernel\""), &st) 86 c.Assert(err, IsNil) 87 c.Check(st, Equals, TypeKernel) 88 89 err = json.Unmarshal([]byte("\"base\""), &st) 90 c.Assert(err, IsNil) 91 c.Check(st, Equals, TypeBase) 92 93 err = json.Unmarshal([]byte("\"snapd\""), &st) 94 c.Assert(err, IsNil) 95 c.Check(st, Equals, TypeSnapd) 96 } 97 98 func (s *typeSuite) TestJsonUnmarshalInvalidTypes(c *C) { 99 invalidTypes := []string{"foo", "-app", "gadget_"} 100 var st Type 101 for _, invalidType := range invalidTypes { 102 err := json.Unmarshal([]byte(fmt.Sprintf("%q", invalidType)), &st) 103 c.Assert(err, NotNil, Commentf("Expected '%s' to be an invalid type", invalidType)) 104 } 105 } 106 107 func (s *typeSuite) TestYamlMarshalTypes(c *C) { 108 out, err := yaml.Marshal(TypeApp) 109 c.Assert(err, IsNil) 110 c.Check(string(out), Equals, "app\n") 111 112 out, err = yaml.Marshal(TypeGadget) 113 c.Assert(err, IsNil) 114 c.Check(string(out), Equals, "gadget\n") 115 116 out, err = yaml.Marshal(TypeOS) 117 c.Assert(err, IsNil) 118 c.Check(string(out), Equals, "os\n") 119 120 out, err = yaml.Marshal(TypeKernel) 121 c.Assert(err, IsNil) 122 c.Check(string(out), Equals, "kernel\n") 123 124 out, err = yaml.Marshal(TypeBase) 125 c.Assert(err, IsNil) 126 c.Check(string(out), Equals, "base\n") 127 } 128 129 func (s *typeSuite) TestYamlUnmarshalTypes(c *C) { 130 var st Type 131 132 err := yaml.Unmarshal([]byte("application"), &st) 133 c.Assert(err, IsNil) 134 c.Check(st, Equals, TypeApp) 135 136 err = yaml.Unmarshal([]byte("app"), &st) 137 c.Assert(err, IsNil) 138 c.Check(st, Equals, TypeApp) 139 140 err = yaml.Unmarshal([]byte("gadget"), &st) 141 c.Assert(err, IsNil) 142 c.Check(st, Equals, TypeGadget) 143 144 err = yaml.Unmarshal([]byte("os"), &st) 145 c.Assert(err, IsNil) 146 c.Check(st, Equals, TypeOS) 147 148 err = yaml.Unmarshal([]byte("kernel"), &st) 149 c.Assert(err, IsNil) 150 c.Check(st, Equals, TypeKernel) 151 152 err = yaml.Unmarshal([]byte("base"), &st) 153 c.Assert(err, IsNil) 154 c.Check(st, Equals, TypeBase) 155 } 156 157 func (s *typeSuite) TestYamlUnmarshalInvalidTypes(c *C) { 158 invalidTypes := []string{"foo", "-app", "gadget_"} 159 var st Type 160 for _, invalidType := range invalidTypes { 161 err := yaml.Unmarshal([]byte(invalidType), &st) 162 c.Assert(err, NotNil, Commentf("Expected '%s' to be an invalid type", invalidType)) 163 } 164 } 165 166 func (s *typeSuite) TestYamlMarshalConfinementTypes(c *C) { 167 out, err := yaml.Marshal(DevModeConfinement) 168 c.Assert(err, IsNil) 169 c.Check(string(out), Equals, "devmode\n") 170 171 out, err = yaml.Marshal(StrictConfinement) 172 c.Assert(err, IsNil) 173 c.Check(string(out), Equals, "strict\n") 174 } 175 176 func (s *typeSuite) TestYamlUnmarshalConfinementTypes(c *C) { 177 var confinementType ConfinementType 178 err := yaml.Unmarshal([]byte("devmode"), &confinementType) 179 c.Assert(err, IsNil) 180 c.Check(confinementType, Equals, DevModeConfinement) 181 182 err = yaml.Unmarshal([]byte("strict"), &confinementType) 183 c.Assert(err, IsNil) 184 c.Check(confinementType, Equals, StrictConfinement) 185 } 186 187 func (s *typeSuite) TestYamlUnmarshalInvalidConfinementTypes(c *C) { 188 var invalidConfinementTypes = []string{ 189 "foo", "strict-", "_devmode", 190 } 191 var confinementType ConfinementType 192 for _, thisConfinementType := range invalidConfinementTypes { 193 err := yaml.Unmarshal([]byte(thisConfinementType), &confinementType) 194 c.Assert(err, NotNil, Commentf("Expected '%s' to be an invalid confinement type", thisConfinementType)) 195 } 196 } 197 198 func (s *typeSuite) TestJsonMarshalConfinementTypes(c *C) { 199 out, err := json.Marshal(DevModeConfinement) 200 c.Assert(err, IsNil) 201 c.Check(string(out), Equals, "\"devmode\"") 202 203 out, err = json.Marshal(StrictConfinement) 204 c.Assert(err, IsNil) 205 c.Check(string(out), Equals, "\"strict\"") 206 } 207 208 func (s *typeSuite) TestJsonUnmarshalConfinementTypes(c *C) { 209 var confinementType ConfinementType 210 err := json.Unmarshal([]byte("\"devmode\""), &confinementType) 211 c.Assert(err, IsNil) 212 c.Check(confinementType, Equals, DevModeConfinement) 213 214 err = json.Unmarshal([]byte("\"strict\""), &confinementType) 215 c.Assert(err, IsNil) 216 c.Check(confinementType, Equals, StrictConfinement) 217 } 218 219 func (s *typeSuite) TestJsonUnmarshalInvalidConfinementTypes(c *C) { 220 var invalidConfinementTypes = []string{ 221 "foo", "strict-", "_devmode", 222 } 223 var confinementType ConfinementType 224 for _, thisConfinementType := range invalidConfinementTypes { 225 err := json.Unmarshal([]byte(fmt.Sprintf("%q", thisConfinementType)), &confinementType) 226 c.Assert(err, NotNil, Commentf("Expected '%s' to be an invalid confinement type", thisConfinementType)) 227 } 228 } 229 230 func (s *typeSuite) TestYamlMarshalDaemonScopes(c *C) { 231 out, err := yaml.Marshal(SystemDaemon) 232 c.Assert(err, IsNil) 233 c.Check(string(out), Equals, "system\n") 234 235 out, err = yaml.Marshal(UserDaemon) 236 c.Assert(err, IsNil) 237 c.Check(string(out), Equals, "user\n") 238 } 239 240 func (s *typeSuite) TestYamlUnmarshalDaemonScopes(c *C) { 241 var daemonScope DaemonScope 242 err := yaml.Unmarshal([]byte("system"), &daemonScope) 243 c.Assert(err, IsNil) 244 c.Check(daemonScope, Equals, SystemDaemon) 245 246 err = yaml.Unmarshal([]byte("user"), &daemonScope) 247 c.Assert(err, IsNil) 248 c.Check(daemonScope, Equals, UserDaemon) 249 } 250 251 func (s *typeSuite) TestYamlUnmarshalInvalidDaemonScopes(c *C) { 252 var invalidDaemonScopes = []string{ 253 "foo", "system-", "_user", 254 } 255 var daemonScope DaemonScope 256 for _, thisDaemonScope := range invalidDaemonScopes { 257 err := yaml.Unmarshal([]byte(thisDaemonScope), &daemonScope) 258 c.Assert(err, NotNil, Commentf("Expected '%s' to be an invalid daemon scope", thisDaemonScope)) 259 } 260 } 261 262 func (s *typeSuite) TestJsonMarshalDaemonScopes(c *C) { 263 out, err := json.Marshal(SystemDaemon) 264 c.Assert(err, IsNil) 265 c.Check(string(out), Equals, "\"system\"") 266 267 out, err = json.Marshal(UserDaemon) 268 c.Assert(err, IsNil) 269 c.Check(string(out), Equals, "\"user\"") 270 } 271 272 func (s *typeSuite) TestJsonUnmarshalDaemonScopes(c *C) { 273 var daemonScope DaemonScope 274 err := json.Unmarshal([]byte("\"system\""), &daemonScope) 275 c.Assert(err, IsNil) 276 c.Check(daemonScope, Equals, SystemDaemon) 277 278 err = json.Unmarshal([]byte("\"user\""), &daemonScope) 279 c.Assert(err, IsNil) 280 c.Check(daemonScope, Equals, UserDaemon) 281 } 282 283 func (s *typeSuite) TestJsonUnmarshalInvalidDaemonScopes(c *C) { 284 var invalidDaemonScopes = []string{ 285 "foo", "system-", "_user", 286 } 287 var daemonScope DaemonScope 288 for _, thisDaemonScope := range invalidDaemonScopes { 289 err := json.Unmarshal([]byte(fmt.Sprintf("%q", thisDaemonScope)), &daemonScope) 290 c.Assert(err, NotNil, Commentf("Expected '%s' to be an invalid daemon scope", thisDaemonScope)) 291 } 292 }