storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/ioutil/ioutil_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 ioutil
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"io"
    23  	goioutil "io/ioutil"
    24  	"os"
    25  	"testing"
    26  	"time"
    27  )
    28  
    29  type sleepWriter struct {
    30  	timeout time.Duration
    31  }
    32  
    33  func (w *sleepWriter) Write(p []byte) (n int, err error) {
    34  	time.Sleep(w.timeout)
    35  	return len(p), nil
    36  }
    37  
    38  func (w *sleepWriter) Close() error {
    39  	return nil
    40  }
    41  
    42  func TestDeadlineWriter(t *testing.T) {
    43  	w := NewDeadlineWriter(&sleepWriter{timeout: 500 * time.Millisecond}, 450*time.Millisecond)
    44  	_, err := w.Write([]byte("1"))
    45  	w.Close()
    46  	if err != context.Canceled {
    47  		t.Error("DeadlineWriter shouldn't be successful - should return context.Canceled")
    48  	}
    49  	_, err = w.Write([]byte("1"))
    50  	if err != context.Canceled {
    51  		t.Error("DeadlineWriter shouldn't be successful - should return context.Canceled")
    52  	}
    53  	w = NewDeadlineWriter(&sleepWriter{timeout: 100 * time.Millisecond}, 600*time.Millisecond)
    54  	n, err := w.Write([]byte("abcd"))
    55  	w.Close()
    56  	if err != nil {
    57  		t.Errorf("DeadlineWriter should succeed but failed with %s", err)
    58  	}
    59  	if n != 4 {
    60  		t.Errorf("DeadlineWriter should succeed but should have only written 4 bytes, but returned %d instead", n)
    61  	}
    62  }
    63  
    64  func TestCloseOnWriter(t *testing.T) {
    65  	writer := WriteOnClose(goioutil.Discard)
    66  	if writer.HasWritten() {
    67  		t.Error("WriteOnCloser must not be marked as HasWritten")
    68  	}
    69  	writer.Write(nil)
    70  	if !writer.HasWritten() {
    71  		t.Error("WriteOnCloser must be marked as HasWritten")
    72  	}
    73  
    74  	writer = WriteOnClose(goioutil.Discard)
    75  	writer.Close()
    76  	if !writer.HasWritten() {
    77  		t.Error("WriteOnCloser must be marked as HasWritten")
    78  	}
    79  }
    80  
    81  // Test for AppendFile.
    82  func TestAppendFile(t *testing.T) {
    83  	f, err := goioutil.TempFile("", "")
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	name1 := f.Name()
    88  	defer os.Remove(name1)
    89  	f.WriteString("aaaaaaaaaa")
    90  	f.Close()
    91  
    92  	f, err = goioutil.TempFile("", "")
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	name2 := f.Name()
    97  	defer os.Remove(name2)
    98  	f.WriteString("bbbbbbbbbb")
    99  	f.Close()
   100  
   101  	if err = AppendFile(name1, name2, false); err != nil {
   102  		t.Error(err)
   103  	}
   104  
   105  	b, err := goioutil.ReadFile(name1)
   106  	if err != nil {
   107  		t.Error(err)
   108  	}
   109  
   110  	expected := "aaaaaaaaaabbbbbbbbbb"
   111  	if string(b) != expected {
   112  		t.Errorf("AppendFile() failed, expected: %s, got %s", expected, string(b))
   113  	}
   114  }
   115  
   116  func TestSkipReader(t *testing.T) {
   117  	testCases := []struct {
   118  		src      io.Reader
   119  		skipLen  int64
   120  		expected string
   121  	}{
   122  		{bytes.NewBuffer([]byte("")), 0, ""},
   123  		{bytes.NewBuffer([]byte("")), 1, ""},
   124  		{bytes.NewBuffer([]byte("abc")), 0, "abc"},
   125  		{bytes.NewBuffer([]byte("abc")), 1, "bc"},
   126  		{bytes.NewBuffer([]byte("abc")), 2, "c"},
   127  		{bytes.NewBuffer([]byte("abc")), 3, ""},
   128  		{bytes.NewBuffer([]byte("abc")), 4, ""},
   129  	}
   130  	for i, testCase := range testCases {
   131  		r := NewSkipReader(testCase.src, testCase.skipLen)
   132  		b, err := goioutil.ReadAll(r)
   133  		if err != nil {
   134  			t.Errorf("Case %d: Unexpected err %v", i, err)
   135  		}
   136  		if string(b) != testCase.expected {
   137  			t.Errorf("Case %d: Got wrong result: %v", i, string(b))
   138  		}
   139  	}
   140  }
   141  
   142  func TestSameFile(t *testing.T) {
   143  	f, err := goioutil.TempFile("", "")
   144  	if err != nil {
   145  		t.Errorf("Error creating tmp file: %v", err)
   146  	}
   147  	tmpFile := f.Name()
   148  	f.Close()
   149  	defer os.Remove(f.Name())
   150  	fi1, err := os.Stat(tmpFile)
   151  	if err != nil {
   152  		t.Fatalf("Error Stat(): %v", err)
   153  	}
   154  	fi2, err := os.Stat(tmpFile)
   155  	if err != nil {
   156  		t.Fatalf("Error Stat(): %v", err)
   157  	}
   158  	if !SameFile(fi1, fi2) {
   159  		t.Fatal("Expected the files to be same")
   160  	}
   161  	if err = goioutil.WriteFile(tmpFile, []byte("aaa"), 0644); err != nil {
   162  		t.Fatal(err)
   163  	}
   164  	fi2, err = os.Stat(tmpFile)
   165  	if err != nil {
   166  		t.Fatalf("Error Stat(): %v", err)
   167  	}
   168  	if SameFile(fi1, fi2) {
   169  		t.Fatal("Expected the files not to be same")
   170  	}
   171  }