storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/copy-part-range_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2017 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  package cmd
    18  
    19  import "testing"
    20  
    21  // Test parseCopyPartRange()
    22  func TestParseCopyPartRangeSpec(t *testing.T) {
    23  	// Test success cases.
    24  	successCases := []struct {
    25  		rangeString string
    26  		offsetBegin int64
    27  		offsetEnd   int64
    28  	}{
    29  		{"bytes=2-5", 2, 5},
    30  		{"bytes=2-9", 2, 9},
    31  		{"bytes=2-2", 2, 2},
    32  		{"bytes=0000-0006", 0, 6},
    33  	}
    34  	objectSize := int64(10)
    35  
    36  	for _, successCase := range successCases {
    37  		rs, err := parseCopyPartRangeSpec(successCase.rangeString)
    38  		if err != nil {
    39  			t.Fatalf("expected: <nil>, got: %s", err)
    40  		}
    41  
    42  		start, length, err1 := rs.GetOffsetLength(objectSize)
    43  		if err1 != nil {
    44  			t.Fatalf("expected: <nil>, got: %s", err1)
    45  		}
    46  
    47  		if start != successCase.offsetBegin {
    48  			t.Fatalf("expected: %d, got: %d", successCase.offsetBegin, start)
    49  		}
    50  
    51  		if start+length-1 != successCase.offsetEnd {
    52  			t.Fatalf("expected: %d, got: %d", successCase.offsetEnd, start+length-1)
    53  		}
    54  	}
    55  
    56  	// Test invalid range strings.
    57  	invalidRangeStrings := []string{
    58  		"bytes=8",
    59  		"bytes=5-2",
    60  		"bytes=+2-5",
    61  		"bytes=2-+5",
    62  		"bytes=2--5",
    63  		"bytes=-",
    64  		"2-5",
    65  		"bytes = 2-5",
    66  		"bytes=2 - 5",
    67  		"bytes=0-0,-1",
    68  		"bytes=2-5 ",
    69  		"bytes=-1",
    70  		"bytes=1-",
    71  	}
    72  	for _, rangeString := range invalidRangeStrings {
    73  		if _, err := parseCopyPartRangeSpec(rangeString); err == nil {
    74  			t.Fatalf("expected: an error, got: <nil> for range %s", rangeString)
    75  		}
    76  	}
    77  
    78  	// Test error range strings.
    79  	errorRangeString := []string{
    80  		"bytes=10-10",
    81  		"bytes=20-30",
    82  	}
    83  	for _, rangeString := range errorRangeString {
    84  		rs, err := parseCopyPartRangeSpec(rangeString)
    85  		if err == nil {
    86  			err1 := checkCopyPartRangeWithSize(rs, objectSize)
    87  			if err1 != errInvalidRangeSource {
    88  				t.Fatalf("expected: %s, got: %s", errInvalidRangeSource, err)
    89  			}
    90  		} else {
    91  			t.Fatalf("expected: %s, got: <nil>", errInvalidRangeSource)
    92  		}
    93  	}
    94  }