github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/snapstate/cookies_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 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 snapstate 21 22 import ( 23 "encoding/json" 24 "fmt" 25 "io/ioutil" 26 "path/filepath" 27 28 . "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/dirs" 31 "github.com/snapcore/snapd/osutil" 32 "github.com/snapcore/snapd/overlord/state" 33 "github.com/snapcore/snapd/testutil" 34 ) 35 36 type cookiesSuite struct { 37 testutil.BaseTest 38 st *state.State 39 snapmgr *SnapManager 40 } 41 42 var _ = Suite(&cookiesSuite{}) 43 44 func (s *cookiesSuite) SetUpTest(c *C) { 45 s.BaseTest.SetUpTest(c) 46 dirs.SetRootDir(c.MkDir()) 47 s.st = state.New(nil) 48 s.snapmgr, _ = Manager(s.st, state.NewTaskRunner(s.st)) 49 } 50 51 func (s *cookiesSuite) TearDownTest(c *C) { 52 s.BaseTest.TearDownTest(c) 53 } 54 55 func checkCookie(c *C, st *state.State, snapName string) { 56 var cookies map[string]string 57 var found int 58 var cookieID string 59 60 c.Assert(st.Get("snap-cookies", &cookies), IsNil) 61 62 for cookie, snap := range cookies { 63 if snap == snapName { 64 found = found + 1 65 cookieID = cookie 66 } 67 } 68 c.Assert(found, Equals, 1) 69 70 c.Assert(fmt.Sprintf("%s/snap.%s", dirs.SnapCookieDir, snapName), testutil.FileEquals, cookieID) 71 c.Assert(cookieID, HasLen, 44) 72 } 73 74 func (s *cookiesSuite) TestSyncCookies(c *C) { 75 s.st.Lock() 76 defer s.st.Unlock() 77 78 // verify that SyncCookies creates a cookie for a snap that's missing it and removes stale/invalid cookies 79 s.st.Set("snaps", map[string]*json.RawMessage{ 80 "some-snap": nil, 81 "other-snap": nil}) 82 staleCookieFile := filepath.Join(dirs.SnapCookieDir, "snap.stale-cookie-snap") 83 c.Assert(ioutil.WriteFile(staleCookieFile, nil, 0644), IsNil) 84 c.Assert(osutil.FileExists(staleCookieFile), Equals, true) 85 86 // some-snap doesn't have cookie 87 cookies := map[string]string{ 88 "123456": "other-snap", 89 "809809": "other-snap", 90 "999999": "unknown-snap", 91 "199989": "unknown-snap", 92 } 93 s.st.Set("snap-cookies", cookies) 94 95 for i := 0; i < 2; i++ { 96 s.snapmgr.SyncCookies(s.st) 97 98 c.Assert(osutil.FileExists(staleCookieFile), Equals, false) 99 100 var newCookies map[string]string 101 err := s.st.Get("snap-cookies", &newCookies) 102 c.Assert(err, IsNil) 103 c.Assert(newCookies, HasLen, 2) 104 105 cookieFile := filepath.Join(dirs.SnapCookieDir, "snap.some-snap") 106 c.Assert(osutil.FileExists(cookieFile), Equals, true) 107 data, err := ioutil.ReadFile(cookieFile) 108 c.Assert(err, IsNil) 109 c.Assert(newCookies[string(data)], NotNil) 110 c.Assert(newCookies[string(data)], Equals, "some-snap") 111 112 cookieFile = filepath.Join(dirs.SnapCookieDir, "snap.other-snap") 113 c.Assert(osutil.FileExists(cookieFile), Equals, true) 114 data, err = ioutil.ReadFile(cookieFile) 115 c.Assert(err, IsNil) 116 c.Assert(newCookies[string(data)], NotNil) 117 c.Assert(newCookies[string(data)], Equals, "other-snap") 118 } 119 } 120 121 func (s *cookiesSuite) TestCreateSnapCookie(c *C) { 122 s.st.Lock() 123 defer s.st.Unlock() 124 125 c.Assert(s.snapmgr.createSnapCookie(s.st, "foo"), IsNil) 126 checkCookie(c, s.st, "foo") 127 c.Assert(s.snapmgr.createSnapCookie(s.st, "foo"), IsNil) 128 checkCookie(c, s.st, "foo") 129 } 130 131 func (s *cookiesSuite) TestRemoveSnapCookie(c *C) { 132 s.st.Lock() 133 defer s.st.Unlock() 134 135 cookieFile := filepath.Join(dirs.SnapCookieDir, "snap.bar") 136 137 c.Assert(ioutil.WriteFile(cookieFile, nil, 0644), IsNil) 138 139 // remove should not fail if cookie is not there 140 c.Assert(s.snapmgr.removeSnapCookie(s.st, "bar"), IsNil) 141 c.Assert(osutil.FileExists(cookieFile), Equals, false) 142 143 c.Assert(s.snapmgr.createSnapCookie(s.st, "foo"), IsNil) 144 c.Assert(s.snapmgr.createSnapCookie(s.st, "bar"), IsNil) 145 c.Assert(osutil.FileExists(cookieFile), Equals, true) 146 147 c.Assert(s.snapmgr.removeSnapCookie(s.st, "bar"), IsNil) 148 c.Assert(osutil.FileExists(cookieFile), Equals, false) 149 150 var cookies map[string]string 151 c.Assert(s.st.Get("snap-cookies", &cookies), IsNil) 152 c.Assert(cookies, HasLen, 1) 153 154 // cookie for snap "foo" remains untouched 155 checkCookie(c, s.st, "foo") 156 }