storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/madmin/encrypt_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package madmin
    19  
    20  import (
    21  	"bytes"
    22  	"encoding/hex"
    23  	"fmt"
    24  	"testing"
    25  )
    26  
    27  var encryptDataTests = []struct {
    28  	Password string
    29  	Data     []byte
    30  }{
    31  	{Password: "", Data: nil},
    32  	{Password: "", Data: make([]byte, 256)},
    33  	{Password: `xPl.8/rhR"Q_1xLt`, Data: make([]byte, 32)},
    34  	{Password: "m69?yz4W-!k+7p0", Data: make([]byte, 1024*1024)},
    35  	{Password: `7h5oU4$te{;K}fgqlI^]`, Data: make([]byte, 256)},
    36  }
    37  
    38  func TestEncryptData(t *testing.T) {
    39  	for i, test := range encryptDataTests {
    40  		i, test := i, test
    41  		t.Run(fmt.Sprintf("Test-%d", i), func(t *testing.T) {
    42  			ciphertext, err := EncryptData(test.Password, test.Data)
    43  			if err != nil {
    44  				t.Fatalf("Failed to encrypt data: %v", err)
    45  			}
    46  
    47  			plaintext, err := DecryptData(test.Password, bytes.NewReader(ciphertext))
    48  			if err != nil {
    49  				t.Fatalf("Failed to decrypt data: %v", err)
    50  			}
    51  			if !bytes.Equal(plaintext, test.Data) {
    52  				t.Fatal("Decrypt plaintext does not match origin data")
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  var decryptDataTests = []struct {
    59  	Password string
    60  	Data     string
    61  }{
    62  	{Password: "", Data: "828aa81599df0651c0461adb82283e8b89956baee9f6e719947ef9cddc849028001dc9d3ac0938f66b07bacc9751437e1985f8a9763c240e81"},
    63  
    64  	{Password: "", Data: "1793c71df6647860437134073c15688cbb15961dc0758c7ee1225e66e79c724c00d790dba9c671eae89da2c736d858286ac9bd027abacc6443" +
    65  		"0375cd41b63b67c070c7fba475a8dd66ae65ba905176c48cbe6f734fc74df87343d8ccff54bada4aeb0a04bd021633ebe6c4768e23f5dea142" +
    66  		"561d4fe3f90ed59d13dc5fb3a585dadec1742325291b9c81692bdd3420b2428127f8195e0ecd9a1c9237712ed67af7339fbbf7ff3ee1c516e1" +
    67  		"f81e69d933e057b30997e7274a2c9698e07c39f0e8d6818858f34c8191871b5a52bea9061806bd029024bfc1d9c1f230904968d6c9e10fddcb" +
    68  		"c006ba97356ff243570fd96df07dd6894e215a6b24c4ed730369519289ebd877aff6ccbd2265985e4ab1a2b7930bab9cfb767b97348a639ddf" +
    69  		"8db81bf5151da7e8f3d9638a1b86eb1dd78cc6a526f10a414c78638f"},
    70  
    71  	{Password: `xPl.8/rhR"Q_1xLt`, Data: "b5c016e93b84b473fc8a37af94936563630c36d6df1841d23a86ee51ca161f9e00ac19116b32f643ff6a56a212b265d8c56" +
    72  		"195bb0d12ce199e13dfdc5272f80c1564da2c6fc2fa18da91d8062de02af5cdafea491c6f3cae1f"},
    73  
    74  	{Password: `7h5oU4$te{;K}fgqlI^]`, Data: "c58edf7cfd557b6b655de6f48b1a3049d8d049dadb3a7bfa9ac9ccbb5baf37ec00f83086a26f43b7d6bc9075ad0" +
    75  		"38bf5741f118d502ebe94165e4072ba7f98535d6b1e3b6ae67a98115d146d9b4d90e4df4ae82df9cfa17ed7cd42" +
    76  		"465181559f7ddf09c98beec521bb4478e0cb73c4e0827af8688ff4e7a07327a10d5a180035e6ddb16d974a85257" +
    77  		"981cd9e0360a20f7b4d653190267dfb241148f018ae180568042e864b9e1b5bc05425a3abc2b0324f50c72d5679" +
    78  		"8f924405dfc0f8523f4bb564ed65af8e1b1c82a7a0640552ecf81985d95d0993d99172592ddc1393dfa63e8f0b3" +
    79  		"d744b2cc4b73384ca4693f0c1aec0e9b00e85f2937e891105d67da8f59c14ca96608e0425c42f9c1e7c2a8b3413" +
    80  		"e1381784f9cfe01de7c47cea1f8d7a7d88f5d4aca783cf55332b47f957a6b9a65269d7eb606b877b"},
    81  }
    82  
    83  func TestDecryptData(t *testing.T) {
    84  	for i, test := range decryptDataTests {
    85  		i, test := i, test
    86  		t.Run(fmt.Sprintf("Test-%d", i), func(t *testing.T) {
    87  			ciphertext, err := hex.DecodeString(test.Data)
    88  			if err != nil {
    89  				t.Fatalf("Failed to decode ciphertext data: %v", err)
    90  			}
    91  			_, err = DecryptData(test.Password, bytes.NewReader(ciphertext))
    92  			if err != nil {
    93  				t.Fatalf("Failed to decrypt data: %v", err)
    94  			}
    95  		})
    96  	}
    97  }