| 1 | package protocol |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestProtocolConstants(t *testing.T) { |
| 10 | if ProtocolID != "reasonix.extension.v1" { |
| 11 | t.Fatalf("ProtocolID = %q", ProtocolID) |
| 12 | } |
| 13 | if ProtocolMajor != 1 || ProtocolVersion != "1" { |
| 14 | t.Fatalf("ProtocolMajor/ProtocolVersion = %d/%q", ProtocolMajor, ProtocolVersion) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | func TestCompareProtocolVersion(t *testing.T) { |
| 19 | if err := CompareProtocolVersion(ProtocolID, "1"); err != nil { |
| 20 | t.Fatalf("exact match rejected: %v", err) |
| 21 | } |
| 22 | tests := []struct { |
| 23 | name string |
| 24 | id string |
| 25 | version string |
| 26 | reason ErrorReason |
| 27 | }{ |
| 28 | {"wrong id", "reasonix.extension.v2", "1", ErrUnsupportedVersion}, |
| 29 | {"empty id", "", "1", ErrUnsupportedVersion}, |
| 30 | {"wrong major", ProtocolID, "2", ErrUnsupportedVersion}, |
| 31 | {"zero major", ProtocolID, "0", ErrUnsupportedVersion}, |
| 32 | {"non-numeric version", ProtocolID, "v1", ErrProtocolError}, |
| 33 | {"empty version", ProtocolID, "", ErrProtocolError}, |
| 34 | } |
| 35 | for _, tt := range tests { |
| 36 | t.Run(tt.name, func(t *testing.T) { |
| 37 | err := CompareProtocolVersion(tt.id, tt.version) |
| 38 | if err == nil { |
| 39 | t.Fatal("expected an error") |
| 40 | } |
| 41 | var protocolErr *ProtocolError |
| 42 | if !errors.As(err, &protocolErr) { |
| 43 | t.Fatalf("error type = %T, want *ProtocolError", err) |
| 44 | } |
| 45 | if protocolErr.Reason != tt.reason { |
| 46 | t.Fatalf("reason = %q, want %q", protocolErr.Reason, tt.reason) |
| 47 | } |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestHandshakeIdentityEmbedsSchemaHash(t *testing.T) { |
| 53 | identity := HandshakeIdentity() |
| 54 | if !strings.Contains(identity, ProtocolID) || !strings.Contains(identity, SchemaHash()) { |
| 55 | t.Fatalf("HandshakeIdentity = %q, want protocol ID and schema hash embedded", identity) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestFrozenLimitsSanity(t *testing.T) { |
| 60 | if FrameBytes != 8<<20 { |
| 61 | t.Fatalf("FrameBytes = %d, want %d", FrameBytes, 8<<20) |
| 62 | } |
| 63 | if ExternalizeFieldBytes != 64<<10 { |
| 64 | t.Fatalf("ExternalizeFieldBytes = %d, want %d", ExternalizeFieldBytes, 64<<10) |
| 65 | } |
| 66 | if ContentRefChunkBytes != 256<<10 { |
| 67 | t.Fatalf("ContentRefChunkBytes = %d, want %d", ContentRefChunkBytes, 256<<10) |
| 68 | } |
| 69 | if ContentRefObjectBytes != 8<<20 { |
| 70 | t.Fatalf("ContentRefObjectBytes = %d, want %d", ContentRefObjectBytes, 8<<20) |
| 71 | } |
| 72 | if ExternalizeFieldBytes >= ContentRefChunkBytes || ContentRefChunkBytes >= ContentRefObjectBytes { |
| 73 | t.Fatal("limits must be strictly ordered: field < chunk < object") |
| 74 | } |
| 75 | if ContentRefObjectBytes > FrameBytes { |
| 76 | t.Fatal("a content object must fit inside one frame ceiling") |
| 77 | } |
| 78 | limits := FrozenLimits() |
| 79 | if limits.FrameBytes != FrameBytes || limits.ExternalizeFieldBytes != ExternalizeFieldBytes || |
| 80 | limits.ContentRefChunkBytes != ContentRefChunkBytes || limits.ContentRefObjectBytes != ContentRefObjectBytes { |
| 81 | t.Fatalf("FrozenLimits = %+v does not match the constants", limits) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestValidateFrameSize(t *testing.T) { |
| 86 | for _, ok := range []int{0, 1, FrameBytes - 1, FrameBytes} { |
| 87 | if err := ValidateFrameSize(ok); err != nil { |
| 88 | t.Fatalf("ValidateFrameSize(%d) = %v, want nil", ok, err) |
| 89 | } |
| 90 | } |
| 91 | for _, bad := range []int{-1, FrameBytes + 1, FrameBytes * 2} { |
| 92 | err := ValidateFrameSize(bad) |
| 93 | if err == nil { |
| 94 | t.Fatalf("ValidateFrameSize(%d) accepted an oversized frame", bad) |
| 95 | } |
| 96 | var protocolErr *ProtocolError |
| 97 | if !errors.As(err, &protocolErr) || protocolErr.Reason != ErrFrameTooLarge { |
| 98 | t.Fatalf("ValidateFrameSize(%d) error = %v, want frame_too_large ProtocolError", bad, err) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestRequiresExternalization(t *testing.T) { |
| 104 | if RequiresExternalization(ExternalizeFieldBytes) { |
| 105 | t.Fatal("payload at the threshold must stay inline") |
| 106 | } |
| 107 | if !RequiresExternalization(ExternalizeFieldBytes + 1) { |
| 108 | t.Fatal("payload above the threshold must externalize") |
| 109 | } |
| 110 | } |
| 111 |