github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/value/encryption/age/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 age
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"testing"
    24  
    25  	"github.com/golang/mock/gomock"
    26  	"github.com/google/go-cmp/cmp"
    27  )
    28  
    29  func Test_Transformer_Age_InvalidKey(t *testing.T) {
    30  	keys := []string{
    31  		"",
    32  		"foo",
    33  		"123456",
    34  	}
    35  	for _, k := range keys {
    36  		key := k
    37  		t.Run(fmt.Sprintf("key `%s`", key), func(t *testing.T) {
    38  			underTest, err := Transformer(key)
    39  			if err == nil {
    40  				t.Fatalf("Transformer should raise an error with key `%s`", key)
    41  			}
    42  			if underTest != nil {
    43  				t.Fatalf("Transformer instance should be nil")
    44  			}
    45  		})
    46  	}
    47  }
    48  
    49  func Test_Transformer_From(t *testing.T) {
    50  	identity := `age-identity:AGE-SECRET-KEY-1W8E69DQEVASNK68FX7C6QLD99KTG96RHWW0EZ3RD0L29AHV4S84QHUAP4C`
    51  	// recipient := `age-recipients:age1ce20pmz8z0ue97v7rz838v6pcpvzqan30lr40tjlzy40ez8eldrqf2zuxe`
    52  
    53  	// Prepare testcases
    54  	testCases := []struct {
    55  		name    string
    56  		input   []byte
    57  		wantErr bool
    58  		want    []byte
    59  	}{
    60  		{
    61  			name:    "Invalid encrypted payload",
    62  			input:   []byte("bad-encryption-payload"),
    63  			wantErr: true,
    64  		},
    65  		{
    66  			name: "valid",
    67  			input: []byte(`-----BEGIN AGE ENCRYPTED FILE-----
    68  YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBFeERxdmZ1SzAzZmVsSWtz
    69  Si9XMVNaV2tLcEdCZUdOQy9RL2dmTnZsbGk4Ci9mcE1DVTZWQTdaenRhSVdQMjZP
    70  ZGpCbmZiUkgzY0NXQk8rT25yQjA1b2sKLS0tIFJNT1R1WlZOTEpnZUNVRTFLZ3Nq
    71  bkZqYUc4NHl2VGJuQU1SeEd5bkFoWmcKV6dwry0emJOt19Jh5IPGKiOzYmJ6AVA2
    72  7aMKPOZ+rMNcgne6
    73  -----END AGE ENCRYPTED FILE-----`),
    74  			wantErr: false,
    75  			want:    []byte("test"),
    76  		},
    77  	}
    78  
    79  	// For each testcase
    80  	for _, tc := range testCases {
    81  		testCase := tc
    82  		t.Run(testCase.name, func(t *testing.T) {
    83  			t.Parallel()
    84  
    85  			ctrl := gomock.NewController(t)
    86  			defer ctrl.Finish()
    87  
    88  			// Initialize mock
    89  			ctx := context.Background()
    90  
    91  			// Initialize transformer
    92  			underTest, err := Transformer(identity)
    93  			if err != nil {
    94  				t.Fatalf("unable to initialize transformer: %v", err)
    95  			}
    96  
    97  			// Do the call
    98  			got, err := underTest.From(ctx, testCase.input)
    99  
   100  			// Assert results expectations
   101  			if (err != nil) != testCase.wantErr {
   102  				t.Errorf("error during the call, error = %v, wantErr %v", err, testCase.wantErr)
   103  				return
   104  			}
   105  			if testCase.wantErr {
   106  				return
   107  			}
   108  			if diff := cmp.Diff(got, testCase.want); diff != "" {
   109  				t.Errorf("%q. Age.From():\n-got/+want\ndiff %s", testCase.name, diff)
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  func Test_Transformer_To(t *testing.T) {
   116  	identity := `age-identity:AGE-SECRET-KEY-1W8E69DQEVASNK68FX7C6QLD99KTG96RHWW0EZ3RD0L29AHV4S84QHUAP4C`
   117  	recipient := `age-recipients:age1ce20pmz8z0ue97v7rz838v6pcpvzqan30lr40tjlzy40ez8eldrqf2zuxe`
   118  
   119  	// Prepare testcases
   120  	testCases := []struct {
   121  		name    string
   122  		input   []byte
   123  		wantErr bool
   124  		want    []byte
   125  	}{
   126  		{
   127  			name:    "Valid payload",
   128  			input:   []byte("test"),
   129  			wantErr: false,
   130  		},
   131  	}
   132  
   133  	// For each testcase
   134  	for _, tc := range testCases {
   135  		testCase := tc
   136  		t.Run(testCase.name, func(t *testing.T) {
   137  			t.Parallel()
   138  
   139  			ctrl := gomock.NewController(t)
   140  			defer ctrl.Finish()
   141  
   142  			// Initialize mock
   143  			ctx := context.Background()
   144  
   145  			// Initialize transformer
   146  			encrypt, err := Transformer(recipient)
   147  			if err != nil {
   148  				t.Fatalf("unable to initialize transformer: %v", err)
   149  			}
   150  
   151  			// Do the call
   152  			got, err := encrypt.To(ctx, testCase.input)
   153  
   154  			// Assert results expectations
   155  			if (err != nil) != testCase.wantErr {
   156  				t.Errorf("error during the call, error = %v, wantErr %v", err, testCase.wantErr)
   157  				return
   158  			}
   159  			if testCase.wantErr {
   160  				return
   161  			}
   162  
   163  			// Initialize transformer
   164  			decrypt, err := Transformer(identity)
   165  			if err != nil {
   166  				t.Fatalf("unable to initialize transformer: %v", err)
   167  			}
   168  
   169  			out, err := decrypt.From(ctx, got)
   170  			if err != nil {
   171  				t.Errorf("error during the Age.From() call, error = %v", err)
   172  			}
   173  			if diff := cmp.Diff(out, testCase.input); diff != "" {
   174  				t.Errorf("%q. Age.To():\n-got/+want\ndiff %s", testCase.name, diff)
   175  			}
   176  		})
   177  	}
   178  }