-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tests for fsstorage backend
- Loading branch information
1 parent
670b553
commit 22df8e8
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package hibp | ||
|
||
import ( | ||
"bytes" | ||
"github.com/klauspost/compress/zstd" | ||
"io" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestFSStorage(t *testing.T) { | ||
testWriteRead := func(t *testing.T, useCompression bool) { | ||
key := "key" | ||
|
||
tmpDir := t.TempDir() | ||
|
||
storage := newFSStorage(tmpDir, !useCompression) | ||
|
||
err := storage.Save(key, "etag", []byte("data")) | ||
if err != nil { | ||
t.Fatalf("could not write: %v", err) | ||
} | ||
|
||
// First, let's check the raw file | ||
var reader io.Reader | ||
|
||
// We have to explicitly refer to the upper-cased key as Save/Load perform this operation internally. | ||
// Depending on the file system that does make a difference. | ||
file, err := os.Open(storage.filePath(strings.ToUpper(key))) | ||
if err != nil { | ||
t.Fatalf("could not open file: %v", err) | ||
} | ||
defer file.Close() | ||
|
||
reader = file | ||
|
||
if useCompression { | ||
dec, err := zstd.NewReader(file) | ||
if err != nil { | ||
t.Fatalf("could not create decompressor: %v", err) | ||
} | ||
|
||
defer dec.Close() | ||
|
||
reader = dec | ||
} | ||
|
||
raw, err := io.ReadAll(reader) | ||
if err != nil { | ||
t.Fatalf("could not read file: %v", err) | ||
} | ||
|
||
if !bytes.Equal([]byte("etag\ndata"), raw) { | ||
t.Fatalf("unexpected data: %q", raw) | ||
} | ||
|
||
// Then, let's check the API | ||
etag, err := storage.LoadETag(key) | ||
if err != nil { | ||
t.Fatalf("could not read etag: %v", err) | ||
} | ||
|
||
if etag != "etag" { | ||
t.Fatalf("unexpected etag: %q", etag) | ||
} | ||
|
||
readCloser, err := storage.LoadData(key) | ||
if err != nil { | ||
t.Fatalf("could not open reader: %v", err) | ||
} | ||
defer readCloser.Close() | ||
|
||
all, err := io.ReadAll(readCloser) | ||
if err != nil { | ||
t.Fatalf("could not read file: %v", err) | ||
} | ||
|
||
if !bytes.Equal([]byte("data"), all) { | ||
t.Fatalf("unexpected data: %q", all) | ||
} | ||
|
||
} | ||
|
||
t.Run("write and read data without compression", func(t *testing.T) { | ||
testWriteRead(t, false) | ||
}) | ||
|
||
t.Run("write and read data with compression", func(t *testing.T) { | ||
testWriteRead(t, true) | ||
}) | ||
} |