github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/encryption-methods_test.go (about) 1 // Copyright (c) 2015-2024 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 cmd 19 20 import ( 21 "fmt" 22 "testing" 23 ) 24 25 func TestParseEncryptionKeys(t *testing.T) { 26 baseAlias := "mintest" 27 basePrefix := "two/layer/prefix" 28 baseObject := "object_name" 29 sseKey := "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDA" 30 sseKeyPlain := "01234567890123456789012345678900" 31 32 // INVALID KEYS 33 sseKeyInvalidShort := "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2" 34 sseKeyInvalidSymbols := "MDEyMzQ1Njc4O____jM0N!!!ODkwMTIzNDU2Nzg5MDA" 35 sseKeyInvalidSpaces := "MDE yMzQ1Njc4OTAxM jM0NTY3ODkwMTIzNDU2Nzg5MDA" 36 sseKeyInvalidPrefixSpace := " MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDA" 37 sseKeyInvalidOneShort := "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MD" 38 39 testCases := []struct { 40 encryptionKey string 41 keyPlain string 42 alias string 43 prefix string 44 object string 45 sseType sseKeyType 46 success bool 47 }{ 48 { 49 encryptionKey: fmt.Sprintf("%s/%s/%s=%s", baseAlias, basePrefix, baseObject, sseKey), 50 keyPlain: sseKeyPlain, 51 alias: baseAlias, 52 prefix: basePrefix, 53 object: baseObject, 54 success: true, 55 }, 56 { 57 encryptionKey: fmt.Sprintf("%s=/%s=/%s==%s", baseAlias, basePrefix, baseObject, sseKey), 58 keyPlain: sseKeyPlain, 59 alias: baseAlias + "=", 60 prefix: basePrefix + "=", 61 object: baseObject + "=", 62 success: true, 63 }, 64 { 65 encryptionKey: fmt.Sprintf("%s//%s//%s/=%s", baseAlias, basePrefix, baseObject, sseKey), 66 keyPlain: sseKeyPlain, 67 alias: baseAlias + "/", 68 prefix: basePrefix + "/", 69 object: baseObject + "/", 70 success: true, 71 }, 72 { 73 encryptionKey: fmt.Sprintf("%s/%s/%s==%s", baseAlias, basePrefix, baseObject, sseKey), 74 keyPlain: sseKeyPlain, 75 alias: baseAlias, 76 prefix: basePrefix, 77 object: baseObject + "=", 78 success: true, 79 }, 80 { 81 encryptionKey: fmt.Sprintf("%s/%s/%s!@_==_$^&*=%s", baseAlias, basePrefix, baseObject, sseKey), 82 keyPlain: sseKeyPlain, 83 alias: baseAlias, 84 prefix: basePrefix, 85 object: baseObject + "!@_==_$^&*", 86 success: true, 87 }, 88 { 89 encryptionKey: fmt.Sprintf("%s/%s/%s=%sXXXXX", baseAlias, basePrefix, baseObject, sseKey), 90 success: false, 91 }, 92 { 93 encryptionKey: fmt.Sprintf("%s/%s/%s=%s", baseAlias, basePrefix, baseObject, sseKeyInvalidShort), 94 success: false, 95 }, 96 { 97 encryptionKey: fmt.Sprintf("%s/%s/%s=%s", baseAlias, basePrefix, baseObject, sseKeyInvalidSymbols), 98 success: false, 99 }, 100 { 101 encryptionKey: fmt.Sprintf("%s/%s/%s=%s", baseAlias, basePrefix, baseObject, sseKeyInvalidSpaces), 102 success: false, 103 }, 104 { 105 encryptionKey: fmt.Sprintf("%s/%s/%s=%s", baseAlias, basePrefix, baseObject, sseKeyInvalidPrefixSpace), 106 success: false, 107 }, 108 { 109 encryptionKey: fmt.Sprintf("%s/%s/%s==%s", baseAlias, basePrefix, baseObject, sseKeyInvalidOneShort), 110 success: false, 111 }, 112 } 113 114 for i, tc := range testCases { 115 alias, prefix, key, err := parseSSEKey(tc.encryptionKey, sseC) 116 if tc.success { 117 if err != nil { 118 t.Fatalf("Test %d: Expected success, got %s", i+1, err) 119 } 120 if fmt.Sprintf("%s/%s", alias, prefix) != fmt.Sprintf("%s/%s/%s", tc.alias, tc.prefix, tc.object) { 121 t.Fatalf("Test %d: alias and prefix parsing was invalid, expected %s/%s/%s, got %s/%s", i, tc.alias, tc.prefix, tc.object, alias, prefix) 122 } 123 if key != tc.keyPlain { 124 t.Fatalf("Test %d: sse key parsing is invalid, expected %s, got %s", i, tc.keyPlain, key) 125 } 126 } 127 128 if !tc.success { 129 if err == nil { 130 t.Fatalf("Test %d: Expected error, got success", i+1) 131 } 132 } 133 } 134 }