gitee.com/mysnapcore/mysnapd@v0.1.0/cmd/snap/cmd_delete_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  	"encoding/json"
    24  	"os"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	snap "gitee.com/mysnapcore/mysnapd/cmd/snap"
    29  	"gitee.com/mysnapcore/mysnapd/testutil"
    30  )
    31  
    32  // XXX: share this helper with signtool tests?
    33  func mockNopExtKeyMgr(c *C) (pgm *testutil.MockCmd, restore func()) {
    34  	os.Setenv("SNAPD_EXT_KEYMGR", "keymgr")
    35  	pgm = testutil.MockCommand(c, "keymgr", `
    36  if [ "$1" == "features" ]; then
    37    echo '{"signing":["RSA-PKCS"] , "public-keys":["DER"]}'
    38    exit 0
    39  fi
    40  exit 1
    41  `)
    42  	r := func() {
    43  		pgm.Restore()
    44  		os.Unsetenv("SNAPD_EXT_KEYMGR")
    45  	}
    46  
    47  	return pgm, r
    48  }
    49  
    50  func (s *SnapKeysSuite) TestDeleteKeyRequiresName(c *C) {
    51  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"delete-key"})
    52  	c.Assert(err, NotNil)
    53  	c.Check(err.Error(), Equals, "the required argument `<key-name>` was not provided")
    54  	c.Check(s.Stdout(), Equals, "")
    55  	c.Check(s.Stderr(), Equals, "")
    56  }
    57  
    58  func (s *SnapKeysSuite) TestDeleteKeyNonexistent(c *C) {
    59  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"delete-key", "nonexistent"})
    60  	c.Assert(err, NotNil)
    61  	c.Check(err.Error(), Equals, `cannot delete key named "nonexistent": cannot find key pair in GPG keyring`)
    62  	c.Check(s.Stdout(), Equals, "")
    63  	c.Check(s.Stderr(), Equals, "")
    64  }
    65  
    66  func (s *SnapKeysSuite) TestDeleteKey(c *C) {
    67  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"delete-key", "another"})
    68  	c.Assert(err, IsNil)
    69  	c.Assert(rest, DeepEquals, []string{})
    70  	c.Check(s.Stdout(), Equals, "")
    71  	c.Check(s.Stderr(), Equals, "")
    72  	_, err = snap.Parser(snap.Client()).ParseArgs([]string{"keys", "--json"})
    73  	c.Assert(err, IsNil)
    74  	expectedResponse := []snap.Key{
    75  		{
    76  			Name:     "default",
    77  			Sha3_384: "g4Pks54W_US4pZuxhgG_RHNAf_UeZBBuZyGRLLmMj1Do3GkE_r_5A5BFjx24ZwVJ",
    78  		},
    79  	}
    80  	var obtainedResponse []snap.Key
    81  	json.Unmarshal(s.stdout.Bytes(), &obtainedResponse)
    82  	c.Check(obtainedResponse, DeepEquals, expectedResponse)
    83  	c.Check(s.Stderr(), Equals, "")
    84  }
    85  
    86  func (s *SnapKeysSuite) TestDeleteKeyExternalUnsupported(c *C) {
    87  	_, restore := mockNopExtKeyMgr(c)
    88  	defer restore()
    89  
    90  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"delete-key", "key"})
    91  	c.Assert(err, NotNil)
    92  	c.Check(err.Error(), Equals, "cannot delete external keypair manager key via snap command, use the appropriate external procedure")
    93  	c.Check(s.Stdout(), Equals, "")
    94  	c.Check(s.Stderr(), Equals, "")
    95  }