github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/value/encryption/jwe/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 jwe 19 20 import ( 21 "context" 22 "encoding/base64" 23 "reflect" 24 "testing" 25 26 "gopkg.in/square/go-jose.v2" 27 ) 28 29 func mustDecodeBase64(in string) []byte { 30 out, err := base64.URLEncoding.DecodeString(in) 31 if err != nil { 32 panic(err) 33 } 34 35 return out 36 } 37 38 func Test_jweTransformer_To(t *testing.T) { 39 type fields struct { 40 key interface{} 41 keyAlgorithm jose.KeyAlgorithm 42 contentEncryption jose.ContentEncryption 43 } 44 type args struct { 45 in0 context.Context 46 input []byte 47 } 48 tests := []struct { 49 name string 50 fields fields 51 args args 52 want []byte 53 wantErr bool 54 }{ 55 { 56 name: "nil", 57 wantErr: true, 58 }, 59 { 60 name: "a128kw", 61 fields: fields{ 62 key: []byte("deterministic-key-for-test-00001"), 63 keyAlgorithm: jose.A128KW, 64 contentEncryption: jose.A128GCM, 65 }, 66 args: args{ 67 input: []byte("cleartext message"), 68 }, 69 wantErr: false, 70 }, 71 { 72 name: "a192kw", 73 fields: fields{ 74 key: []byte("deterministic-key-for-test-00001"), 75 keyAlgorithm: jose.A192KW, 76 contentEncryption: jose.A128GCM, 77 }, 78 args: args{ 79 input: []byte("cleartext message"), 80 }, 81 wantErr: false, 82 }, 83 { 84 name: "a256kw", 85 fields: fields{ 86 key: []byte("deterministic-key-for-test-00001"), 87 keyAlgorithm: jose.A256KW, 88 contentEncryption: jose.A256GCM, 89 }, 90 args: args{ 91 input: []byte("cleartext message"), 92 }, 93 wantErr: false, 94 }, 95 { 96 name: "pbes2-hs256-a128kw", 97 fields: fields{ 98 key: []byte("deterministic-key-for-test-0001"), 99 keyAlgorithm: jose.PBES2_HS256_A128KW, 100 contentEncryption: jose.A128GCM, 101 }, 102 args: args{ 103 input: []byte("cleartext message"), 104 }, 105 wantErr: false, 106 }, 107 { 108 name: "pbes2-hs384-a192kw", 109 fields: fields{ 110 key: []byte("deterministic-key-for-test-0001"), 111 keyAlgorithm: jose.PBES2_HS384_A192KW, 112 contentEncryption: jose.A192GCM, 113 }, 114 args: args{ 115 input: []byte("cleartext message"), 116 }, 117 wantErr: false, 118 }, 119 { 120 name: "pbes2-hs512-a256kw", 121 fields: fields{ 122 key: []byte("deterministic-key-for-test-0001"), 123 keyAlgorithm: jose.PBES2_HS512_A256KW, 124 contentEncryption: jose.A256GCM, 125 }, 126 args: args{ 127 input: []byte("cleartext message"), 128 }, 129 wantErr: false, 130 }, 131 } 132 for _, tt := range tests { 133 t.Run(tt.name, func(t *testing.T) { 134 d := &jweTransformer{ 135 key: tt.fields.key, 136 keyAlgorithm: tt.fields.keyAlgorithm, 137 contentEncryption: tt.fields.contentEncryption, 138 } 139 _, err := d.To(tt.args.in0, tt.args.input) 140 if (err != nil) != tt.wantErr { 141 t.Errorf("jweTransformer.To() error = %v, wantErr %v", err, tt.wantErr) 142 return 143 } 144 }) 145 } 146 } 147 148 func Test_jweTransformer_From(t *testing.T) { 149 type fields struct { 150 key interface{} 151 keyAlgorithm jose.KeyAlgorithm 152 contentEncryption jose.ContentEncryption 153 } 154 type args struct { 155 input []byte 156 } 157 tests := []struct { 158 name string 159 fields fields 160 args args 161 want []byte 162 wantErr bool 163 }{ 164 { 165 name: "nil", 166 wantErr: true, 167 }, 168 { 169 name: "empty", 170 fields: fields{ 171 key: (""), 172 }, 173 args: args{ 174 input: []byte{}, 175 }, 176 wantErr: true, 177 }, 178 // --------------------------------------------------------------------- 179 { 180 name: "valid", 181 fields: fields{ 182 key: mustDecodeBase64("abSOB6OHnFK1CHIm60OXsA=="), 183 keyAlgorithm: jose.A128KW, 184 contentEncryption: jose.A128GCM, 185 }, 186 args: args{ 187 input: []byte("eyJhbGciOiJBMTI4S1ciLCJlbmMiOiJBMTI4R0NNIn0.22-PjbJqsJ6TFVLhPwYJG3a0HZq0cAcf.zWKWg_GfycXIrVa9.6XvjKMvr2CjG.pcMO_ou5QqTa6u6PzDWFIg"), 188 }, 189 wantErr: false, 190 want: []byte("cleartext"), 191 }, 192 } 193 for _, tt := range tests { 194 t.Run(tt.name, func(t *testing.T) { 195 d := &jweTransformer{ 196 key: tt.fields.key, 197 keyAlgorithm: tt.fields.keyAlgorithm, 198 contentEncryption: tt.fields.contentEncryption, 199 } 200 got, err := d.From(context.Background(), tt.args.input) 201 if (err != nil) != tt.wantErr { 202 t.Errorf("jweTransformer.From() error = %v, wantErr %v", err, tt.wantErr) 203 return 204 } 205 if !reflect.DeepEqual(got, tt.want) { 206 t.Errorf("jweTransformer.From() = %v, want %v", got, tt.want) 207 } 208 }) 209 } 210 }