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