enodia-sentinal/go-agent/internal/assurance/chain_test.go

55 lines
1.5 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package assurance
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestAppendBuildsDurableLinkedRecords(t *testing.T) {
directory := t.TempDir()
chain, err := New(directory)
if err != nil {
t.Fatal(err)
}
firstPath := filepath.Join(directory, "first.json")
secondPath := filepath.Join(directory, "second.log")
os.WriteFile(firstPath, []byte("first"), 0o600)
os.WriteFile(secondPath, []byte("second"), 0o600)
first, err := chain.Append("snapshot-json", firstPath, time.Unix(1000, 0))
if err != nil {
t.Fatal(err)
}
second, err := chain.Append("snapshot-log", secondPath, time.Unix(1001, 0))
if err != nil {
t.Fatal(err)
}
if first.ChainHash == "" || second.PrevHash != first.ChainHash || second.ChainHash == first.ChainHash {
t.Fatalf("first=%#v second=%#v", first, second)
}
records, err := ReadAll(chain.Path)
if err != nil || len(records) != 2 {
t.Fatalf("records=%#v err=%v", records, err)
}
info, err := os.Stat(chain.Path)
if err != nil || info.Mode().Perm() != 0o600 {
t.Fatalf("chain mode info=%v err=%v", info, err)
}
}
func TestAppendUnavailableArtifactMatchesPythonFailOpenRecord(t *testing.T) {
chain, err := New(t.TempDir())
if err != nil {
t.Fatal(err)
}
record, err := chain.Append("missing", "/definitely/missing", time.Unix(1, 0))
if err != nil {
t.Fatal(err)
}
if record.Size != 0 || record.SHA256 != "" || record.ChainHash == "" {
t.Fatalf("record=%#v", record)
}
}