package listing import ( "bytes" "encoding/json" "os" "testing" "time" "git.stinnesbeck.com/nils/allmende/internal/identity" ) // The browser re-implements canonicalization and verification. This golden // file is produced by the Go implementation and consumed by the TypeScript // tests (web/test), so the two can never drift apart unnoticed. // // Refresh it after an intentional format change with: // // UPDATE_FIXTURES=1 go test ./internal/listing -run TestGoldenFixture const fixturePath = "testdata/fixture.json" const ( fixtureNow = 1_800_000_000 fixtureDifficulty = 12 // Awkward text on purpose: newline, umlauts, an emoji, quotes, a // backslash and U+2028 (legal in JSON, but a classic JavaScript trap). fixtureDescription = "Zeile 1\nZeile 2 – Überraschung \U0001F3E0 \"quoted\" \\ back 
 end" ) type fixtureFile struct { Now int64 `json:"now"` Difficulty int `json:"difficulty"` Canonical string `json:"canonical"` // the exact bytes Go signed Listing json.RawMessage `json:"listing"` // the same record as plain JSON } func buildFixture(t *testing.T) (*Listing, []byte) { t.Helper() root, err := identity.FromPhrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") if err != nil { t.Fatal(err) } seed := bytes.Repeat([]byte{7}, 32) devKey, err := identity.FromSeed(seed) if err != nil { t.Fatal(err) } now := time.Unix(fixtureNow, 0) cert, err := identity.IssueCert(root, devKey.PublicKeyHex(), now.Add(-time.Hour), now.Add(90*24*time.Hour)) if err != nil { t.Fatal(err) } l := &Listing{ Version: Version, ID: "00112233445566778899aabbccddeeff", Seq: 3, CreatedAt: now.Unix(), ExpiresAt: now.Add(14 * 24 * time.Hour).Unix(), Kind: "rent", Category: "apartment", Geohash: "u1xjhx", Price: &Price{AmountCents: 123450, Currency: "EUR", Period: "month"}, AreaM2: 62, RoomsX2: 5, Features: []string{"balcony", "Aufzug"}, Title: "Helle 2,5-Zimmer-Wohnung – Altbau", Description: fixtureDescription, } if err := l.Seal(&identity.Device{Key: devKey, Cert: cert}, fixtureDifficulty); err != nil { t.Fatal(err) } canon, err := l.Bytes() if err != nil { t.Fatal(err) } return l, canon } func TestGoldenFixture(t *testing.T) { l, canon := buildFixture(t) // Go itself must accept what it signed. if err := l.Check(time.Unix(fixtureNow, 0), fixtureDifficulty); err != nil { t.Fatal(err) } plain, err := json.MarshalIndent(l, " ", " ") if err != nil { t.Fatal(err) } want, err := json.MarshalIndent(fixtureFile{ Now: fixtureNow, Difficulty: fixtureDifficulty, Canonical: string(canon), Listing: plain, }, "", " ") if err != nil { t.Fatal(err) } want = append(want, '\n') if os.Getenv("UPDATE_FIXTURES") != "" { if err := os.MkdirAll("testdata", 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(fixturePath, want, 0o644); err != nil { t.Fatal(err) } t.Log("fixture updated") return } got, err := os.ReadFile(fixturePath) if err != nil { t.Fatalf("%v (run with UPDATE_FIXTURES=1 to create it)", err) } if !bytes.Equal(got, want) { t.Fatal("golden fixture is stale: the signed format changed. If intentional, run:\n" + " UPDATE_FIXTURES=1 go test ./internal/listing -run TestGoldenFixture\n" + "and make sure the web verifier (web/src/verify.ts) matches") } }