Skip to content
go-libfossil Artifact [1e70d85064]
Login

Artifact [1e70d85064]

Artifact 1e70d85064a9e388c4a82078819a0dab52d504d1b59eaa057ffd8637c76b2171:


package conformance

import (
	"path/filepath"
	"testing"

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

// syncVectorCase checks one byte-exact draft-fossil-sync-protocol-00 Appendix A
// wire vector stored under testdata/sync/.
type syncVectorCase struct {
	Case
	file string
	run  func(msg *xfer.Message) error
}

func syncVectorCases() []syncVectorCase {
	return []syncVectorCase{
		{
			Case: Case{ID: "sync.a.1.round1-reply", Req: "SP-9.1-1"},
			file: "sync_a1_round1_reply.txt",
			run:  checkSyncVectorA1R1,
		},
		{
			Case: Case{ID: "sync.a.1.round2-file-cards", Req: "SP-7.1-1"},
			file: "sync_a1_round2_file_cards.txt",
			run:  checkSyncVectorA1R2Files,
		},
		{
			Case: Case{ID: "sync.a.1.round2-reply", Req: "SP-7.1-1"},
			file: "sync_a1_round2_reply.txt",
			run:  checkSyncVectorA1R2Reply,
		},
		{
			Case: Case{ID: "sync.a.2.sha3-gate-reply", Req: "SP-7.6-1"},
			file: "sync_a2_reply.txt",
			run:  checkSyncVectorA2,
		},
	}
}

func TestSyncVectors(t *testing.T) {
	for _, vc := range syncVectorCases() {
		check(t, markSync(vc.Case), func(t *testing.T) error {
			raw, stripped, err := loadGuardedVector(filepath.Join("testdata", "sync", vc.file))
			if err != nil {
				return err
			}
			if _, err := deck.Parse(raw); err == nil {
				return expectf("guarded vector %s parsed as an artifact", vc.file)
			}
			msg, err := xfer.DecodeUncompressed(stripped)
			if err != nil {
				return expectf("DecodeUncompressed: %v", err)
			}
			return vc.run(msg)
		})
	}
}

func checkSyncVectorA1R1(msg *xfer.Message) error {
	// The trailing "# timestamp ..." comment line is ignored on decode (ยง5.2).
	if len(msg.Cards) != 5 {
		return expectf("card count = %d, want 5", len(msg.Cards))
	}
	p, ok := msg.Cards[0].(*xfer.PragmaCard)
	if !ok || p.Name != "server-version" || len(p.Values) != 3 || p.Values[0] != "22800" {
		return expectf("card 0 = %#v, want pragma server-version 22800 ...", msg.Cards[0])
	}
	for _, idx := range []int{1, 2} {
		if _, ok := msg.Cards[idx].(*xfer.GimmeCard); !ok {
			return expectf("card %d = %T, want *xfer.GimmeCard", idx, msg.Cards[idx])
		}
	}
	for _, idx := range []int{3, 4} {
		if _, ok := msg.Cards[idx].(*xfer.IGotCard); !ok {
			return expectf("card %d = %T, want *xfer.IGotCard", idx, msg.Cards[idx])
		}
	}
	return nil
}

func checkSyncVectorA1R2Files(msg *xfer.Message) error {
	if len(msg.Cards) != 3 {
		return expectf("card count = %d, want 3", len(msg.Cards))
	}
	if err := checkVectorFileCard(msg.Cards[0], 272, true); err != nil {
		return err
	}
	if err := checkVectorFileCard(msg.Cards[1], 26, false); err != nil {
		return err
	}
	g, ok := msg.Cards[2].(*xfer.GimmeCard)
	const wantGimme = "4361252d0f450879dcd2e0b6a42b95ffe8a41d8e948130253ae8eefa7f4b79ea"
	if !ok || g.UUID != wantGimme {
		return expectf("card 2 = %#v, want gimme %s", msg.Cards[2], wantGimme)
	}
	return nil
}

func checkSyncVectorA1R2Reply(msg *xfer.Message) error {
	if len(msg.Cards) != 3 {
		return expectf("card count = %d, want 3", len(msg.Cards))
	}
	p, ok := msg.Cards[0].(*xfer.PragmaCard)
	if !ok || p.Name != "server-version" {
		return expectf("card 0 = %#v, want pragma server-version", msg.Cards[0])
	}
	if err := checkVectorFileCard(msg.Cards[1], 270, true); err != nil {
		return err
	}
	return checkVectorFileCard(msg.Cards[2], 31, false)
}

func checkVectorFileCard(card xfer.Card, wantSize int, isManifest bool) error {
	fc, ok := card.(*xfer.FileCard)
	if !ok {
		return expectf("card = %T, want *xfer.FileCard", card)
	}
	if len(fc.Content) != wantSize {
		return expectf("file %s payload size = %d, want %d", fc.UUID, len(fc.Content), wantSize)
	}
	if got := hash.SHA3(fc.Content); got != fc.UUID {
		return expectf("file payload SHA3-256 = %s, want %s", got, fc.UUID)
	}
	if isManifest {
		if _, err := deck.Parse(fc.Content); err != nil {
			return expectf("manifest payload failed deck.Parse: %v", err)
		}
	}
	return nil
}

func checkSyncVectorA2(msg *xfer.Message) error {
	if len(msg.Cards) != 3 {
		return expectf("card count = %d, want 3", len(msg.Cards))
	}
	ec, ok := msg.Cards[0].(*xfer.ErrorCard)
	if !ok || ec.Message != "Fossil version 2.0 or later required." {
		return expectf("card 0 = %#v, want SHA3 gate error", msg.Cards[0])
	}
	cs, ok := msg.Cards[1].(*xfer.CloneSeqNoCard)
	if !ok || cs.SeqNo != 0 {
		return expectf("card 1 = %#v, want clone_seqno 0", msg.Cards[1])
	}
	if _, ok := msg.Cards[2].(*xfer.PushCard); !ok {
		return expectf("card 2 = %T, want *xfer.PushCard", msg.Cards[2])
	}
	return nil
}