github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/config-encrypted_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 cmd
    19  
    20  import (
    21  	"bytes"
    22  	"testing"
    23  
    24  	"github.com/minio/madmin-go/v3"
    25  	"github.com/minio/minio/internal/auth"
    26  )
    27  
    28  func TestDecryptData(t *testing.T) {
    29  	cred1 := auth.Credentials{
    30  		AccessKey: "minio",
    31  		SecretKey: "minio123",
    32  	}
    33  
    34  	cred2 := auth.Credentials{
    35  		AccessKey: "minio",
    36  		SecretKey: "minio1234",
    37  	}
    38  
    39  	data := []byte(`config data`)
    40  	edata1, err := madmin.EncryptData(cred1.String(), data)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	edata2, err := madmin.EncryptData(cred2.String(), data)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	tests := []struct {
    51  		edata   []byte
    52  		cred    auth.Credentials
    53  		success bool
    54  	}{
    55  		{edata1, cred1, true},
    56  		{edata2, cred2, true},
    57  		{data, cred1, false},
    58  	}
    59  
    60  	for _, test := range tests {
    61  		t.Run("", func(t *testing.T) {
    62  			ddata, err := madmin.DecryptData(test.cred.String(), bytes.NewReader(test.edata))
    63  			if err != nil && test.success {
    64  				t.Errorf("Expected success, saw failure %v", err)
    65  			}
    66  			if err == nil && !test.success {
    67  				t.Error("Expected failure, saw success")
    68  			}
    69  			if test.success {
    70  				if !bytes.Equal(ddata, data) {
    71  					t.Errorf("Expected %s, got %s", string(data), string(ddata))
    72  				}
    73  			}
    74  		})
    75  	}
    76  }