github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/bootloader/grubenv/grubenv_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 grubenv_test
    21  
    22  import (
    23  	"fmt"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/bootloader/grubenv"
    30  	"github.com/snapcore/snapd/testutil"
    31  )
    32  
    33  // Hook up check.v1 into the "go test" runner
    34  func Test(t *testing.T) { TestingT(t) }
    35  
    36  type grubenvTestSuite struct {
    37  	envPath string
    38  }
    39  
    40  var _ = Suite(&grubenvTestSuite{})
    41  
    42  func (g *grubenvTestSuite) SetUpTest(c *C) {
    43  	g.envPath = filepath.Join(c.MkDir(), "grubenv")
    44  }
    45  
    46  func (g *grubenvTestSuite) TestSet(c *C) {
    47  	env := grubenv.NewEnv(g.envPath)
    48  	c.Check(env, NotNil)
    49  
    50  	env.Set("key", "value")
    51  	c.Check(env.Get("key"), Equals, "value")
    52  }
    53  
    54  func (g *grubenvTestSuite) TestSave(c *C) {
    55  	env := grubenv.NewEnv(g.envPath)
    56  	c.Check(env, NotNil)
    57  
    58  	env.Set("key1", "value1")
    59  	env.Set("key2", "value2")
    60  	env.Set("key3", "value3")
    61  	env.Set("key4", "value4")
    62  	env.Set("key5", "value5")
    63  	env.Set("key6", "value6")
    64  	env.Set("key7", "value7")
    65  	// set "key1" again, ordering (position) does not change
    66  	env.Set("key1", "value1")
    67  
    68  	err := env.Save()
    69  	c.Assert(err, IsNil)
    70  
    71  	c.Assert(g.envPath, testutil.FileEquals, `# GRUB Environment Block
    72  key1=value1
    73  key2=value2
    74  key3=value3
    75  key4=value4
    76  key5=value5
    77  key6=value6
    78  key7=value7
    79  ###################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################`)
    80  }
    81  
    82  func (g *grubenvTestSuite) TestSaveOverflow(c *C) {
    83  	env := grubenv.NewEnv(g.envPath)
    84  	c.Check(env, NotNil)
    85  
    86  	for i := 0; i < 101; i++ {
    87  		env.Set(fmt.Sprintf("key%d", i), "foo")
    88  	}
    89  
    90  	err := env.Save()
    91  	c.Assert(err, ErrorMatches, `cannot write grubenv .*: bigger than 1024 bytes \(1026\)`)
    92  }