github.com/ethersphere/bee/v2@v2.2.0/pkg/encryption/mock/mock_test.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mock_test
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"testing"
    11  
    12  	"github.com/ethersphere/bee/v2/pkg/encryption/mock"
    13  )
    14  
    15  var errTest = errors.New("test error")
    16  
    17  func TestEncryptor_Encrypt(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	for _, tc := range []struct {
    21  		name    string
    22  		options []mock.Option
    23  		data    []byte
    24  		want    []byte
    25  		wantErr error
    26  	}{
    27  		{
    28  			name:    "empty",
    29  			wantErr: mock.ErrNotImplemented,
    30  		},
    31  		{
    32  			name: "func constant",
    33  			options: []mock.Option{mock.WithEncryptFunc(func([]byte) ([]byte, error) {
    34  				return []byte("some random data"), nil
    35  			})},
    36  			want: []byte("some random data"),
    37  		},
    38  		{
    39  			name: "func identity",
    40  			data: []byte("input data"),
    41  			options: []mock.Option{mock.WithEncryptFunc(func(data []byte) ([]byte, error) {
    42  				return data, nil
    43  			})},
    44  			want: []byte("input data"),
    45  		},
    46  		{
    47  			name: "func err",
    48  			options: []mock.Option{mock.WithEncryptFunc(func([]byte) ([]byte, error) {
    49  				return nil, errTest
    50  			})},
    51  			wantErr: errTest,
    52  		},
    53  		{
    54  			name:    "xor",
    55  			data:    []byte("input data"),
    56  			options: []mock.Option{mock.WithXOREncryption([]byte("the key"))},
    57  			want:    []byte{0x1d, 0x6, 0x15, 0x55, 0x1f, 0x45, 0x1d, 0x15, 0x1c, 0x4},
    58  		},
    59  		{
    60  			name:    "xor error",
    61  			options: []mock.Option{mock.WithXOREncryption(nil)},
    62  			wantErr: mock.ErrInvalidXORKey,
    63  		},
    64  	} {
    65  		tc := tc
    66  		t.Run(tc.name, func(t *testing.T) {
    67  			t.Parallel()
    68  
    69  			got, err := mock.New(tc.options...).Encrypt(tc.data)
    70  			if !errors.Is(err, tc.wantErr) {
    71  				t.Fatalf("got error %v, want %v", err, tc.wantErr)
    72  			}
    73  			if !bytes.Equal(got, tc.want) {
    74  				t.Errorf("got data %#v, want %#v", got, tc.want)
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  func TestEncryptor_Decrypt(t *testing.T) {
    81  	t.Parallel()
    82  
    83  	for _, tc := range []struct {
    84  		name    string
    85  		options []mock.Option
    86  		data    []byte
    87  		want    []byte
    88  		wantErr error
    89  	}{
    90  		{
    91  			name:    "empty",
    92  			wantErr: mock.ErrNotImplemented,
    93  		},
    94  		{
    95  			name: "func constant",
    96  			options: []mock.Option{mock.WithDecryptFunc(func([]byte) ([]byte, error) {
    97  				return []byte("some random data"), nil
    98  			})},
    99  			want: []byte("some random data"),
   100  		},
   101  		{
   102  			name: "func identity",
   103  			data: []byte("input data"),
   104  			options: []mock.Option{mock.WithDecryptFunc(func(data []byte) ([]byte, error) {
   105  				return data, nil
   106  			})},
   107  			want: []byte("input data"),
   108  		},
   109  		{
   110  			name: "func err",
   111  			options: []mock.Option{mock.WithDecryptFunc(func([]byte) ([]byte, error) {
   112  				return nil, errTest
   113  			})},
   114  			wantErr: errTest,
   115  		},
   116  		{
   117  			name:    "xor",
   118  			data:    []byte("input data"),
   119  			options: []mock.Option{mock.WithXOREncryption([]byte("the key"))},
   120  			want:    []byte{0x1d, 0x6, 0x15, 0x55, 0x1f, 0x45, 0x1d, 0x15, 0x1c, 0x4},
   121  		},
   122  		{
   123  			name:    "xor error",
   124  			options: []mock.Option{mock.WithXOREncryption(nil)},
   125  			wantErr: mock.ErrInvalidXORKey,
   126  		},
   127  	} {
   128  		tc := tc
   129  		t.Run(tc.name, func(t *testing.T) {
   130  			t.Parallel()
   131  
   132  			got, err := mock.New(tc.options...).Decrypt(tc.data)
   133  			if !errors.Is(err, tc.wantErr) {
   134  				t.Fatalf("got error %v, want %v", err, tc.wantErr)
   135  			}
   136  			if !bytes.Equal(got, tc.want) {
   137  				t.Errorf("got data %#v, want %#v", got, tc.want)
   138  			}
   139  		})
   140  	}
   141  }
   142  
   143  func TestEncryptor_Reset(t *testing.T) {
   144  	t.Parallel()
   145  
   146  	t.Run("empty", func(t *testing.T) {
   147  		t.Parallel()
   148  
   149  		// should not panic
   150  		mock.New().Reset()
   151  	})
   152  	t.Run("func", func(t *testing.T) {
   153  		t.Parallel()
   154  
   155  		var called bool
   156  		mock.New(mock.WithResetFunc(func() {
   157  			called = true
   158  		})).Reset()
   159  		if !called {
   160  			t.Error("reset func not called")
   161  		}
   162  	})
   163  }
   164  
   165  func TestEncryptor_XOREncryption(t *testing.T) {
   166  	t.Parallel()
   167  
   168  	key := []byte("some strong key")
   169  
   170  	e := mock.New(mock.WithXOREncryption(key))
   171  
   172  	data := []byte("very secret data")
   173  
   174  	enc, err := e.Encrypt(data)
   175  	if err != nil {
   176  		t.Fatal(err)
   177  	}
   178  
   179  	if bytes.Equal(enc, data) {
   180  		t.Fatal("encrypted and input data must not be the same")
   181  	}
   182  
   183  	dec, err := e.Decrypt(enc)
   184  	if err != nil {
   185  		t.Fatal(err)
   186  	}
   187  
   188  	if !bytes.Equal(dec, data) {
   189  		t.Errorf("got decrypted data %#v, want %#v", dec, data)
   190  	}
   191  }