storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/kms/dek_test.go (about) 1 // MinIO Cloud Storage, (C) 2021 MinIO, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package kms 16 17 import ( 18 "bytes" 19 "encoding/base64" 20 "testing" 21 ) 22 23 var dekEncodeDecodeTests = []struct { 24 Key DEK 25 }{ 26 { 27 Key: DEK{}, 28 }, 29 { 30 Key: DEK{ 31 Plaintext: nil, 32 Ciphertext: mustDecodeB64("eyJhZWFkIjoiQUVTLTI1Ni1HQ00tSE1BQy1TSEEtMjU2IiwiaXYiOiJ3NmhLUFVNZXVtejZ5UlVZL29pTFVBPT0iLCJub25jZSI6IktMSEU3UE1jRGo2N2UweHkiLCJieXRlcyI6Ik1wUkhjQWJaTzZ1Sm5lUGJGcnpKTkxZOG9pdkxwTmlUcTNLZ0hWdWNGYkR2Y0RlbEh1c1lYT29zblJWVTZoSXIifQ=="), 33 }, 34 }, 35 { 36 Key: DEK{ 37 Plaintext: mustDecodeB64("GM2UvLXp/X8lzqq0mibFC0LayDCGlmTHQhYLj7qAy7Q="), 38 Ciphertext: mustDecodeB64("eyJhZWFkIjoiQUVTLTI1Ni1HQ00tSE1BQy1TSEEtMjU2IiwiaXYiOiJ3NmhLUFVNZXVtejZ5UlVZL29pTFVBPT0iLCJub25jZSI6IktMSEU3UE1jRGo2N2UweHkiLCJieXRlcyI6Ik1wUkhjQWJaTzZ1Sm5lUGJGcnpKTkxZOG9pdkxwTmlUcTNLZ0hWdWNGYkR2Y0RlbEh1c1lYT29zblJWVTZoSXIifQ=="), 39 }, 40 }, 41 } 42 43 func TestEncodeDecodeDEK(t *testing.T) { 44 for i, test := range dekEncodeDecodeTests { 45 text, err := test.Key.MarshalText() 46 if err != nil { 47 t.Fatalf("Test %d: failed to marshal DEK: %v", i, err) 48 } 49 50 var key DEK 51 if err = key.UnmarshalText(text); err != nil { 52 t.Fatalf("Test %d: failed to unmarshal DEK: %v", i, err) 53 } 54 if key.Plaintext != nil { 55 t.Fatalf("Test %d: unmarshaled DEK contains non-nil plaintext", i) 56 } 57 if !bytes.Equal(key.Ciphertext, test.Key.Ciphertext) { 58 t.Fatalf("Test %d: ciphertext mismatch: got %x - want %x", i, key.Ciphertext, test.Key.Ciphertext) 59 } 60 } 61 } 62 63 func mustDecodeB64(s string) []byte { 64 b, err := base64.StdEncoding.DecodeString(s) 65 if err != nil { 66 panic(err) 67 } 68 return b 69 }