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.
93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
package dataswamp
|
|
|
|
import (
|
|
"bytes"
|
|
"caj-larsson/bog/dataswamp/swampfile"
|
|
"github.com/matryer/is"
|
|
"testing"
|
|
"time"
|
|
// "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) {
|
|
is := is.New(t)
|
|
s := NewTestSwampFileService()
|
|
outfile, err := s.OpenOutFile(file_ref1)
|
|
|
|
is.True(err == swampfile.ErrNotExists)
|
|
is.True(outfile == nil)
|
|
}
|
|
|
|
func TestFileIsStored(t *testing.T) {
|
|
is := is.New(t)
|
|
s := NewTestSwampFileService()
|
|
|
|
fakefile := bytes.NewBufferString("My bog data")
|
|
|
|
err := s.SaveFile(file_ref1, fakefile, int64(fakefile.Len()))
|
|
|
|
is.NoErr(err)
|
|
|
|
largefakefile := bytes.NewBufferString("")
|
|
|
|
for largefakefile.Len() < 64000 {
|
|
_, err = largefakefile.WriteString("A very repetitive file")
|
|
}
|
|
|
|
err = s.SaveFile(file_ref3, largefakefile, int64(largefakefile.Len()))
|
|
|
|
is.Equal(err, swampfile.ErrExceedQuota)
|
|
}
|
|
|
|
func TestFileIsReadBack(t *testing.T) {
|
|
is := is.New(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)
|
|
|
|
is.Equal(outfile.String(), "My bog data")
|
|
}
|
|
|
|
func TestUAIsolation(t *testing.T) {
|
|
is := is.New(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)
|
|
|
|
is.Equal(outfile.String(), "My bog data ua1")
|
|
}
|
|
|
|
func TestCleanPath(t *testing.T) {
|
|
is := is.New(t)
|
|
|
|
is.Equal(CleanPath("/"), "/")
|
|
}
|