github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/engine/pgp_sign_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 engine
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  	"testing"
    10  
    11  	"github.com/keybase/client/go/libkb"
    12  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    13  )
    14  
    15  type signTest struct {
    16  	name  string
    17  	input string
    18  }
    19  
    20  var signTests = []signTest{
    21  	{name: "john hancock", input: "When in the Course of human events, it becomes necessary for one people to dissolve the political bands"},
    22  	{name: "empty", input: ""},
    23  }
    24  
    25  // Test pgp sign attached.
    26  func TestPGPSign(t *testing.T) {
    27  	tc := SetupEngineTest(t, "pgp_sign")
    28  	defer tc.Cleanup()
    29  	fu := createFakeUserWithPGPSibkeyPushed(tc)
    30  
    31  	if err := fu.LoadUser(tc); err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	if fu.User == nil {
    36  		t.Fatal("got a nil User")
    37  	}
    38  
    39  	m := libkb.NewMetaContextForTest(tc)
    40  
    41  	skb, err := fu.User.GetSyncedSecretKey(m)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	if skb == nil {
    47  		t.Fatalf("skb is nil")
    48  	}
    49  
    50  	key, err := skb.GetPubKey()
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	for _, test := range signTests {
    56  		var sink bytes.Buffer
    57  
    58  		earg := PGPSignArg{
    59  			Sink:   libkb.NopWriteCloser{W: &sink},
    60  			Source: io.NopCloser(bytes.NewBufferString(test.input)),
    61  			Opts: keybase1.PGPSignOptions{
    62  				Mode: keybase1.SignMode_ATTACHED,
    63  			},
    64  		}
    65  
    66  		eng := NewPGPSignEngine(tc.G, &earg)
    67  		uis := libkb.UIs{
    68  			PgpUI:    &TestPgpUI{},
    69  			SecretUI: fu.NewSecretUI(),
    70  		}
    71  
    72  		m := NewMetaContextForTest(tc).WithUIs(uis)
    73  		err = RunEngine2(m, eng)
    74  		if err != nil {
    75  			t.Errorf("%s: run error: %s", test.name, err)
    76  			continue
    77  		}
    78  
    79  		sig := sink.String()
    80  
    81  		_, err = key.VerifyString(tc.G.Log, sig, []byte(test.input))
    82  		if err != nil {
    83  			t.Errorf("%s: verify error: %s", test.name, err)
    84  			continue
    85  		}
    86  	}
    87  }