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.
bog/dataswamp/services_test.go

108 lines
2.6 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", "ns1"}
var file_ref2 = swampfile.FileReference{"/path1", "ns2"}
var file_ref3 = swampfile.FileReference{"/path2", "ns1"}
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 TestNSIsolation(t *testing.T) {
is := is.New(t)
s := NewTestSwampFileService()
ns1_file := bytes.NewBufferString("My bog data ns1")
ns2_file := bytes.NewBufferString("My bog data ns2")
_ = 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 ns1")
}
func TestPathStrictMode(t *testing.T) {
is := is.New(t)
s := NewTestSwampFileService()
ns_file := bytes.NewBufferString("My bog data ns1")
ref := swampfile.FileReference {
"/path/../with/../backrefs",
"ns1",
}
outfile, err := s.OpenOutFile(ref)
is.Equal(err, swampfile.ErrUnacceptablePath)
is.Equal(outfile, nil)
err = s.SaveFile(ref, ns_file, int64(ns_file.Len()))
is.Equal(err, swampfile.ErrUnacceptablePath)
}