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

     1  package stellar
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/keybase/client/go/kbtest"
     8  	"github.com/keybase/client/go/libkb"
     9  	"github.com/keybase/client/go/protocol/stellar1"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestNoteRoundtrip(t *testing.T) {
    14  	sk := randomSymmetricKey(t)
    15  	pre := sampleNote()
    16  	expect := pre.DeepCopy()
    17  	encNote, err := noteEncryptHelper(context.Background(), pre, sk)
    18  	require.NoError(t, err)
    19  	post, err := noteDecryptHelper(context.Background(), encNote, sk)
    20  	require.NoError(t, err)
    21  	require.Equal(t, expect, post)
    22  }
    23  
    24  func TestNoteBadKey(t *testing.T) {
    25  	sk := randomSymmetricKey(t)
    26  	pre := sampleNote()
    27  	encNote, err := noteEncryptHelper(context.Background(), pre, sk)
    28  	require.NoError(t, err)
    29  	if sk[3] == 'c' {
    30  		sk[3] = 'd'
    31  	} else {
    32  		sk[3] = 'c'
    33  	}
    34  	_, err = noteDecryptHelper(context.Background(), encNote, sk)
    35  	require.Error(t, err)
    36  	require.Equal(t, "could not decrypt note secretbox", err.Error())
    37  }
    38  
    39  func TestNoteCorruptCiphertext(t *testing.T) {
    40  	sk := randomSymmetricKey(t)
    41  	pre := sampleNote()
    42  	encNote, err := noteEncryptHelper(context.Background(), pre, sk)
    43  	if encNote.E[3] == 'c' {
    44  		encNote.E[3] = 'd'
    45  	} else {
    46  		encNote.E[3] = 'c'
    47  	}
    48  	require.NoError(t, err)
    49  	_, err = noteDecryptHelper(context.Background(), encNote, sk)
    50  	require.Error(t, err)
    51  	require.Equal(t, "could not decrypt note secretbox", err.Error())
    52  }
    53  
    54  func TestNoteLengthLimit(t *testing.T) {
    55  	tc := libkb.SetupTest(t, "stellar", 2)
    56  	defer tc.Cleanup()
    57  
    58  	_, err := kbtest.CreateAndSignupFakeUser("t", tc.G)
    59  	require.NoError(t, err)
    60  
    61  	user2, err := kbtest.CreateAndSignupFakeUser("t", tc.G)
    62  	require.NoError(t, err)
    63  	uv2 := user2.GetUserVersion()
    64  
    65  	// verify we can encrypt content with max length
    66  	pre := sampleNote()
    67  	note, err := libkb.RandString("", libkb.MaxStellarPaymentNoteLength)
    68  	require.NoError(t, err)
    69  	pre.Note = note[:libkb.MaxStellarPaymentNoteLength]
    70  	expect := pre.DeepCopy()
    71  	encNote, err := NoteEncryptB64(libkb.NewMetaContextForTest(tc), pre, &uv2)
    72  	require.NoError(t, err)
    73  	post, err := NoteDecryptB64(libkb.NewMetaContextForTest(tc), encNote)
    74  	require.NoError(t, err)
    75  	require.Equal(t, expect, post)
    76  
    77  	// encryption fails for content exceeding max length
    78  	pre.Note += "!"
    79  	encNote, err = NoteEncryptB64(libkb.NewMetaContextForTest(tc), pre, &uv2)
    80  	require.Error(t, err)
    81  	require.Equal(t, "", encNote)
    82  }
    83  
    84  func randomSymmetricKey(t testing.TB) libkb.NaclSecretBoxKey {
    85  	puk, err := libkb.GeneratePerUserKeySeed()
    86  	require.NoError(t, err)
    87  	symmetricKey, err := puk.DeriveSymmetricKey(libkb.DeriveReason("testing testing testing"))
    88  	require.NoError(t, err)
    89  	return symmetricKey
    90  }
    91  
    92  func sampleNote() stellar1.NoteContents {
    93  	return stellar1.NoteContents{
    94  		Note:      "wizbang",
    95  		StellarID: stellar1.TransactionID("6653fc2fdbc42ad51ccbe77ee0a3c29e258a5513c62fdc532cbfff91ab101abf"),
    96  	}
    97  }