github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/value/encryption/paseto/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 paseto 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_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 // Prepare testcases 51 testCases := []struct { 52 name string 53 input []byte 54 wantErr bool 55 want []byte 56 }{ 57 { 58 name: "Invalid encrypted payload", 59 input: []byte("bad-encryption-payload"), 60 wantErr: true, 61 }, 62 { 63 name: "Valid payload", 64 input: []byte("v4.local.tMe_MuiltiVR4NlnbtCiXP7w3v2rkE3iLpOJG4Gyfxc3UTHIbHzKIrRu0e8Mb_Q93kXTm99GU5AjquJalAG8qTp7fxs"), 65 wantErr: false, 66 want: []byte("test"), 67 }, 68 } 69 70 // For each testcase 71 for _, tc := range testCases { 72 testCase := tc 73 t.Run(testCase.name, func(t *testing.T) { 74 t.Parallel() 75 76 ctrl := gomock.NewController(t) 77 defer ctrl.Finish() 78 79 // Initialize mock 80 ctx := context.Background() 81 82 // Initialize transformer 83 underTest, err := Transformer("kP1yHnBcOhjowNFXSCyycSuXdUqTlbuE6ES5tTp-I_o=") 84 if err != nil { 85 t.Fatalf("unable to initialize transformer: %v", err) 86 } 87 88 // Do the call 89 got, err := underTest.From(ctx, testCase.input) 90 91 // Assert results expectations 92 if (err != nil) != testCase.wantErr { 93 t.Errorf("error during the call, error = %v, wantErr %v", err, testCase.wantErr) 94 return 95 } 96 if testCase.wantErr { 97 return 98 } 99 if diff := cmp.Diff(got, testCase.want); diff != "" { 100 t.Errorf("%q. Paseto.From():\n-got/+want\ndiff %s", testCase.name, diff) 101 } 102 }) 103 } 104 } 105 106 func Test_Transformer_To(t *testing.T) { 107 // Prepare testcases 108 testCases := []struct { 109 name string 110 input []byte 111 wantErr bool 112 want []byte 113 }{ 114 { 115 name: "Valid payload", 116 input: []byte("test"), 117 wantErr: false, 118 }, 119 } 120 121 // For each testcase 122 for _, tc := range testCases { 123 testCase := tc 124 t.Run(testCase.name, func(t *testing.T) { 125 t.Parallel() 126 127 ctrl := gomock.NewController(t) 128 defer ctrl.Finish() 129 130 // Initialize mock 131 ctx := context.Background() 132 133 // Initialize transformer 134 underTest, err := Transformer("kP1yHnBcOhjowNFXSCyycSuXdUqTlbuE6ES5tTp-I_o=") 135 if err != nil { 136 t.Fatalf("unable to initialize transformer: %v", err) 137 } 138 139 // Do the call 140 got, err := underTest.To(ctx, testCase.input) 141 142 // Assert results expectations 143 if (err != nil) != testCase.wantErr { 144 t.Errorf("error during the call, error = %v, wantErr %v", err, testCase.wantErr) 145 return 146 } 147 if testCase.wantErr { 148 return 149 } 150 out, err := underTest.From(ctx, got) 151 if err != nil { 152 t.Errorf("error during the Paseto.From() call, error = %v", err) 153 } 154 if diff := cmp.Diff(out, testCase.input); diff != "" { 155 t.Errorf("%q. Paseto.To():\n-got/+want\ndiff %s", testCase.name, diff) 156 } 157 }) 158 } 159 }