github.com/readium/readium-lcp-server@v0.0.0-20240509124024-799e77a0bbd6/storage/fs_test.go (about)

     1  // Copyright (c) 2016 Readium Foundation
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification,
     4  // are permitted provided that the following conditions are met:
     5  //
     6  // 1. Redistributions of source code must retain the above copyright notice, this
     7  //    list of conditions and the following disclaimer.
     8  // 2. Redistributions in binary form must reproduce the above copyright notice,
     9  //    this list of conditions and the following disclaimer in the documentation and/or
    10  //    other materials provided with the distribution.
    11  // 3. Neither the name of the organization nor the names of its contributors may be
    12  //    used to endorse or promote products derived from this software without specific
    13  //    prior written permission
    14  //
    15  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    16  // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    17  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    18  // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
    19  // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    20  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    21  // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    22  // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    23  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    24  // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    25  
    26  package storage
    27  
    28  import (
    29  	"bytes"
    30  	"fmt"
    31  	"io"
    32  	"math/rand"
    33  	"os"
    34  	"path/filepath"
    35  	"testing"
    36  	"time"
    37  )
    38  
    39  func TestFileSystemStorage(t *testing.T) {
    40  	dir := filepath.Join(os.TempDir(), "lcpserve_test_store", fmt.Sprintf("%d", rand.New(rand.NewSource(time.Now().UnixNano())).Int()))
    41  	err := os.MkdirAll(dir, os.ModePerm)
    42  	if err != nil {
    43  		t.Error("Could not create temp directory for test")
    44  		t.Error(err)
    45  		t.FailNow()
    46  	}
    47  	defer os.RemoveAll(dir)
    48  
    49  	store := NewFileSystem(dir, "http://localhost/assets")
    50  
    51  	item, err := store.Add("test", bytes.NewReader([]byte("test1234")))
    52  	if err != nil {
    53  		t.Error(err)
    54  		t.FailNow()
    55  	}
    56  
    57  	if item.Key() != "test" {
    58  		t.Errorf("expected item key to be test, got %s", item.Key())
    59  	}
    60  
    61  	// Commented out because store.Info no longer exists.
    62  	//
    63  	// fileInfo, err := store.Info("test")
    64  	// if fileInfo.Name() != "test" {
    65  	// 	t.Errorf("expected item file name to be test, got %s", fileInfo.Name())
    66  	// }
    67  	// if fileInfo.Size() != 8 {
    68  	// 	t.Errorf("expected item file size to be 8, got %d", fileInfo.Size())
    69  	// }
    70  
    71  	if item.PublicURL() != "http://localhost/assets/test" {
    72  		t.Errorf("expected item url to be http://localhost/assets/test, got %s", item.PublicURL())
    73  	}
    74  
    75  	var buf [8]byte
    76  	contents, err := item.Contents()
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	if _, err = io.ReadFull(contents, buf[:]); err != nil {
    81  		t.Fatal(err)
    82  	} else {
    83  		if string(buf[:]) != "test1234" {
    84  			t.Error("expected buf to be test1234, got ", string(buf[:]))
    85  		}
    86  	}
    87  
    88  	results, err := store.List()
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	if len(results) != 1 {
    94  		t.Error("Expected 1 element, got ", len(results))
    95  	}
    96  
    97  }