2022-01-08 17:14:43 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"heckel.io/ntfy/util"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-07-08 14:00:04 +00:00
|
|
|
"time"
|
2022-01-08 17:14:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
oneKilobyteArray = make([]byte, 1024)
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFileCache_Write_Success(t *testing.T) {
|
|
|
|
dir, c := newTestFileCache(t)
|
2022-07-08 14:00:04 +00:00
|
|
|
size, err := c.Write("abcdefghijkl", strings.NewReader("normal file"), util.NewFixedLimiter(999))
|
2022-01-08 17:14:43 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, int64(11), size)
|
2022-07-08 14:00:04 +00:00
|
|
|
require.Equal(t, "normal file", readFile(t, dir+"/abcdefghijkl"))
|
2022-01-08 17:14:43 +00:00
|
|
|
require.Equal(t, int64(11), c.Size())
|
|
|
|
require.Equal(t, int64(10229), c.Remaining())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileCache_Write_Remove_Success(t *testing.T) {
|
|
|
|
dir, c := newTestFileCache(t) // max = 10k (10240), each = 1k (1024)
|
|
|
|
for i := 0; i < 10; i++ { // 10x999 = 9990
|
2022-07-08 14:00:04 +00:00
|
|
|
size, err := c.Write(fmt.Sprintf("abcdefghijk%d", i), bytes.NewReader(make([]byte, 999)))
|
2022-01-08 17:14:43 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, int64(999), size)
|
|
|
|
}
|
|
|
|
require.Equal(t, int64(9990), c.Size())
|
|
|
|
require.Equal(t, int64(250), c.Remaining())
|
2022-07-08 14:00:04 +00:00
|
|
|
require.FileExists(t, dir+"/abcdefghijk1")
|
|
|
|
require.FileExists(t, dir+"/abcdefghijk5")
|
2022-01-08 17:14:43 +00:00
|
|
|
|
2022-07-08 14:00:04 +00:00
|
|
|
require.Nil(t, c.Remove("abcdefghijk1", "abcdefghijk5"))
|
|
|
|
require.NoFileExists(t, dir+"/abcdefghijk1")
|
|
|
|
require.NoFileExists(t, dir+"/abcdefghijk5")
|
2022-01-08 17:14:43 +00:00
|
|
|
require.Equal(t, int64(7992), c.Size())
|
|
|
|
require.Equal(t, int64(2248), c.Remaining())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileCache_Write_FailedTotalSizeLimit(t *testing.T) {
|
|
|
|
dir, c := newTestFileCache(t)
|
|
|
|
for i := 0; i < 10; i++ {
|
2022-07-08 14:00:04 +00:00
|
|
|
size, err := c.Write(fmt.Sprintf("abcdefghijk%d", i), bytes.NewReader(oneKilobyteArray))
|
2022-01-08 17:14:43 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, int64(1024), size)
|
|
|
|
}
|
2022-07-08 14:00:04 +00:00
|
|
|
_, err := c.Write("abcdefghijkX", bytes.NewReader(oneKilobyteArray))
|
2022-01-08 17:14:43 +00:00
|
|
|
require.Equal(t, util.ErrLimitReached, err)
|
2022-07-08 14:00:04 +00:00
|
|
|
require.NoFileExists(t, dir+"/abcdefghijkX")
|
2022-01-08 17:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileCache_Write_FailedFileSizeLimit(t *testing.T) {
|
|
|
|
dir, c := newTestFileCache(t)
|
2022-07-08 14:00:04 +00:00
|
|
|
_, err := c.Write("abcdefghijkl", bytes.NewReader(make([]byte, 1025)))
|
2022-01-08 17:14:43 +00:00
|
|
|
require.Equal(t, util.ErrLimitReached, err)
|
2022-07-08 14:00:04 +00:00
|
|
|
require.NoFileExists(t, dir+"/abcdefghijkl")
|
2022-01-08 17:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileCache_Write_FailedAdditionalLimiter(t *testing.T) {
|
|
|
|
dir, c := newTestFileCache(t)
|
2022-07-08 14:00:04 +00:00
|
|
|
_, err := c.Write("abcdefghijkl", bytes.NewReader(make([]byte, 1001)), util.NewFixedLimiter(1000))
|
2022-01-08 17:14:43 +00:00
|
|
|
require.Equal(t, util.ErrLimitReached, err)
|
2022-07-08 14:00:04 +00:00
|
|
|
require.NoFileExists(t, dir+"/abcdefghijkl")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileCache_RemoveExpired(t *testing.T) {
|
|
|
|
dir, c := newTestFileCache(t)
|
|
|
|
_, err := c.Write("abcdefghijkl", bytes.NewReader(make([]byte, 1001)))
|
|
|
|
require.Nil(t, err)
|
|
|
|
_, err = c.Write("notdeleted12", bytes.NewReader(make([]byte, 1001)))
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
modTime := time.Now().Add(-1 * 4 * time.Hour)
|
|
|
|
require.Nil(t, os.Chtimes(dir+"/abcdefghijkl", modTime, modTime))
|
|
|
|
|
|
|
|
olderThan := time.Now().Add(-1 * 3 * time.Hour)
|
|
|
|
ids, err := c.Expired(olderThan)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, []string{"abcdefghijkl"}, ids)
|
|
|
|
require.Nil(t, c.Remove(ids...))
|
|
|
|
require.NoFileExists(t, dir+"/abcdefghijkl")
|
|
|
|
require.FileExists(t, dir+"/notdeleted12")
|
|
|
|
|
|
|
|
ids, err = c.Expired(olderThan)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Empty(t, ids)
|
2022-01-08 17:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestFileCache(t *testing.T) (dir string, cache *fileCache) {
|
|
|
|
dir = t.TempDir()
|
|
|
|
cache, err := newFileCache(dir, 10*1024, 1*1024)
|
|
|
|
require.Nil(t, err)
|
|
|
|
return dir, cache
|
|
|
|
}
|
|
|
|
|
|
|
|
func readFile(t *testing.T, f string) string {
|
|
|
|
b, err := os.ReadFile(f)
|
|
|
|
require.Nil(t, err)
|
|
|
|
return string(b)
|
|
|
|
}
|