github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/utils_test.go (about)

     1  // Copyright (c) 2015-2022 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  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestParseAttribute(t *testing.T) {
    26  	metaDataCases := []struct {
    27  		input  string
    28  		output map[string]string
    29  		err    error
    30  		status bool
    31  	}{
    32  		// // When blank value is passed.
    33  		{"", map[string]string{}, ErrInvalidFileSystemAttribute, false},
    34  		//  When space is passed.
    35  		{"  ", map[string]string{}, ErrInvalidFileSystemAttribute, false},
    36  		// When / is passed.
    37  		{"/", map[string]string{}, ErrInvalidFileSystemAttribute, false},
    38  		// When "atime:" is passed.
    39  		{"atime:/", map[string]string{"atime": ""}, ErrInvalidFileSystemAttribute, false},
    40  		// When "atime:" is passed.
    41  		{"atime", map[string]string{"atime": ""}, nil, true},
    42  		//  When "atime:" is passed.
    43  		{"atime:", map[string]string{"atime": ""}, nil, true},
    44  		// Passing a valid value
    45  		{
    46  			"atime:1/gid:1/gname:a/md:/mode:3/mtime:1/uid:1/uname:a",
    47  			map[string]string{
    48  				"atime": "1",
    49  				"gid":   "1",
    50  				"gname": "a",
    51  				"md":    "",
    52  				"mode":  "3",
    53  				"mtime": "1",
    54  				"uid":   "1",
    55  				"uname": "a",
    56  			},
    57  			nil, true,
    58  		},
    59  	}
    60  
    61  	for idx, testCase := range metaDataCases {
    62  		meta, err := parseAttribute(map[string]string{
    63  			metadataKey: testCase.input,
    64  		})
    65  		if testCase.status == true {
    66  			if err != nil {
    67  				t.Fatalf("Test %d: generated error not matching, expected = `%s`, found = `%s`", idx+1, testCase.err, err)
    68  			}
    69  			if !reflect.DeepEqual(meta, testCase.output) {
    70  				t.Fatalf("Test %d: generated Map not matching, expected = `%s`, found = `%s`", idx+1, testCase.input, meta)
    71  			}
    72  		}
    73  		if testCase.status == false {
    74  			if !reflect.DeepEqual(meta, testCase.output) {
    75  				t.Fatalf("Test %d: generated Map not matching, expected = `%s`, found = `%s`", idx+1, testCase.input, meta)
    76  			}
    77  			if err != testCase.err {
    78  				t.Fatalf("Test %d: generated error not matching, expected = `%s`, found = `%s`", idx+1, testCase.err, err)
    79  			}
    80  		}
    81  
    82  	}
    83  }