gitee.com/mysnapcore/mysnapd@v0.1.0/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 "gitee.com/mysnapcore/mysnapd/release" 28 "gitee.com/mysnapcore/mysnapd/snapdenv" 29 "gitee.com/mysnapcore/mysnapd/testutil" 30 ) 31 32 type UASuite struct { 33 restore func() 34 } 35 36 var _ = Suite(&UASuite{}) 37 38 func (s *UASuite) SetUpTest(c *C) { 39 s.restore = snapdenv.MockUserAgent("-") 40 } 41 42 func (s *UASuite) TearDownTest(c *C) { 43 s.restore() 44 } 45 46 func (s *UASuite) TestUserAgent(c *C) { 47 snapdenv.SetUserAgentFromVersion("10", nil) 48 ua := snapdenv.UserAgent() 49 c.Check(strings.HasPrefix(ua, "snapd/10 "), Equals, true) 50 51 snapdenv.SetUserAgentFromVersion("10", nil, "extraProd") 52 ua = snapdenv.UserAgent() 53 c.Check(strings.Contains(ua, "extraProd"), Equals, true) 54 c.Check(strings.Contains(ua, "devmode"), Equals, false) 55 56 devmode := false 57 probeForceDevMode := func() bool { return devmode } 58 59 snapdenv.SetUserAgentFromVersion("10", probeForceDevMode, "extraProd") 60 ua = snapdenv.UserAgent() 61 c.Check(strings.Contains(ua, "devmode"), Equals, false) 62 63 devmode = true 64 snapdenv.SetUserAgentFromVersion("10", probeForceDevMode, "extraProd") 65 ua = snapdenv.UserAgent() 66 c.Check(strings.Contains(ua, "devmode"), Equals, true) 67 } 68 69 func (s *UASuite) TestUserAgentWSL(c *C) { 70 defer testutil.Backup(&release.OnWSL)() 71 72 release.OnWSL = false 73 snapdenv.SetUserAgentFromVersion("10", nil) 74 ua := snapdenv.UserAgent() 75 c.Check(strings.Contains(ua, "wsl"), Equals, false) 76 77 release.OnWSL = true 78 snapdenv.SetUserAgentFromVersion("10", nil) 79 ua = snapdenv.UserAgent() 80 c.Check(strings.Contains(ua, "wsl"), Equals, true) 81 } 82 83 func (s *UASuite) TestStripUnsafeRunes(c *C) { 84 // Validity check, strings like that are not modified 85 for _, unchanged := range []string{ 86 "abc-xyz-ABC-XYZ-0-9", 87 ".", "-", "_", 88 "4.4.0-62-generic", 89 "4.8.6-x86_64-linode78", 90 } { 91 c.Check(snapdenv.StripUnsafeRunes(unchanged), Equals, unchanged, Commentf("%q", unchanged)) 92 } 93 for _, t := range []struct{ orig, changed string }{ 94 {"space bar", "spacebar"}, 95 {"~;+()[]", ""}, // most punctuation goes away 96 } { 97 c.Check(snapdenv.StripUnsafeRunes(t.orig), Equals, t.changed) 98 } 99 100 } 101 102 func (s *UASuite) TestSanitizeKernelVersion(c *C) { 103 // Ensure that it is not too long (at most 25 runes) 104 const in = "this-is-a-very-long-thing-that-pretends-to-be-a-kernel-version-string" 105 const out = "this-is-a-very-long-thing" 106 c.Check(snapdenv.SanitizeKernelVersion(in), Equals, out) 107 108 }