add content-addressed hak source mode (#7)
Reviewed-on: #7 Co-authored-by: vickydotbat <vickydotbat@tutamail.com> Co-committed-by: vickydotbat <vickydotbat@tutamail.com>
This commit was merged in pull request #7.
This commit is contained in:
@@ -2,9 +2,72 @@ package erf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func writeTempPayload(t *testing.T, payload []byte) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "payload.bin")
|
||||
if err := os.WriteFile(path, payload, 0o644); err != nil {
|
||||
t.Fatalf("write payload: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func TestWriteRejectsSourcePathSHA256Mismatch(t *testing.T) {
|
||||
payload := []byte("hello world")
|
||||
path := writeTempPayload(t, payload)
|
||||
|
||||
archive := New("HAK ", []Resource{{
|
||||
Name: "asset",
|
||||
Type: 0x0003,
|
||||
SourcePath: path,
|
||||
Size: int64(len(payload)),
|
||||
ExpectedSHA256: strings.Repeat("a", 64),
|
||||
}})
|
||||
|
||||
var buf bytes.Buffer
|
||||
err := Write(&buf, archive)
|
||||
if err == nil {
|
||||
t.Fatal("expected sha256 mismatch error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "sha256 mismatch") {
|
||||
t.Fatalf("error = %v, want sha256 mismatch", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteAcceptsMatchingSourcePathSHA256(t *testing.T) {
|
||||
payload := []byte("hello world")
|
||||
path := writeTempPayload(t, payload)
|
||||
sum := sha256.Sum256(payload)
|
||||
|
||||
archive := New("HAK ", []Resource{{
|
||||
Name: "asset",
|
||||
Type: 0x0003,
|
||||
SourcePath: path,
|
||||
Size: int64(len(payload)),
|
||||
ExpectedSHA256: hex.EncodeToString(sum[:]),
|
||||
}})
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := Write(&buf, archive); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
|
||||
decoded, err := Read(bytes.NewReader(buf.Bytes()))
|
||||
if err != nil {
|
||||
t.Fatalf("read: %v", err)
|
||||
}
|
||||
if len(decoded.Resources) != 1 || string(decoded.Resources[0].Data) != string(payload) {
|
||||
t.Fatalf("unexpected payload: %#v", decoded.Resources)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArchiveRoundTrip(t *testing.T) {
|
||||
archive := New("MOD ", []Resource{
|
||||
{Name: "module", Type: 0x07DE, Data: []byte("ifo")},
|
||||
|
||||
Reference in New Issue
Block a user