github.com/0chain/gosdk@v1.17.11/zboxcore/zboxutil/util_test.go (about)

     1  package zboxutil
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestScryptEncryption(t *testing.T) {
    10  	t.Parallel()
    11  
    12  	testCases := []struct {
    13  		name      string
    14  		key       string
    15  		plaintext string
    16  		wantErr   bool
    17  	}{
    18  		{
    19  			name:      "valid plaintext",
    20  			key:       "passphrase1111111111111111111111",
    21  			plaintext: "glare mistake gun joke bid spare across diagram wrap cube swear cactus cave repeat you brave few best wild lion pitch pole original wasp",
    22  			wantErr:   false,
    23  		},
    24  		{
    25  			name:      "empty key",
    26  			key:       "",
    27  			plaintext: "glare mistake gun joke bid spare across diagram wrap cube swear cactus cave repeat you brave few best wild lion pitch pole original wasp",
    28  			wantErr:   true,
    29  		},
    30  		{
    31  			name:      "empty plaintext",
    32  			key:       "passphrase1111111111111111111111",
    33  			plaintext: "",
    34  			wantErr:   true,
    35  		},
    36  	}
    37  
    38  	for _, tc := range testCases {
    39  		tc := tc
    40  		t.Run(tc.name, func(t *testing.T) {
    41  			t.Parallel()
    42  
    43  			key := []byte(tc.key)
    44  			plaintext := []byte(tc.plaintext)
    45  
    46  			// Encrypt plaintext
    47  			ciphertext, err := ScryptEncrypt(key, plaintext)
    48  
    49  			if tc.wantErr {
    50  				require.Error(t, err)
    51  				require.Nil(t, ciphertext)
    52  			} else {
    53  				require.NoError(t, err)
    54  				require.NotNil(t, ciphertext)
    55  
    56  				// Decrypt ciphertext
    57  				decryptedText, err := ScryptDecrypt(key, ciphertext)
    58  				require.NoError(t, err)
    59  				require.Equal(t, plaintext, decryptedText)
    60  			}
    61  		})
    62  	}
    63  }