github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/kms/dek_test.go (about) 1 // Copyright (c) 2015-2021 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package kms 19 20 import ( 21 "bytes" 22 "encoding/base64" 23 "testing" 24 ) 25 26 var dekEncodeDecodeTests = []struct { 27 Key DEK 28 }{ 29 { 30 Key: DEK{}, 31 }, 32 { 33 Key: DEK{ 34 Plaintext: nil, 35 Ciphertext: mustDecodeB64("eyJhZWFkIjoiQUVTLTI1Ni1HQ00tSE1BQy1TSEEtMjU2IiwiaXYiOiJ3NmhLUFVNZXVtejZ5UlVZL29pTFVBPT0iLCJub25jZSI6IktMSEU3UE1jRGo2N2UweHkiLCJieXRlcyI6Ik1wUkhjQWJaTzZ1Sm5lUGJGcnpKTkxZOG9pdkxwTmlUcTNLZ0hWdWNGYkR2Y0RlbEh1c1lYT29zblJWVTZoSXIifQ=="), 36 }, 37 }, 38 { 39 Key: DEK{ 40 Plaintext: mustDecodeB64("GM2UvLXp/X8lzqq0mibFC0LayDCGlmTHQhYLj7qAy7Q="), 41 Ciphertext: mustDecodeB64("eyJhZWFkIjoiQUVTLTI1Ni1HQ00tSE1BQy1TSEEtMjU2IiwiaXYiOiJ3NmhLUFVNZXVtejZ5UlVZL29pTFVBPT0iLCJub25jZSI6IktMSEU3UE1jRGo2N2UweHkiLCJieXRlcyI6Ik1wUkhjQWJaTzZ1Sm5lUGJGcnpKTkxZOG9pdkxwTmlUcTNLZ0hWdWNGYkR2Y0RlbEh1c1lYT29zblJWVTZoSXIifQ=="), 42 }, 43 }, 44 } 45 46 func TestEncodeDecodeDEK(t *testing.T) { 47 for i, test := range dekEncodeDecodeTests { 48 text, err := test.Key.MarshalText() 49 if err != nil { 50 t.Fatalf("Test %d: failed to marshal DEK: %v", i, err) 51 } 52 53 var key DEK 54 if err = key.UnmarshalText(text); err != nil { 55 t.Fatalf("Test %d: failed to unmarshal DEK: %v", i, err) 56 } 57 if key.Plaintext != nil { 58 t.Fatalf("Test %d: unmarshaled DEK contains non-nil plaintext", i) 59 } 60 if !bytes.Equal(key.Ciphertext, test.Key.Ciphertext) { 61 t.Fatalf("Test %d: ciphertext mismatch: got %x - want %x", i, key.Ciphertext, test.Key.Ciphertext) 62 } 63 } 64 } 65 66 func mustDecodeB64(s string) []byte { 67 b, err := base64.StdEncoding.DecodeString(s) 68 if err != nil { 69 panic(err) 70 } 71 return b 72 }