From c7c403ee42309d11d77f95ef66d2e5c058d7524c Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 10 Aug 2020 05:51:32 +0200 Subject: [PATCH] Add ADR 027 test vector --- packages/proto-signing/src/adr27.spec.ts | 62 ++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/packages/proto-signing/src/adr27.spec.ts b/packages/proto-signing/src/adr27.spec.ts index 785cb43c..f8221496 100644 --- a/packages/proto-signing/src/adr27.spec.ts +++ b/packages/proto-signing/src/adr27.spec.ts @@ -46,5 +46,67 @@ describe("adr27", () => { expect(omitDefault(Review.values["ACCEPTED"])).toEqual(Review.values["ACCEPTED"]); expect(omitDefault(Review.values["UNSPECIFIED"])).toEqual(null); }); + + it("can be used to reproduce ADR 027 test vector", () => { + const proto = ` + // Article.proto + + package blog; + syntax = "proto3"; + + enum Type { + UNSPECIFIED = 0; + IMAGES = 1; + NEWS = 2; + }; + + enum Review { + UNSPECIFIED = 0; + ACCEPTED = 1; + REJECTED = 2; + }; + + message Article { + string title = 1; + string description = 2; + uint64 created = 3; + uint64 updated = 4; + bool public = 5; + bool promoted = 6; + Type type = 7; + Review review = 8; + repeated string comments = 9; + repeated string backlinks = 10; + }; + `; + const root = parse(proto).root; + + // eslint-disable-next-line @typescript-eslint/naming-convention + const Article = root.lookupType("blog.Article"); + // eslint-disable-next-line @typescript-eslint/naming-convention + const Type = root.lookupEnum("blog.Type"); + // eslint-disable-next-line @typescript-eslint/naming-convention + const Review = root.lookupEnum("blog.Review"); + + const expected = fromHex( + "0a1654686520776f726c64206e65656473206368616e676518e8bebec8bc2e280138024a084e696365206f6e654a095468616e6b20796f75", + ); + + const serialization = Uint8Array.from( + Article.encode({ + title: omitDefault("The world needs change"), + description: omitDefault(""), + created: omitDefault(1596806111080), + updated: omitDefault(0), + public: omitDefault(true), + promoted: omitDefault(false), + type: omitDefault(Type.values["NEWS"]), + review: omitDefault(Review.values["UNSPECIFIED"]), + comments: omitDefault(["Nice one", "Thank you"]), + backlinks: omitDefault([]), + }).finish(), + ); + expect(serialization).toEqual(expected); + }); }); });