You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.4 KiB
Go
103 lines
2.4 KiB
Go
package dataswamp
|
|
|
|
import (
|
|
"time"
|
|
"bytes"
|
|
"testing"
|
|
"github.com/matryer/is"
|
|
"caj-larsson/bog/dataswamp/swampfile"
|
|
"caj-larsson/bog/dataswamp/namespace"
|
|
m_namespace "caj-larsson/bog/infrastructure/memory/namespace"
|
|
m_swampfile "caj-larsson/bog/infrastructure/memory/swampfile"
|
|
)
|
|
|
|
var file_ref1 = swampfile.FileReference { "ptah1", "ua1" }
|
|
var file_ref2 = swampfile.FileReference { "path1", "ua2" }
|
|
var file_ref3 = swampfile.FileReference { "path2", "ua1" }
|
|
|
|
|
|
func NewTestSwampFileService() SwampFileService {
|
|
file_repo := m_swampfile.NewRepository()
|
|
ns_repo := m_namespace.NewRepository()
|
|
return NewSwampFileService(ns_repo, file_repo, 1024, time.Hour)
|
|
}
|
|
|
|
func TestFileDontExist(t *testing.T) {
|
|
s := NewTestSwampFileService()
|
|
outfile, err := s.OpenOutFile(file_ref1)
|
|
|
|
if outfile != nil && err != swampfile.ErrNotExists {
|
|
t.Errorf("File shall not exist by default")
|
|
}
|
|
}
|
|
|
|
func TestFileIsStored(t *testing.T) {
|
|
s := NewTestSwampFileService()
|
|
|
|
fakefile := bytes.NewBufferString("My bog data")
|
|
|
|
err := s.SaveFile(file_ref1, fakefile, int64(fakefile.Len()))
|
|
|
|
if err != nil {
|
|
t.Errorf("A small file should be writable %s", err)
|
|
}
|
|
|
|
largefakefile := bytes.NewBufferString("")
|
|
|
|
for largefakefile.Len() < 64000 {
|
|
_, err = largefakefile.WriteString("A very repetitive file")
|
|
}
|
|
|
|
err = s.SaveFile(file_ref3, largefakefile, int64(largefakefile.Len()))
|
|
|
|
if err != namespace.ErrExceedQuota {
|
|
t.Errorf("too large files should not be excepted")
|
|
}
|
|
|
|
}
|
|
|
|
|
|
func TestFileIsReadBack(t *testing.T) {
|
|
s := NewTestSwampFileService()
|
|
|
|
infile := bytes.NewBufferString("My bog data")
|
|
|
|
_ = s.SaveFile(file_ref1, infile, int64(infile.Len()))
|
|
|
|
outswampfile, _ := s.OpenOutFile(file_ref1)
|
|
|
|
outfile := bytes.NewBufferString("")
|
|
_, _ = outfile.ReadFrom(outswampfile)
|
|
|
|
if outfile.String() != "My bog data" {
|
|
t.Errorf("file corrupted")
|
|
}
|
|
}
|
|
|
|
|
|
func TestUAIsolation(t *testing.T) {
|
|
s := NewTestSwampFileService()
|
|
|
|
ns1_file := bytes.NewBufferString("My bog data ua1")
|
|
ns2_file := bytes.NewBufferString("My bog data ua2")
|
|
|
|
_ = s.SaveFile(file_ref1, ns1_file, int64(ns1_file.Len()))
|
|
_ = s.SaveFile(file_ref2, ns2_file, int64(ns2_file.Len()))
|
|
|
|
outswampfile, _ := s.OpenOutFile(file_ref1)
|
|
|
|
outfile := bytes.NewBufferString("")
|
|
_, _ = outfile.ReadFrom(outswampfile)
|
|
|
|
if outfile.String() != "My bog data ua1" {
|
|
t.Errorf("file corrupted")
|
|
}
|
|
}
|
|
|
|
|
|
func TestCleanPath(t *testing.T) {
|
|
is := is.New(t)
|
|
|
|
is.Equal(CleanPath("/"), "/")
|
|
}
|