github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/engine/saltpack_verify_test.go (about)

     1  package engine
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/keybase/client/go/libkb"
    10  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    11  	"github.com/keybase/saltpack"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestSaltpackVerifyErrors(t *testing.T) {
    16  	tc := SetupEngineTest(t, "verify")
    17  	defer tc.Cleanup()
    18  	fu := CreateAndSignupFakeUser(tc, "ver")
    19  
    20  	var sink bytes.Buffer
    21  
    22  	msg := "10 days in Japan"
    23  	sarg := &SaltpackSignArg{
    24  		Sink:   libkb.NopWriteCloser{W: &sink},
    25  		Source: io.NopCloser(bytes.NewBufferString(msg)),
    26  	}
    27  
    28  	eng := NewSaltpackSign(tc.G, sarg)
    29  	uis := libkb.UIs{
    30  		IdentifyUI: &FakeIdentifyUI{},
    31  		SecretUI:   fu.NewSecretUI(),
    32  	}
    33  
    34  	m := NewMetaContextForTest(tc).WithUIs(uis)
    35  	if err := RunEngine2(m, eng); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	sig := sink.String()
    40  	t.Logf("signed data: %s", sig)
    41  
    42  	// test SignedBy option:
    43  	varg := &SaltpackVerifyArg{
    44  		Sink:   libkb.NopWriteCloser{W: &sink},
    45  		Source: strings.NewReader(sig),
    46  		Opts: keybase1.SaltpackVerifyOptions{
    47  			SignedBy: fu.Username,
    48  		},
    49  	}
    50  
    51  	m = m.WithSaltpackUI(fakeSaltpackUI{})
    52  	veng := NewSaltpackVerify(tc.G, varg)
    53  	if err := RunEngine2(m, veng); err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	invalidFormatMsg := `BEGIN KEYBASE SALTPACK SIGNED MESSAGE. kXR7VktZdyH7rvq v5weRa0zkUpZbPl nShyKLCCGSyBcvL sg7Gi9ySjlkxHPS RUM4Vm3DUD635GV a9LihWKrvns0JGJ RmkXOCgHdP3xfwr if6ynJGBkv7cOUC xLo6Q4zPrJ4TAKG bIc1OaeFW8rmpBo OyWcfzK9cRARuy5 hP0TMta2T2mgL0P 3Dwjg3VFKL. END KEYBASE SALTPACK SIGNED.`
    58  
    59  	// test SignedBy option:
    60  	varg = &SaltpackVerifyArg{
    61  		Sink:   libkb.NopWriteCloser{W: &sink},
    62  		Source: strings.NewReader(invalidFormatMsg),
    63  		Opts: keybase1.SaltpackVerifyOptions{
    64  			SignedBy: fu.Username,
    65  		},
    66  	}
    67  	veng = NewSaltpackVerify(tc.G, varg)
    68  	err := RunEngine2(m, veng)
    69  	require.Error(t, err)
    70  	require.IsType(t, libkb.VerificationError{}, err)
    71  	if err, ok := err.(libkb.VerificationError); ok {
    72  		require.Equal(t, 0, err.Cause.StatusCode) // not set
    73  	}
    74  
    75  	wrongTypeSig := `BEGIN KEYBASE SALTPACK ENCRYPTED MESSAGE. kiOUtMhcc4NXXRb XMxIeCbf5rCLT18 pRBzZdL55xh2r43 A3X3m0ge60cfaQp Lv2fkhFdLXXzkqr qPkQ8IoPvsi27CB wQhS4rFkNRwOgDF G1keffFjdpeZj7h xk15rsO1gIBbpIy 1KYUAdFv8GcyxQ7 jipKdjlAr334MDn r6aaaXxiTs6io71 cr0FJoqVtCJfxNV JGaqDu1W9zT7XTq 5pyyVKTdzE1MzLd jTi21JXMTxOeWfe B42YrRzmyNOrBw9 rve1C4zoYUaL2zk 44zqEkpZloLiyn9 Y9EOjMRe6tdGnq4 GxJBAI0q6Im0Q7k fN4STMCmhgdjnag PWdxXfL1gh912B9 M7EFAQ3x9IWnp7U N2ezXytHvVJTXkR Lpo23jh7WOQkXpS 2tDszd1a46Atd7Q 9u2G9JsfxIKi6im AkCxEBReBQbhpWM e2r4qyN24Kbya2s Zc67fEYK6EWcMFq MEEwO2hKZYBwshq nND9jcuPAVxPTLv 9mEotIO7ubkYHVm 8DCyR4W4Owkiw4V 0DFLUP5tgHXJKXy 2YYDzR2NvnKfqJd z8Bn15pYkZ9sUhA KuFIHFiA95RfFsY cn2Se7YGGBP. END KEYBASE SALTPACK ENCRYPTED MESSAGE.`
    76  
    77  	// test SignedBy option:
    78  	varg = &SaltpackVerifyArg{
    79  		Sink:   libkb.NopWriteCloser{W: &sink},
    80  		Source: strings.NewReader(wrongTypeSig),
    81  		Opts: keybase1.SaltpackVerifyOptions{
    82  			SignedBy: fu.Username,
    83  		},
    84  	}
    85  	veng = NewSaltpackVerify(tc.G, varg)
    86  	err = RunEngine2(m, veng)
    87  	require.Error(t, err)
    88  	require.IsType(t, libkb.VerificationError{}, err)
    89  	if err, ok := err.(libkb.VerificationError); ok {
    90  		require.IsType(t, saltpack.ErrWrongMessageType{}, err.Cause.Err)
    91  		require.IsType(t, libkb.SCWrongCryptoMsgType, err.Cause.StatusCode)
    92  	}
    93  }