github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/gpg_cli_test.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package libkb
     5  
     6  import (
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  func TestGPGKeyring(t *testing.T) {
    12  	tc := SetupTest(t, "gpg_cli", 1)
    13  	defer tc.Cleanup()
    14  	err := tc.GenerateGPGKeyring("no@no.no")
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  
    19  	for _, fn := range []string{"secring.gpg", "pubring.gpg"} {
    20  		p := filepath.Join(tc.Tp.GPGHome, fn)
    21  		ok, err := FileExists(p)
    22  		if err != nil {
    23  			t.Fatal(err)
    24  		}
    25  		if !ok {
    26  			t.Errorf("file not found: %s", p)
    27  		}
    28  	}
    29  }
    30  
    31  func TestGPGImportSecret(t *testing.T) {
    32  	tc := SetupTest(t, "gpg_cli", 1)
    33  	defer tc.Cleanup()
    34  	if err := tc.GenerateGPGKeyring("no@no.no"); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	cli := NewGpgCLI(tc.G, nil)
    38  	if err := cli.Configure(tc.MetaContext()); err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	index, _, err := cli.Index(tc.MetaContext(), true, "")
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	fps := index.AllFingerprints()
    46  	if len(fps) != 1 {
    47  		t.Fatalf("num fingerprints: %d, expected 1", len(fps))
    48  	}
    49  	bundle, err := cli.ImportKey(tc.MetaContext(), true, fps[0], "")
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	if bundle == nil {
    54  		t.Fatal("nil bundle")
    55  	}
    56  	if !bundle.HasSecretKey() {
    57  		t.Fatal("bundle doesn't have secret key")
    58  	}
    59  	if !bundle.CanSign() {
    60  		t.Fatal("bundle can't sign")
    61  	}
    62  }
    63  
    64  // Useful to track down signing errors in GPG < 2.0.29
    65  func TestGPGSign(t *testing.T) {
    66  	t.Skip("skipping GPG Sign test")
    67  	tc := SetupTest(t, "gpg_cli", 1)
    68  	defer tc.Cleanup()
    69  	err := tc.GenerateGPGKeyring("no@no.no")
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	cli := NewGpgCLI(tc.G, nil)
    74  	if err := cli.Configure(tc.MetaContext()); err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	index, _, err := cli.Index(tc.MetaContext(), true, "")
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	fps := index.AllFingerprints()
    82  	if len(fps) != 1 {
    83  		t.Fatalf("num fingerprints: %d, expected 1", len(fps))
    84  	}
    85  	fp := fps[0]
    86  
    87  	for i := 0; i < 1000; i++ {
    88  		_, err = cli.Sign(tc.MetaContext(), fp, []byte("hello"))
    89  		if err != nil {
    90  			t.Fatal(err)
    91  		}
    92  	}
    93  }