@@ -17,6 +17,7 @@ package localstatequery
17
17
import (
18
18
"encoding/hex"
19
19
"fmt"
20
+ "math/big"
20
21
"os"
21
22
"reflect"
22
23
"testing"
@@ -30,6 +31,7 @@ type testDefinition struct {
30
31
CborHex string
31
32
Message protocol.Message
32
33
MessageType uint
34
+ Result interface {}
33
35
}
34
36
35
37
var tests = []testDefinition {
@@ -100,9 +102,19 @@ var tests = []testDefinition{
100
102
MessageType : MessageTypeQuery ,
101
103
},
102
104
{
103
- CborHex : string (readFile ("../../cardano-blueprint/src/api/examples/getSystemStart/result.cbor" )),
104
- Message : NewMsgResult ([]byte {5 }), // FIXME: not correct and should also check SystemStart decoder
105
+ CborHex : string (readFile ("../../cardano-blueprint/src/api/examples/getSystemStart/result.cbor" )),
106
+ Message : NewMsgResult (unsafeCbor (
107
+ SystemStartResult {
108
+ Year : unsafeBigInt ([]byte ("703941703872597091335551638723343370661404331303175992839224705786473148" )),
109
+ Day : - 4205646576720553090 ,
110
+ Picoseconds : unsafeBigInt ([]byte ("-554918151390414980540174869115975093799476848534297657333456993160799627" )),
111
+ })),
105
112
MessageType : MessageTypeResult ,
113
+ Result : SystemStartResult {
114
+ Year : unsafeBigInt ([]byte ("703941703872597091335551638723343370661404331303175992839224705786473148" )),
115
+ Day : - 4205646576720553090 ,
116
+ Picoseconds : unsafeBigInt ([]byte ("-554918151390414980540174869115975093799476848534297657333456993160799627" )),
117
+ },
106
118
},
107
119
}
108
120
@@ -116,6 +128,22 @@ func TestDecode(t *testing.T) {
116
128
if err != nil {
117
129
t .Fatalf ("failed to decode CBOR: %s" , err )
118
130
}
131
+ // cast msg to MsgResult and further try to decode cbor
132
+ if m , ok := msg .(* MsgResult ); ok && test .Result != nil {
133
+ var decoded = reflect .New (reflect .TypeOf (test .Result ))
134
+ _ , err := cbor .Decode (m .Result , decoded .Interface ())
135
+ if err != nil {
136
+ t .Fatalf ("failed to decode result: %s" , err )
137
+ }
138
+ if ! reflect .DeepEqual (decoded .Interface (), test .Result ) {
139
+ t .Fatalf (
140
+ "MsgResult content did not decode to expected Result object\n got: %#v\n wanted: %#v" ,
141
+ decoded .Interface (),
142
+ test .Result ,
143
+ )
144
+ }
145
+ }
146
+
119
147
// Set the raw CBOR so the comparison should succeed
120
148
test .Message .SetCbor (cborData )
121
149
if m , ok := msg .(* MsgQuery ); ok {
@@ -148,6 +176,24 @@ func TestEncode(t *testing.T) {
148
176
}
149
177
}
150
178
179
+ // Helper function to encode to cbor or panic
180
+ func unsafeCbor (data interface {}) []byte {
181
+ cborData , err := cbor .Encode (data )
182
+ if err != nil {
183
+ panic (fmt .Sprintf ("error encoding to CBOR: %s" , err ))
184
+ }
185
+ return cborData
186
+ }
187
+
188
+ func unsafeBigInt (text []byte ) big.Int {
189
+ var i big.Int
190
+ err := i .UnmarshalText (text )
191
+ if err != nil {
192
+ panic (fmt .Sprintf ("error unmarshalling text to big.Int: %s" , err ))
193
+ }
194
+ return i
195
+ }
196
+
151
197
// Helper function to allow inline reading of a file without capturing the error
152
198
func readFile (path string ) []byte {
153
199
data , err := os .ReadFile (path )
0 commit comments