package dataswamp import ( "bytes" "caj-larsson/bog/dataswamp/namespace" "caj-larsson/bog/dataswamp/swampfile" m_namespace "caj-larsson/bog/infrastructure/memory/namespace" m_swampfile "caj-larsson/bog/infrastructure/memory/swampfile" "caj-larsson/bog/infrastructure/system_time" "fmt" "github.com/matryer/is" "github.com/spf13/afero" "testing" "time" ) type TestLogger struct{} func (t TestLogger) Debug(format string, a ...interface{}) {} func (t TestLogger) Info(format string, a ...interface{}) {} func (t TestLogger) Warn(format string, a ...interface{}) {} var file_ref1 = swampfile.FileReference{"/path1", "ns1"} var file_ref2 = swampfile.FileReference{"/path1", "ns2"} var file_ref3 = swampfile.FileReference{"/path2", "ns1"} func NewTestDataSwampService() DataSwampService { file_repo := m_swampfile.NewRepository() ns_repo := m_namespace.NewRepository() logger := TestLogger{} ns_svc := namespace.NewNamespaceService( ns_repo, logger, system_time.Clock{}, time.Hour, 1024, ) return *NewDataSwampService(*ns_svc, file_repo, logger) } func TestFileDontExist(t *testing.T) { is := is.New(t) s := NewTestDataSwampService() 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 := NewTestDataSwampService() 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 := NewTestDataSwampService() 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 := NewTestDataSwampService() 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 := NewTestDataSwampService() 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) } func TestQuotaWithContenSizeLieOver(t *testing.T) { is := is.New(t) s := NewTestDataSwampService() largefakefile := bytes.NewBufferString("") for largefakefile.Len() < 64000 { _, _ = largefakefile.WriteString("A very repetitive file") } err := s.SaveFile(file_ref3, largefakefile, int64(10)) is.Equal(err, swampfile.ErrContentSizeExceeded) } func TestQuotaWithContenSizeLieUnder(t *testing.T) { is := is.New(t) s := NewTestDataSwampService() largefakefile := bytes.NewBufferString("small") err := s.SaveFile(file_ref3, largefakefile, int64(1024)) is.Equal(err, swampfile.ErrContentSizeExaggerated) } func TestCleanUpExpired(t *testing.T) { is := is.New(t) fs := afero.NewMemMapFs() file_repo := m_swampfile.Repository{fs} ns_repo := m_namespace.NewRepository() logger := TestLogger{} ns_svc := namespace.NewNamespaceService( ns_repo, logger, system_time.Clock{}, time.Hour, 1024, ) s := NewDataSwampService(*ns_svc, file_repo, logger) fakefile := bytes.NewBufferString("My bog") err := s.SaveFile(file_ref1, fakefile, int64(fakefile.Len())) is.NoErr(err) fakefile = bytes.NewBufferString("My bog") err = s.SaveFile(file_ref3, fakefile, int64(fakefile.Len())) is.NoErr(err) err = fs.Chtimes("1/path1", time.Now(), time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)) is.NoErr(s.CleanUpExpiredFiles()) ns, err := ns_repo.GetByName("ns1") fmt.Printf("file final usage %v\n", ns.FileQuota) is.NoErr(err) fmt.Printf("file\n") is.Equal(ns.FileQuota.CurrentUsage, int64(len("My bog"))) }