github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/commands/files/file_test.go (about)

     1  package files
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"mime/multipart"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func TestSliceFiles(t *testing.T) {
    12  	name := "testname"
    13  	files := []File{
    14  		NewReaderFile("file.txt", "file.txt", ioutil.NopCloser(strings.NewReader("Some text!\n")), nil),
    15  		NewReaderFile("beep.txt", "beep.txt", ioutil.NopCloser(strings.NewReader("beep")), nil),
    16  		NewReaderFile("boop.txt", "boop.txt", ioutil.NopCloser(strings.NewReader("boop")), nil),
    17  	}
    18  	buf := make([]byte, 20)
    19  
    20  	sf := NewSliceFile(name, name, files)
    21  
    22  	if !sf.IsDirectory() {
    23  		t.Error("SliceFile should always be a directory")
    24  	}
    25  	if n, err := sf.Read(buf); n > 0 || err != ErrNotReader {
    26  		t.Error("Shouldn't be able to call `Read` on a SliceFile")
    27  	}
    28  	if err := sf.Close(); err != ErrNotReader {
    29  		t.Error("Shouldn't be able to call `Close` on a SliceFile")
    30  	}
    31  
    32  	file, err := sf.NextFile()
    33  	if file == nil || err != nil {
    34  		t.Error("Expected a file and nil error")
    35  	}
    36  	read, err := file.Read(buf)
    37  	if read != 11 || err != nil {
    38  		t.Error("NextFile got a file in the wrong order")
    39  	}
    40  
    41  	file, err = sf.NextFile()
    42  	if file == nil || err != nil {
    43  		t.Error("Expected a file and nil error")
    44  	}
    45  	file, err = sf.NextFile()
    46  	if file == nil || err != nil {
    47  		t.Error("Expected a file and nil error")
    48  	}
    49  
    50  	file, err = sf.NextFile()
    51  	if file != nil || err != io.EOF {
    52  		t.Error("Expected a nil file and io.EOF")
    53  	}
    54  }
    55  
    56  func TestReaderFiles(t *testing.T) {
    57  	message := "beep boop"
    58  	rf := NewReaderFile("file.txt", "file.txt", ioutil.NopCloser(strings.NewReader(message)), nil)
    59  	buf := make([]byte, len(message))
    60  
    61  	if rf.IsDirectory() {
    62  		t.Error("ReaderFile should never be a directory")
    63  	}
    64  	file, err := rf.NextFile()
    65  	if file != nil || err != ErrNotDirectory {
    66  		t.Error("Expected a nil file and ErrNotDirectory")
    67  	}
    68  
    69  	if n, err := rf.Read(buf); n == 0 || err != nil {
    70  		t.Error("Expected to be able to read")
    71  	}
    72  	if err := rf.Close(); err != nil {
    73  		t.Error("Should be able to close")
    74  	}
    75  	if n, err := rf.Read(buf); n != 0 || err != io.EOF {
    76  		t.Error("Expected EOF when reading after close")
    77  	}
    78  }
    79  
    80  func TestMultipartFiles(t *testing.T) {
    81  	data := `
    82  --Boundary!
    83  Content-Type: text/plain
    84  Content-Disposition: file; filename="name"
    85  Some-Header: beep
    86  
    87  beep
    88  --Boundary!
    89  Content-Type: multipart/mixed; boundary=OtherBoundary
    90  Content-Disposition: file; filename="dir"
    91  
    92  --OtherBoundary
    93  Content-Type: text/plain
    94  Content-Disposition: file; filename="some/file/path"
    95  
    96  test
    97  --OtherBoundary
    98  Content-Type: text/plain
    99  
   100  boop
   101  --OtherBoundary
   102  Content-Type: text/plain
   103  
   104  bloop
   105  --OtherBoundary--
   106  --Boundary!--
   107  
   108  `
   109  
   110  	reader := strings.NewReader(data)
   111  	mpReader := multipart.NewReader(reader, "Boundary!")
   112  	buf := make([]byte, 20)
   113  
   114  	// test properties of a file created from the first part
   115  	part, err := mpReader.NextPart()
   116  	if part == nil || err != nil {
   117  		t.Error("Expected non-nil part, nil error")
   118  	}
   119  	mpf, err := NewFileFromPart(part)
   120  	if mpf == nil || err != nil {
   121  		t.Error("Expected non-nil MultipartFile, nil error")
   122  	}
   123  	if mpf.IsDirectory() {
   124  		t.Error("Expected file to not be a directory")
   125  	}
   126  	if mpf.FileName() != "name" {
   127  		t.Error("Expected filename to be \"name\"")
   128  	}
   129  	if file, err := mpf.NextFile(); file != nil || err != ErrNotDirectory {
   130  		t.Error("Expected a nil file and ErrNotDirectory")
   131  	}
   132  	if n, err := mpf.Read(buf); n != 4 || err != nil {
   133  		t.Error("Expected to be able to read 4 bytes")
   134  	}
   135  	if err := mpf.Close(); err != nil {
   136  		t.Error("Expected to be able to close file")
   137  	}
   138  
   139  	// test properties of file created from second part (directory)
   140  	part, err = mpReader.NextPart()
   141  	if part == nil || err != nil {
   142  		t.Error("Expected non-nil part, nil error")
   143  	}
   144  	mpf, err = NewFileFromPart(part)
   145  	if mpf == nil || err != nil {
   146  		t.Error("Expected non-nil MultipartFile, nil error")
   147  	}
   148  	if !mpf.IsDirectory() {
   149  		t.Error("Expected file to be a directory")
   150  	}
   151  	if mpf.FileName() != "dir" {
   152  		t.Error("Expected filename to be \"dir\"")
   153  	}
   154  	if n, err := mpf.Read(buf); n > 0 || err != ErrNotReader {
   155  		t.Error("Shouldn't be able to call `Read` on a directory")
   156  	}
   157  	if err := mpf.Close(); err != ErrNotReader {
   158  		t.Error("Shouldn't be able to call `Close` on a directory")
   159  	}
   160  
   161  	// test properties of first child file
   162  	child, err := mpf.NextFile()
   163  	if child == nil || err != nil {
   164  		t.Error("Expected to be able to read a child file")
   165  	}
   166  	if child.IsDirectory() {
   167  		t.Error("Expected file to not be a directory")
   168  	}
   169  	if child.FileName() != "some/file/path" {
   170  		t.Error("Expected filename to be \"some/file/path\"")
   171  	}
   172  
   173  	// test processing files out of order
   174  	child, err = mpf.NextFile()
   175  	if child == nil || err != nil {
   176  		t.Error("Expected to be able to read a child file")
   177  	}
   178  	child2, err := mpf.NextFile()
   179  	if child == nil || err != nil {
   180  		t.Error("Expected to be able to read a child file")
   181  	}
   182  	if n, err := child2.Read(buf); n != 5 || err != nil {
   183  		t.Error("Expected to be able to read")
   184  	}
   185  	if n, err := child.Read(buf); n != 0 || err == nil {
   186  		t.Error("Expected to not be able to read after advancing NextFile() past this file")
   187  	}
   188  
   189  	// make sure the end is handled properly
   190  	child, err = mpf.NextFile()
   191  	if child != nil || err == nil {
   192  		t.Error("Expected NextFile to return (nil, EOF)")
   193  	}
   194  }