|
| 1 | +// Copyright 2026 Blink Labs Software |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package common |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/blinklabs-io/gouroboros/cbor" |
| 21 | + test "github.com/blinklabs-io/gouroboros/internal/test" |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestCommonDispatchersAcceptListLengthEncodings(t *testing.T) { |
| 27 | + credential := Credential{ |
| 28 | + CredType: CredentialTypeAddrKeyHash, |
| 29 | + Credential: Blake2b224{1, 2, 3}, |
| 30 | + } |
| 31 | + |
| 32 | + t.Run("CertificateWrapper", func(t *testing.T) { |
| 33 | + canonical, err := cbor.Encode(&StakeRegistrationCertificate{ |
| 34 | + CertType: uint(CertificateTypeStakeRegistration), |
| 35 | + StakeCredential: credential, |
| 36 | + }) |
| 37 | + require.NoError(t, err) |
| 38 | + for _, encoding := range test.CanonicalAndNonShortestList(canonical) { |
| 39 | + t.Run(encoding.Name, func(t *testing.T) { |
| 40 | + var decoded CertificateWrapper |
| 41 | + require.NoError(t, decoded.UnmarshalCBOR(encoding.Data)) |
| 42 | + require.IsType(t, &StakeRegistrationCertificate{}, decoded.Certificate) |
| 43 | + }) |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("Drep", func(t *testing.T) { |
| 48 | + canonical, err := cbor.Encode(Drep{Type: DrepTypeAbstain}) |
| 49 | + require.NoError(t, err) |
| 50 | + for _, encoding := range test.CanonicalAndNonShortestList(canonical) { |
| 51 | + t.Run(encoding.Name, func(t *testing.T) { |
| 52 | + var decoded Drep |
| 53 | + require.NoError(t, decoded.UnmarshalCBOR(encoding.Data)) |
| 54 | + require.Equal(t, DrepTypeAbstain, decoded.Type) |
| 55 | + }) |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("PoolRelay", func(t *testing.T) { |
| 60 | + canonical, err := cbor.Encode([]any{1, uint32(3001), "relay.example"}) |
| 61 | + require.NoError(t, err) |
| 62 | + for _, encoding := range test.CanonicalAndNonShortestList(canonical) { |
| 63 | + t.Run(encoding.Name, func(t *testing.T) { |
| 64 | + var decoded PoolRelay |
| 65 | + require.NoError(t, decoded.UnmarshalCBOR(encoding.Data)) |
| 66 | + assert.Equal(t, PoolRelayTypeSingleHostName, decoded.Type) |
| 67 | + require.NotNil(t, decoded.Hostname) |
| 68 | + assert.Equal(t, "relay.example", *decoded.Hostname) |
| 69 | + }) |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("Nonce", func(t *testing.T) { |
| 74 | + canonical, err := cbor.Encode([]any{NonceTypeNeutral}) |
| 75 | + require.NoError(t, err) |
| 76 | + for _, encoding := range test.CanonicalAndNonShortestList(canonical) { |
| 77 | + t.Run(encoding.Name, func(t *testing.T) { |
| 78 | + var decoded Nonce |
| 79 | + require.NoError(t, decoded.UnmarshalCBOR(encoding.Data)) |
| 80 | + require.Equal(t, uint(NonceTypeNeutral), decoded.Type) |
| 81 | + }) |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + t.Run("NativeScript", func(t *testing.T) { |
| 86 | + canonical, err := cbor.Encode(NativeScriptInvalidBefore{Type: 4, Slot: 5}) |
| 87 | + require.NoError(t, err) |
| 88 | + for _, encoding := range test.CanonicalAndNonShortestList(canonical) { |
| 89 | + t.Run(encoding.Name, func(t *testing.T) { |
| 90 | + var decoded NativeScript |
| 91 | + require.NoError(t, decoded.UnmarshalCBOR(encoding.Data)) |
| 92 | + require.IsType(t, &NativeScriptInvalidBefore{}, decoded.Item()) |
| 93 | + }) |
| 94 | + } |
| 95 | + }) |
| 96 | +} |
0 commit comments