github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/secboot/encrypt_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  // +build !nosecboot
     3  
     4  /*
     5   * Copyright (C) 2019-2020 Canonical Ltd
     6   *
     7   * This program is free software: you can redistribute it and/or modify
     8   * it under the terms of the GNU General Public License version 3 as
     9   * published by the Free Software Foundation.
    10   *
    11   * This program is distributed in the hope that it will be useful,
    12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14   * GNU General Public License for more details.
    15   *
    16   * You should have received a copy of the GNU General Public License
    17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18   *
    19   */
    20  
    21  package secboot_test
    22  
    23  import (
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/secboot"
    30  	"github.com/snapcore/snapd/testutil"
    31  )
    32  
    33  type encryptSuite struct {
    34  	dir string
    35  }
    36  
    37  var _ = Suite(&encryptSuite{})
    38  
    39  func (s *encryptSuite) SetUpTest(c *C) {
    40  	s.dir = c.MkDir()
    41  }
    42  
    43  func (s *encryptSuite) TestRecoveryKeySave(c *C) {
    44  	kf := filepath.Join(s.dir, "test-key")
    45  	kfNested := filepath.Join(s.dir, "deeply/nested/test-key")
    46  
    47  	rkey := secboot.RecoveryKey{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 255}
    48  	err := rkey.Save(kf)
    49  	c.Assert(err, IsNil)
    50  	c.Assert(kf, testutil.FileEquals, rkey[:])
    51  
    52  	fileInfo, err := os.Stat(kf)
    53  	c.Assert(err, IsNil)
    54  	c.Assert(fileInfo.Mode(), Equals, os.FileMode(0600))
    55  
    56  	err = rkey.Save(kfNested)
    57  	c.Assert(err, IsNil)
    58  	c.Assert(kfNested, testutil.FileEquals, rkey[:])
    59  	di, err := os.Stat(filepath.Dir(kfNested))
    60  	c.Assert(err, IsNil)
    61  	c.Assert(di.Mode().Perm(), Equals, os.FileMode(0755))
    62  }
    63  
    64  func (s *encryptSuite) TestEncryptionKeySave(c *C) {
    65  	kf := filepath.Join(s.dir, "test-key")
    66  	kfNested := filepath.Join(s.dir, "deeply/nested/test-key")
    67  
    68  	ekey := secboot.EncryptionKey{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 255}
    69  	err := ekey.Save(kf)
    70  	c.Assert(err, IsNil)
    71  	c.Assert(kf, testutil.FileEquals, ekey[:])
    72  
    73  	fileInfo, err := os.Stat(kf)
    74  	c.Assert(err, IsNil)
    75  	c.Assert(fileInfo.Mode(), Equals, os.FileMode(0600))
    76  
    77  	err = ekey.Save(kfNested)
    78  	c.Assert(err, IsNil)
    79  	c.Assert(kfNested, testutil.FileEquals, ekey[:])
    80  	di, err := os.Stat(filepath.Dir(kfNested))
    81  	c.Assert(err, IsNil)
    82  	c.Assert(di.Mode().Perm(), Equals, os.FileMode(0755))
    83  }