github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/snapdenv/useragent_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-2017 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 snapdenv_test 21 22 import ( 23 "strings" 24 25 . "gopkg.in/check.v1" 26 27 "github.com/snapcore/snapd/snapdenv" 28 ) 29 30 type UASuite struct { 31 restore func() 32 } 33 34 var _ = Suite(&UASuite{}) 35 36 func (s *UASuite) SetUpTest(c *C) { 37 s.restore = snapdenv.MockUserAgent("-") 38 } 39 40 func (s *UASuite) TearDownTest(c *C) { 41 s.restore() 42 } 43 44 func (s *UASuite) TestUserAgent(c *C) { 45 snapdenv.SetUserAgentFromVersion("10", nil) 46 ua := snapdenv.UserAgent() 47 c.Check(strings.HasPrefix(ua, "snapd/10 "), Equals, true) 48 49 snapdenv.SetUserAgentFromVersion("10", nil, "extraProd") 50 ua = snapdenv.UserAgent() 51 c.Check(strings.Contains(ua, "extraProd"), Equals, true) 52 c.Check(strings.Contains(ua, "devmode"), Equals, false) 53 54 devmode := false 55 probeForceDevMode := func() bool { return devmode } 56 57 snapdenv.SetUserAgentFromVersion("10", probeForceDevMode, "extraProd") 58 ua = snapdenv.UserAgent() 59 c.Check(strings.Contains(ua, "devmode"), Equals, false) 60 61 devmode = true 62 snapdenv.SetUserAgentFromVersion("10", probeForceDevMode, "extraProd") 63 ua = snapdenv.UserAgent() 64 c.Check(strings.Contains(ua, "devmode"), Equals, true) 65 } 66 67 func (s *UASuite) TestStripUnsafeRunes(c *C) { 68 // Sanity check, strings like that are not modified 69 for _, unchanged := range []string{ 70 "abc-xyz-ABC-XYZ-0-9", 71 ".", "-", "_", 72 "4.4.0-62-generic", 73 "4.8.6-x86_64-linode78", 74 } { 75 c.Check(snapdenv.StripUnsafeRunes(unchanged), Equals, unchanged, Commentf("%q", unchanged)) 76 } 77 for _, t := range []struct{ orig, changed string }{ 78 {"space bar", "spacebar"}, 79 {"~;+()[]", ""}, // most punctuation goes away 80 } { 81 c.Check(snapdenv.StripUnsafeRunes(t.orig), Equals, t.changed) 82 } 83 84 } 85 86 func (s *UASuite) TestSanitizeKernelVersion(c *C) { 87 // Ensure that it is not too long (at most 25 runes) 88 const in = "this-is-a-very-long-thing-that-pretends-to-be-a-kernel-version-string" 89 const out = "this-is-a-very-long-thing" 90 c.Check(snapdenv.SanitizeKernelVersion(in), Equals, out) 91 92 }