github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/content/fileStorage_test.go (about) 1 package content 2 3 import ( 4 "bytes" 5 "crypto/sha256" 6 "encoding/hex" 7 "io" 8 "io/ioutil" 9 "os" 10 "path" 11 12 . "gopkg.in/check.v1" 13 ) 14 15 type FileStorageTests struct { 16 dir string 17 storage *FileStorage 18 data []byte 19 } 20 21 var _ = Suite(&FileStorageTests{}) 22 23 var dangerousNames = []string{"..", "../xyz"} 24 25 func (t *FileStorageTests) SetUpTest(c *C) { 26 t.dir = c.MkDir() 27 t.storage = NewFileStorage(t.dir) 28 t.data = []byte("This is a test blob storage file input.") 29 } 30 31 func (t *FileStorageTests) blobID() string { 32 blobIDBytes := sha256.New().Sum(t.data) 33 return hex.EncodeToString(blobIDBytes[:]) 34 } 35 36 func (t *FileStorageTests) blobPath() string { 37 return path.Join(t.dir, t.blobID()) 38 } 39 40 func (t *FileStorageTests) testReader() io.Reader { 41 return bytes.NewReader(t.data) 42 } 43 44 func (t *FileStorageTests) setData() error { 45 return t.storage.Set(t.blobID(), t.testReader()) 46 } 47 48 func (t *FileStorageTests) TestSetDangerousName(c *C) { 49 r := t.testReader() 50 for _, id := range dangerousNames { 51 err := t.storage.Set(id, r) 52 c.Assert(err, NotNil) 53 } 54 } 55 56 func (t *FileStorageTests) TestSet(c *C) { 57 err := t.setData() 58 c.Assert(err, IsNil) 59 _, err = os.Stat(t.blobPath()) 60 c.Assert(err, IsNil) 61 } 62 63 func (t *FileStorageTests) TestSetInputData(c *C) { 64 t.setData() 65 file, _ := os.Open(t.blobPath()) 66 defer file.Close() 67 fileData, _ := ioutil.ReadAll(file) 68 c.Assert(fileData[:], DeepEquals, t.data[:]) 69 } 70 71 func (t *FileStorageTests) TestExistsNegative(c *C) { 72 c.Assert(t.storage.Exists(t.blobID()), Equals, false) 73 } 74 75 func (t *FileStorageTests) TestExistsPositive(c *C) { 76 t.setData() 77 c.Assert(t.storage.Exists(t.blobID()), Equals, true) 78 } 79 80 func (t *FileStorageTests) TestGet(c *C) { 81 t.storage.Set(t.blobID(), t.testReader()) 82 _, err := t.storage.Get(t.blobID()) 83 c.Assert(err, IsNil) 84 } 85 86 func (t *FileStorageTests) TestGetDangerousName(c *C) { 87 for _, id := range dangerousNames { 88 _, err := t.storage.Get(id) 89 c.Assert(err, NotNil) 90 } 91 } 92 93 func (t *FileStorageTests) TestGetData(c *C) { 94 t.setData() 95 file, _ := t.storage.Get(t.blobID()) 96 fileData, _ := ioutil.ReadAll(file) 97 c.Assert(fileData[:], DeepEquals, t.data) 98 } 99 100 func (t *FileStorageTests) TestGetError(c *C) { 101 _, err := t.storage.Get(t.blobID()) 102 c.Assert(err, NotNil) 103 } 104 105 func (t *FileStorageTests) TestSetError(c *C) { 106 os.RemoveAll(t.dir) 107 108 err := t.storage.Set(t.blobID(), 109 t.testReader()) 110 c.Assert(err, NotNil) 111 } 112 113 func (t *FileStorageTests) TestDelete(c *C) { 114 t.setData() 115 err := t.storage.Delete(t.blobID()) 116 c.Assert(err, IsNil) 117 c.Assert(t.storage.Exists(t.blobID()), Equals, false) 118 } 119 120 func (t *FileStorageTests) TestDeleteError(c *C) { 121 err := t.storage.Delete(t.blobID()) 122 c.Assert(err, NotNil) 123 }