github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/value/encryption/envelope/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 envelope
    19  
    20  import (
    21  	"context"
    22  	"encoding/base64"
    23  	"fmt"
    24  	"testing"
    25  
    26  	"github.com/google/go-cmp/cmp"
    27  
    28  	"github.com/zntrio/harp/v2/pkg/sdk/value"
    29  	"github.com/zntrio/harp/v2/pkg/sdk/value/encryption/secretbox"
    30  )
    31  
    32  type testEnvelopeService struct{}
    33  
    34  func (s *testEnvelopeService) Encrypt(_ context.Context, data []byte) ([]byte, error) {
    35  	return []byte(base64.URLEncoding.EncodeToString(data)), nil
    36  }
    37  
    38  func (s *testEnvelopeService) Decrypt(_ context.Context, data []byte) ([]byte, error) {
    39  	return base64.URLEncoding.DecodeString(string(data))
    40  }
    41  
    42  // -----------------------------------------------------------------------------
    43  
    44  func Test_Envelope_From(t *testing.T) {
    45  	testCases := []struct {
    46  		name    string
    47  		input   []byte
    48  		wantErr bool
    49  		want    []byte
    50  	}{
    51  		{
    52  			name:    "Nil input",
    53  			input:   nil,
    54  			wantErr: true,
    55  		},
    56  		{
    57  			name:    "Payload",
    58  			input:   []byte("foo"),
    59  			wantErr: true,
    60  		},
    61  	}
    62  
    63  	for _, tc := range testCases {
    64  		testCase := tc
    65  		t.Run(testCase.name, func(t *testing.T) {
    66  			t.Parallel()
    67  
    68  			// Arm mock
    69  			ctx := context.Background()
    70  			envelopeService := &testEnvelopeService{}
    71  
    72  			underTest, err := Transformer(envelopeService, secretbox.Transformer)
    73  			if err != nil {
    74  				t.Errorf("error during transformer initialization, error = %v", err)
    75  				return
    76  			}
    77  
    78  			// Do the call
    79  			got, err := underTest.From(ctx, testCase.input)
    80  
    81  			// Assert results expectations
    82  			if (err != nil) != testCase.wantErr {
    83  				t.Errorf("error during the call, error = %v, wantErr %v", err, testCase.wantErr)
    84  				return
    85  			}
    86  			if testCase.wantErr {
    87  				return
    88  			}
    89  			if diff := cmp.Diff(got, testCase.input); diff != "" {
    90  				t.Errorf("%q. Envelope.From():\n-got/+want\ndiff %s", testCase.name, diff)
    91  			}
    92  		})
    93  	}
    94  }
    95  
    96  func Test_Envelope_To_From(t *testing.T) {
    97  	testCases := []struct {
    98  		name    string
    99  		input   []byte
   100  		wantErr bool
   101  		want    []byte
   102  	}{
   103  		{
   104  			name:    "Nil input",
   105  			input:   nil,
   106  			wantErr: false,
   107  		},
   108  		{
   109  			name:    "Payload",
   110  			input:   []byte("foo"),
   111  			wantErr: false,
   112  		},
   113  	}
   114  
   115  	for _, tc := range testCases {
   116  		testCase := tc
   117  		t.Run(testCase.name, func(t *testing.T) {
   118  			t.Parallel()
   119  
   120  			// Arm mock
   121  			ctx := context.Background()
   122  			envelopeService := &testEnvelopeService{}
   123  
   124  			underTest, err := Transformer(envelopeService, secretbox.Transformer)
   125  			if err != nil {
   126  				t.Errorf("error during transformer initialization, error = %v", err)
   127  				return
   128  			}
   129  
   130  			// Do the call
   131  			got, err := underTest.To(ctx, testCase.input)
   132  
   133  			// Assert results expectations
   134  			if (err != nil) != testCase.wantErr {
   135  				t.Errorf("error during the call, error = %v, wantErr %v", err, testCase.wantErr)
   136  				return
   137  			}
   138  			if testCase.wantErr {
   139  				return
   140  			}
   141  
   142  			clearText, err := underTest.From(ctx, got)
   143  			if err != nil {
   144  				t.Errorf("error during the Fernet.From() call, error = %v", err)
   145  			}
   146  			if diff := cmp.Diff(clearText, testCase.input); diff != "" {
   147  				t.Errorf("%q. Envelope.To/From():\n-got/+want\ndiff %s", testCase.name, diff)
   148  			}
   149  		})
   150  	}
   151  }
   152  
   153  type testErrorEnvelopeService struct{}
   154  
   155  func (s *testErrorEnvelopeService) Encrypt(_ context.Context, data []byte) ([]byte, error) {
   156  	return nil, fmt.Errorf("foo")
   157  }
   158  
   159  func (s *testErrorEnvelopeService) Decrypt(_ context.Context, data []byte) ([]byte, error) {
   160  	return nil, fmt.Errorf("foo")
   161  }
   162  
   163  func Test_Envelope_Service_Error(t *testing.T) {
   164  	testCases := []struct {
   165  		name    string
   166  		input   []byte
   167  		wantErr bool
   168  		want    []byte
   169  	}{
   170  		{
   171  			name:    "Nil input",
   172  			input:   nil,
   173  			wantErr: true,
   174  		},
   175  		{
   176  			name:    "Payload",
   177  			input:   []byte("foo"),
   178  			wantErr: true,
   179  		},
   180  	}
   181  
   182  	for _, tc := range testCases {
   183  		testCase := tc
   184  		t.Run(testCase.name, func(t *testing.T) {
   185  			t.Parallel()
   186  
   187  			// Arm mock
   188  			ctx := context.Background()
   189  			envelopeService := &testErrorEnvelopeService{}
   190  
   191  			underTest, err := Transformer(envelopeService, secretbox.Transformer)
   192  			if err != nil {
   193  				t.Errorf("error during transformer initialization, error = %v", err)
   194  				return
   195  			}
   196  
   197  			// Do the call
   198  			got, err := underTest.To(ctx, testCase.input)
   199  
   200  			// Assert results expectations
   201  			if (err != nil) != testCase.wantErr {
   202  				t.Errorf("error during the call, error = %v, wantErr %v", err, testCase.wantErr)
   203  				return
   204  			}
   205  			if testCase.wantErr {
   206  				return
   207  			}
   208  
   209  			clearText, err := underTest.From(ctx, got)
   210  			if err != nil {
   211  				t.Errorf("error during the Fernet.From() call, error = %v", err)
   212  			}
   213  			if diff := cmp.Diff(clearText, testCase.input); diff != "" {
   214  				t.Errorf("%q. Envelope.To/From():\n-got/+want\ndiff %s", testCase.name, diff)
   215  			}
   216  		})
   217  	}
   218  }
   219  
   220  func Test_Envelope_Transformer_Error(t *testing.T) {
   221  	testCases := []struct {
   222  		name    string
   223  		input   []byte
   224  		wantErr bool
   225  		want    []byte
   226  	}{
   227  		{
   228  			name:    "Nil input",
   229  			input:   nil,
   230  			wantErr: true,
   231  		},
   232  		{
   233  			name:    "Payload",
   234  			input:   []byte("foo"),
   235  			wantErr: true,
   236  		},
   237  	}
   238  
   239  	for _, tc := range testCases {
   240  		testCase := tc
   241  		t.Run(testCase.name, func(t *testing.T) {
   242  			t.Parallel()
   243  
   244  			// Arm mock
   245  			ctx := context.Background()
   246  			envelopeService := &testEnvelopeService{}
   247  
   248  			underTest, err := Transformer(envelopeService, func(string) (value.Transformer, error) {
   249  				return nil, fmt.Errorf("foo")
   250  			})
   251  			if err != nil {
   252  				t.Errorf("error during transformer initialization, error = %v", err)
   253  				return
   254  			}
   255  
   256  			// Do the call
   257  			got, err := underTest.To(ctx, testCase.input)
   258  
   259  			// Assert results expectations
   260  			if (err != nil) != testCase.wantErr {
   261  				t.Errorf("error during the call, error = %v, wantErr %v", err, testCase.wantErr)
   262  				return
   263  			}
   264  			if testCase.wantErr {
   265  				return
   266  			}
   267  
   268  			clearText, err := underTest.From(ctx, got)
   269  			if err != nil {
   270  				t.Errorf("error during the Fernet.From() call, error = %v", err)
   271  			}
   272  			if diff := cmp.Diff(clearText, testCase.input); diff != "" {
   273  				t.Errorf("%q. Envelope.To/From():\n-got/+want\ndiff %s", testCase.name, diff)
   274  			}
   275  		})
   276  	}
   277  }