github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/cp-main_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 TestParseMetaData(t *testing.T) {
    26  	metaDataCases := []struct {
    27  		input  string
    28  		output map[string]string
    29  		err    error
    30  		status bool
    31  	}{
    32  		// success scenario using ; as delimiter
    33  		{"key1=value1;key2=value2", map[string]string{"Key1": "value1", "Key2": "value2"}, nil, true},
    34  		// success scenario using ; as delimiter
    35  		{"key1=m1=m2,m3=m4;key2=value2", map[string]string{"Key1": "m1=m2,m3=m4", "Key2": "value2"}, nil, true},
    36  		// success scenario using = more than once
    37  		{"Cache-Control=max-age=90000,min-fresh=9000;key1=value1;key2=value2", map[string]string{"Cache-Control": "max-age=90000,min-fresh=9000", "Key1": "value1", "Key2": "value2"}, nil, true},
    38  		// using different delimiter, other than '=' between key value
    39  		{"key1:value1;key2:value2", nil, ErrInvalidMetadata, false},
    40  		// using no delimiter
    41  		{"key1:value1:key2:value2", nil, ErrInvalidMetadata, false},
    42  		// success: use value in quotes
    43  		{"Content-Disposition='form-data; name=\"description\"'", map[string]string{"Content-Disposition": "form-data; name=\"description\""}, nil, true},
    44  		// success: use value in double quotes
    45  		{"Content-Disposition=\"form-data; name='description'\"", map[string]string{"Content-Disposition": "form-data; name='description'"}, nil, true},
    46  		// fail: unterminated quote
    47  		{"Content-Disposition='form-data; name=\"description\"", nil, ErrInvalidMetadata, false},
    48  		// fail: unterminated double quote
    49  		{"Content-Disposition=\"form-data; name='description'", nil, ErrInvalidMetadata, false},
    50  		// success: use value and key in quotes
    51  		{"\"Content-Disposition\"='form-data; name=\"description\"'", map[string]string{"Content-Disposition": "form-data; name=\"description\""}, nil, true},
    52  		// success: use value and key in quotes
    53  		{"\"Content=Disposition;Other key part=this is also key data\"='form-data; name=\"description\"'", map[string]string{"Content=Disposition;Other key part=this is also key data": "form-data; name=\"description\""}, nil, true},
    54  	}
    55  
    56  	for idx, testCase := range metaDataCases {
    57  		metaDatamap, errMeta := getMetaDataEntry(testCase.input)
    58  		if testCase.status == true {
    59  			if errMeta != nil {
    60  				t.Fatalf("Test %d: generated error not matching, expected = `%s`, found = `%s`", idx+1, testCase.err, errMeta)
    61  			}
    62  			if !reflect.DeepEqual(metaDatamap, testCase.output) {
    63  				t.Fatalf("Test %d: generated Map not matching, expected = `%s`, found = `%s`", idx+1, testCase.input, metaDatamap)
    64  			}
    65  		}
    66  
    67  		if testCase.status == false {
    68  			if !reflect.DeepEqual(metaDatamap, testCase.output) {
    69  				t.Fatalf("Test %d: generated Map not matching, expected = `%s`, found = `%s`", idx+1, testCase.input, metaDatamap)
    70  			}
    71  			if errMeta.Cause.Error() != testCase.err.Error() {
    72  				t.Fatalf("Test %d: generated error not matching, expected = `%s`, found = `%s`", idx+1, testCase.err, errMeta)
    73  			}
    74  		}
    75  	}
    76  }