github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/value/encryption/fernet/transformer_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package fernet
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"testing"
    24  
    25  	"github.com/fernet/fernet-go"
    26  	"github.com/golang/mock/gomock"
    27  	"github.com/google/go-cmp/cmp"
    28  )
    29  
    30  func Test_Transformer_Fernet_InvalidKey(t *testing.T) {
    31  	keys := []string{
    32  		"",
    33  		"foo",
    34  		"123456",
    35  	}
    36  	for _, k := range keys {
    37  		key := k
    38  		t.Run(fmt.Sprintf("key `%s`", key), func(t *testing.T) {
    39  			underTest, err := Transformer(key)
    40  			if err == nil {
    41  				t.Fatalf("Transformer should raise an error with key `%s`", key)
    42  			}
    43  			if underTest != nil {
    44  				t.Fatalf("Transformer instance should be nil")
    45  			}
    46  		})
    47  	}
    48  }
    49  
    50  func Test_Transformer_Fernet_From(t *testing.T) {
    51  	// Generate a random fernet key
    52  	k := &fernet.Key{}
    53  	if err := k.Generate(); err != nil {
    54  		t.Fatalf("unable to generate fernet key: %v", err)
    55  	}
    56  
    57  	// Prepare valid dataset
    58  	plainText := []byte("cool-protected-data")
    59  	encrypted, err := fernet.EncryptAndSign(plainText, k)
    60  	if err != nil {
    61  		t.Fatalf("unable to encrypt data with fernet key: %v", err)
    62  	}
    63  
    64  	// Prepare testcases
    65  	testCases := []struct {
    66  		name    string
    67  		input   []byte
    68  		wantErr bool
    69  		want    []byte
    70  	}{
    71  		{
    72  			name:    "Invalid encrypted payload",
    73  			input:   []byte("bad-encryption-payload"),
    74  			wantErr: true,
    75  		},
    76  		{
    77  			name:    "Valid payload",
    78  			input:   encrypted,
    79  			wantErr: false,
    80  			want:    plainText,
    81  		},
    82  	}
    83  
    84  	// For each testcase
    85  	for _, tc := range testCases {
    86  		testCase := tc
    87  		t.Run(testCase.name, func(t *testing.T) {
    88  			t.Parallel()
    89  
    90  			ctrl := gomock.NewController(t)
    91  			defer ctrl.Finish()
    92  
    93  			// Initialize mock
    94  			ctx := context.Background()
    95  
    96  			// Initialize transformer
    97  			underTest, err := Transformer(k.Encode())
    98  			if err != nil {
    99  				t.Fatalf("unable to initialize transformer: %v", err)
   100  			}
   101  
   102  			// Do the call
   103  			got, err := underTest.From(ctx, testCase.input)
   104  
   105  			// Assert results expectations
   106  			if (err != nil) != testCase.wantErr {
   107  				t.Errorf("error during the call, error = %v, wantErr %v", err, testCase.wantErr)
   108  				return
   109  			}
   110  			if testCase.wantErr {
   111  				return
   112  			}
   113  			if diff := cmp.Diff(got, testCase.want); diff != "" {
   114  				t.Errorf("%q. Fernet.From():\n-got/+want\ndiff %s", testCase.name, diff)
   115  			}
   116  		})
   117  	}
   118  }
   119  
   120  func Test_Transformer_Fernet_To(t *testing.T) {
   121  	// Generate a random fernet key
   122  	k := &fernet.Key{}
   123  	if err := k.Generate(); err != nil {
   124  		t.Fatalf("unable to generate fernet key: %v", err)
   125  	}
   126  
   127  	t.Log(k.Encode())
   128  
   129  	// Prepare valid dataset
   130  	plainText := []byte("cool-protected-data")
   131  
   132  	// Prepare testcases
   133  	testCases := []struct {
   134  		name    string
   135  		input   []byte
   136  		wantErr bool
   137  		want    []byte
   138  	}{
   139  		{
   140  			name:    "Valid payload",
   141  			input:   plainText,
   142  			wantErr: false,
   143  		},
   144  	}
   145  
   146  	// For each testcase
   147  	for _, tc := range testCases {
   148  		testCase := tc
   149  		t.Run(testCase.name, func(t *testing.T) {
   150  			t.Parallel()
   151  
   152  			ctrl := gomock.NewController(t)
   153  			defer ctrl.Finish()
   154  
   155  			// Initialize mock
   156  			ctx := context.Background()
   157  
   158  			// Initialize transformer
   159  			underTest, err := Transformer(k.Encode())
   160  			if err != nil {
   161  				t.Fatalf("unable to initialize transformer: %v", err)
   162  			}
   163  
   164  			// Do the call
   165  			got, err := underTest.To(ctx, testCase.input)
   166  
   167  			// Assert results expectations
   168  			if (err != nil) != testCase.wantErr {
   169  				t.Errorf("error during the call, error = %v, wantErr %v", err, testCase.wantErr)
   170  				return
   171  			}
   172  			if testCase.wantErr {
   173  				return
   174  			}
   175  			out, err := underTest.From(ctx, got)
   176  			if err != nil {
   177  				t.Errorf("error during the Fernet.From() call, error = %v", err)
   178  			}
   179  			if diff := cmp.Diff(out, testCase.input); diff != "" {
   180  				t.Errorf("%q. Fernet.To():\n-got/+want\ndiff %s", testCase.name, diff)
   181  			}
   182  		})
   183  	}
   184  }