github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap/cmd_keys_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  	"fmt"
    25  	"io/ioutil"
    26  	"os"
    27  	"path/filepath"
    28  	"testing"
    29  
    30  	. "gopkg.in/check.v1"
    31  
    32  	snap "github.com/snapcore/snapd/cmd/snap"
    33  )
    34  
    35  type SnapKeysSuite struct {
    36  	BaseSnapSuite
    37  
    38  	GnupgCmd string
    39  	tempdir  string
    40  }
    41  
    42  // FIXME: Ideally we would just use gpg2 and remove the gnupg2_test.go file.
    43  //        However currently there is LP: #1621839 which prevents us from
    44  //        switching to gpg2 fully. Once this is resolved we should switch.
    45  var _ = Suite(&SnapKeysSuite{GnupgCmd: "/usr/bin/gpg"})
    46  
    47  var fakePinentryData = []byte(`#!/bin/sh
    48  set -e
    49  echo "OK Pleased to meet you"
    50  while true; do
    51    read line
    52    case $line in
    53    BYE)
    54      exit 0
    55    ;;
    56    *)
    57      echo "OK I agree to everything"
    58      ;;
    59  esac
    60  done
    61  `)
    62  
    63  func (s *SnapKeysSuite) SetUpTest(c *C) {
    64  	if testing.Short() && s.GnupgCmd == "/usr/bin/gpg2" {
    65  		c.Skip("gpg2 does not do short tests")
    66  	}
    67  	s.BaseSnapSuite.SetUpTest(c)
    68  
    69  	s.tempdir = c.MkDir()
    70  	for _, fileName := range []string{"pubring.gpg", "secring.gpg", "trustdb.gpg"} {
    71  		data, err := ioutil.ReadFile(filepath.Join("test-data", fileName))
    72  		c.Assert(err, IsNil)
    73  		err = ioutil.WriteFile(filepath.Join(s.tempdir, fileName), data, 0644)
    74  		c.Assert(err, IsNil)
    75  	}
    76  	fakePinentryFn := filepath.Join(s.tempdir, "pinentry-fake")
    77  	err := ioutil.WriteFile(fakePinentryFn, fakePinentryData, 0755)
    78  	c.Assert(err, IsNil)
    79  	gpgAgentConfFn := filepath.Join(s.tempdir, "gpg-agent.conf")
    80  	err = ioutil.WriteFile(gpgAgentConfFn, []byte(fmt.Sprintf(`pinentry-program %s`, fakePinentryFn)), 0644)
    81  	c.Assert(err, IsNil)
    82  
    83  	os.Setenv("SNAP_GNUPG_HOME", s.tempdir)
    84  	os.Setenv("SNAP_GNUPG_CMD", s.GnupgCmd)
    85  }
    86  
    87  func (s *SnapKeysSuite) TearDownTest(c *C) {
    88  	os.Unsetenv("SNAP_GNUPG_HOME")
    89  	os.Unsetenv("SNAP_GNUPG_CMD")
    90  	s.BaseSnapSuite.TearDownTest(c)
    91  }
    92  
    93  func (s *SnapKeysSuite) TestKeys(c *C) {
    94  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"keys"})
    95  	c.Assert(err, IsNil)
    96  	c.Assert(rest, DeepEquals, []string{})
    97  	c.Check(s.Stdout(), Matches, `Name +SHA3-384
    98  default +g4Pks54W_US4pZuxhgG_RHNAf_UeZBBuZyGRLLmMj1Do3GkE_r_5A5BFjx24ZwVJ
    99  another +DVQf1U4mIsuzlQqAebjjTPYtYJ-GEhJy0REuj3zvpQYTZ7EJj7adBxIXLJ7Vmk3L
   100  `)
   101  	c.Check(s.Stderr(), Equals, "")
   102  }
   103  
   104  func (s *SnapKeysSuite) TestKeysEmptyNoHeader(c *C) {
   105  	// simulate empty keys
   106  	err := os.RemoveAll(s.tempdir)
   107  	c.Assert(err, IsNil)
   108  
   109  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"keys"})
   110  	c.Assert(err, IsNil)
   111  	c.Assert(rest, DeepEquals, []string{})
   112  	c.Check(s.Stdout(), Equals, "")
   113  	c.Check(s.Stderr(), Equals, "No keys registered, see `snapcraft create-key`\n")
   114  }
   115  
   116  func (s *SnapKeysSuite) TestKeysJSON(c *C) {
   117  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"keys", "--json"})
   118  	c.Assert(err, IsNil)
   119  	c.Assert(rest, DeepEquals, []string{})
   120  	expectedResponse := []snap.Key{
   121  		{
   122  			Name:     "default",
   123  			Sha3_384: "g4Pks54W_US4pZuxhgG_RHNAf_UeZBBuZyGRLLmMj1Do3GkE_r_5A5BFjx24ZwVJ",
   124  		},
   125  		{
   126  			Name:     "another",
   127  			Sha3_384: "DVQf1U4mIsuzlQqAebjjTPYtYJ-GEhJy0REuj3zvpQYTZ7EJj7adBxIXLJ7Vmk3L",
   128  		},
   129  	}
   130  	var obtainedResponse []snap.Key
   131  	json.Unmarshal(s.stdout.Bytes(), &obtainedResponse)
   132  	c.Check(obtainedResponse, DeepEquals, expectedResponse)
   133  	c.Check(s.Stderr(), Equals, "")
   134  }
   135  
   136  func (s *SnapKeysSuite) TestKeysJSONEmpty(c *C) {
   137  	err := os.RemoveAll(os.Getenv("SNAP_GNUPG_HOME"))
   138  	c.Assert(err, IsNil)
   139  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"keys", "--json"})
   140  	c.Assert(err, IsNil)
   141  	c.Assert(rest, DeepEquals, []string{})
   142  	c.Check(s.Stdout(), Equals, "[]\n")
   143  	c.Check(s.Stderr(), Equals, "")
   144  }