github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/asserts/memkeypairmgr_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2015-2016 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 asserts_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/asserts"
    26  )
    27  
    28  type memKeypairMgtSuite struct {
    29  	keypairMgr asserts.KeypairManager
    30  }
    31  
    32  var _ = Suite(&memKeypairMgtSuite{})
    33  
    34  func (mkms *memKeypairMgtSuite) SetUpTest(c *C) {
    35  	mkms.keypairMgr = asserts.NewMemoryKeypairManager()
    36  }
    37  
    38  func (mkms *memKeypairMgtSuite) TestPutAndGet(c *C) {
    39  	pk1 := testPrivKey1
    40  	keyID := pk1.PublicKey().ID()
    41  	err := mkms.keypairMgr.Put(pk1)
    42  	c.Assert(err, IsNil)
    43  
    44  	got, err := mkms.keypairMgr.Get(keyID)
    45  	c.Assert(err, IsNil)
    46  	c.Assert(got, NotNil)
    47  	c.Check(got.PublicKey().ID(), Equals, pk1.PublicKey().ID())
    48  }
    49  
    50  func (mkms *memKeypairMgtSuite) TestPutAlreadyExists(c *C) {
    51  	pk1 := testPrivKey1
    52  	err := mkms.keypairMgr.Put(pk1)
    53  	c.Assert(err, IsNil)
    54  
    55  	err = mkms.keypairMgr.Put(pk1)
    56  	c.Check(err, ErrorMatches, "key pair with given key id already exists")
    57  }
    58  
    59  func (mkms *memKeypairMgtSuite) TestGetNotFound(c *C) {
    60  	pk1 := testPrivKey1
    61  	keyID := pk1.PublicKey().ID()
    62  
    63  	got, err := mkms.keypairMgr.Get(keyID)
    64  	c.Check(got, IsNil)
    65  	c.Check(err, ErrorMatches, "cannot find key pair")
    66  
    67  	err = mkms.keypairMgr.Put(pk1)
    68  	c.Assert(err, IsNil)
    69  
    70  	got, err = mkms.keypairMgr.Get(keyID + "x")
    71  	c.Check(got, IsNil)
    72  	c.Check(err, ErrorMatches, "cannot find key pair")
    73  }