github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/snap/cmd_export_key_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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 main_test
    21  
    22  import (
    23  	"time"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/asserts"
    28  	"github.com/snapcore/snapd/asserts/assertstest"
    29  	snap "github.com/snapcore/snapd/cmd/snap"
    30  )
    31  
    32  func (s *SnapKeysSuite) TestExportKeyNonexistent(c *C) {
    33  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"export-key", "nonexistent"})
    34  	c.Assert(err, NotNil)
    35  	c.Check(err.Error(), Equals, "cannot find key named \"nonexistent\" in GPG keyring")
    36  	c.Check(s.Stdout(), Equals, "")
    37  	c.Check(s.Stderr(), Equals, "")
    38  }
    39  
    40  func (s *SnapKeysSuite) TestExportKeyDefault(c *C) {
    41  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"export-key"})
    42  	c.Assert(err, IsNil)
    43  	c.Assert(rest, DeepEquals, []string{})
    44  	pubKey, err := asserts.DecodePublicKey(s.stdout.Bytes())
    45  	c.Assert(err, IsNil)
    46  	c.Check(pubKey.ID(), Equals, "g4Pks54W_US4pZuxhgG_RHNAf_UeZBBuZyGRLLmMj1Do3GkE_r_5A5BFjx24ZwVJ")
    47  	c.Check(s.Stderr(), Equals, "")
    48  }
    49  
    50  func (s *SnapKeysSuite) TestExportKeyNonDefault(c *C) {
    51  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"export-key", "another"})
    52  	c.Assert(err, IsNil)
    53  	c.Assert(rest, DeepEquals, []string{})
    54  	pubKey, err := asserts.DecodePublicKey(s.stdout.Bytes())
    55  	c.Assert(err, IsNil)
    56  	c.Check(pubKey.ID(), Equals, "DVQf1U4mIsuzlQqAebjjTPYtYJ-GEhJy0REuj3zvpQYTZ7EJj7adBxIXLJ7Vmk3L")
    57  	c.Check(s.Stderr(), Equals, "")
    58  }
    59  
    60  func (s *SnapKeysSuite) TestExportKeyAccount(c *C) {
    61  	storeSigning := assertstest.NewStoreStack("canonical", nil)
    62  	manager := asserts.NewGPGKeypairManager()
    63  	assertstest.NewAccount(storeSigning, "developer1", nil, "")
    64  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"export-key", "another", "--account=developer1"})
    65  	c.Assert(err, IsNil)
    66  	c.Assert(rest, DeepEquals, []string{})
    67  	assertion, err := asserts.Decode(s.stdout.Bytes())
    68  	c.Assert(err, IsNil)
    69  	c.Check(assertion.Type(), Equals, asserts.AccountKeyRequestType)
    70  	c.Check(assertion.Revision(), Equals, 0)
    71  	c.Check(assertion.HeaderString("account-id"), Equals, "developer1")
    72  	c.Check(assertion.HeaderString("name"), Equals, "another")
    73  	c.Check(assertion.HeaderString("public-key-sha3-384"), Equals, "DVQf1U4mIsuzlQqAebjjTPYtYJ-GEhJy0REuj3zvpQYTZ7EJj7adBxIXLJ7Vmk3L")
    74  	since, err := time.Parse(time.RFC3339, assertion.HeaderString("since"))
    75  	c.Assert(err, IsNil)
    76  	zone, offset := since.Zone()
    77  	c.Check(zone, Equals, "UTC")
    78  	c.Check(offset, Equals, 0)
    79  	c.Check(s.Stderr(), Equals, "")
    80  	privKey, err := manager.Get(assertion.HeaderString("public-key-sha3-384"))
    81  	c.Assert(err, IsNil)
    82  	err = asserts.SignatureCheck(assertion, privKey.PublicKey())
    83  	c.Assert(err, IsNil)
    84  }