Skip to content
go-libfossil Artifact [006252b598]
Login

Artifact [006252b598]

Artifact 006252b5988fdd48cbd665b6a97d126056b58c98dc67ab0da4e11deca7998762:


package conformance

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"github.com/danmestas/go-libfossil/internal/deck"
	"github.com/danmestas/go-libfossil/internal/hash"
)

// artifactVectorCase checks one byte-exact RFC Appendix A vector stored under
// testdata/artifact/. Each file carries leading "#" guard lines so Fossil will
// never crosslink it as a live artifact; the guard itself is verified here too.
type artifactVectorCase struct {
	Case
	file string
	run  func(raw, stripped []byte) error
}

func artifactVectorCases() []artifactVectorCase {
	return []artifactVectorCase{
		{
			Case: Case{ID: "artifact.a.1.1.sha1-manifest", Req: "AF-3.1-1"},
			file: "artifact_a1_1_sha1_manifest.txt",
			run:  checkArtifactVectorA11,
		},
		{
			Case: Case{ID: "artifact.a.1.2.sha3-technote", Req: "AF-3.1-1"},
			file: "artifact_a1_2_sha3_technote.txt",
			run:  checkArtifactVectorA12,
		},
		{
			Case: Case{ID: "artifact.a.3.rcard-manifest", Req: "AF-6.3-2"},
			file: "artifact_a3_rcard_manifest.txt",
			run:  checkArtifactVectorA3,
		},
	}
}

func TestArtifactVectors(t *testing.T) {
	for _, vc := range artifactVectorCases() {
		check(t, vc.Case, func(t *testing.T) error {
			raw, stripped, err := loadGuardedVector(filepath.Join("testdata", "artifact", vc.file))
			if err != nil {
				return err
			}
			if _, err := deck.Parse(raw); err == nil {
				return expectf("guarded vector %s parsed without stripping", vc.file)
			}
			return vc.run(raw, stripped)
		})
	}
}

// loadGuardedVector reads path and returns both the raw guarded bytes on disk
// and the bytes after stripping leading "#" guard lines.
func loadGuardedVector(path string) (raw, stripped []byte, err error) {
	if path == "" {
		panic("conformance: empty vector path")
	}
	raw, err = os.ReadFile(path)
	if err != nil {
		return nil, nil, err
	}
	if !bytes.HasPrefix(raw, []byte("#")) {
		return nil, nil, fmt.Errorf("%s: missing leading '#' guard block", path)
	}
	stripped = raw
	const maxGuardLines = 64
	for i := 0; i < maxGuardLines && bytes.HasPrefix(stripped, []byte("#")); i++ {
		nl := bytes.IndexByte(stripped, '\n')
		if nl < 0 {
			return nil, nil, fmt.Errorf("%s: guard line without newline", path)
		}
		stripped = stripped[nl+1:]
	}
	return raw, stripped, nil
}

func checkArtifactVectorA11(_, data []byte) error {
	const wantSHA1 = "a28c83647dfa805f05f3204a7e146eb1f0d90505"
	const wantR = "d41d8cd98f00b204e9800998ecf8427e"
	if got := hash.SHA1(data); got != wantSHA1 {
		return expectf("SHA-1 = %s, want %s", got, wantSHA1)
	}
	d, err := deck.Parse(data)
	if err != nil {
		return expectf("deck.Parse: %v", err)
	}
	if d.Type != deck.Checkin {
		return expectf("type = %d, want Checkin", d.Type)
	}
	if len(d.P) != 0 || d.R != wantR || d.U == nil || *d.U != "drh" {
		return expectf("P=%v R=%q U=%v", d.P, d.R, d.U)
	}
	gotR, err := d.ComputeR(nil)
	if err != nil || gotR != wantR {
		return expectf("ComputeR = %q (%v), want %q", gotR, err, wantR)
	}
	return nil
}

func checkArtifactVectorA12(_, data []byte) error {
	const wantSHA3 = "2fa494a18a766efe9a8ba706d669b7157d5329e1fbcc2a6d6c15a386d113218d"
	if got := hash.SHA3(data); got != wantSHA3 {
		return expectf("SHA3-256 = %s, want %s", got, wantSHA3)
	}
	d, err := deck.Parse(data)
	if err != nil {
		return expectf("deck.Parse: %v", err)
	}
	if d.Type != deck.Event {
		return expectf("type = %d, want Event", d.Type)
	}
	if len(d.W) != 415 || d.N != "text/x-fossil-wiki" || d.U == nil || *d.U != "drh" {
		return expectf("len(W)=%d N=%q U=%v", len(d.W), d.N, d.U)
	}
	return nil
}

func checkArtifactVectorA3(_, data []byte) error {
	const wantR = "95feb799536dc053e7968efb7cdd3ec3"
	d, err := deck.Parse(data)
	if err != nil {
		return expectf("deck.Parse: %v", err)
	}
	if d.Type != deck.Checkin || d.R != wantR {
		return expectf("type=%d R=%q, want Checkin and %q", d.Type, d.R, wantR)
	}
	files := map[string][]byte{
		"alpha.txt": []byte("hello world\n"),
		"beta.txt":  []byte("fossil artifact format\n"),
		"gamma.txt": []byte("R card test\n"),
	}
	byHash := make(map[string][]byte, len(files))
	for name, content := range files {
		sum := hash.SHA3(content)
		byHash[sum] = content
		found := false
		for _, fc := range d.F {
			if fc.Name == name && fc.UUID == sum {
				found = true
			}
		}
		if !found {
			return expectf("F card for %s did not match SHA3-256 %s", name, sum)
		}
	}
	gotR, err := d.ComputeR(func(uuid string) ([]byte, error) {
		b, ok := byHash[uuid]
		if !ok {
			return nil, fmt.Errorf("missing %s", uuid)
		}
		return b, nil
	})
	if err != nil || gotR != wantR {
		return expectf("ComputeR = %q (%v), want %q", gotR, err, wantR)
	}
	return nil
}