@@ -3,12 +3,15 @@ package dagcbor
3
3
import (
4
4
"bytes"
5
5
"crypto/rand"
6
+ "encoding/hex"
7
+ "math"
6
8
"strings"
7
9
"testing"
8
10
9
11
qt "github.com/frankban/quicktest"
10
12
cid "github.com/ipfs/go-cid"
11
13
14
+ "github.com/ipld/go-ipld-prime/datamodel"
12
15
"github.com/ipld/go-ipld-prime/fluent"
13
16
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
14
17
"github.com/ipld/go-ipld-prime/node/basicnode"
@@ -115,3 +118,65 @@ func TestRoundtripLinksAndBytes(t *testing.T) {
115
118
reconstructed := nb .Build ()
116
119
qt .Check (t , reconstructed , nodetests .NodeContentEquals , linkByteNode )
117
120
}
121
+
122
+ func TestInts (t * testing.T ) {
123
+ data := []struct {
124
+ name string
125
+ hex string
126
+ value uint64
127
+ intValue int64
128
+ intErr string
129
+ decodeErr string
130
+ }{
131
+ {"max uint64" , "1bffffffffffffffff" , math .MaxUint64 , 0 , "unsigned integer out of range of int64 type" , "" },
132
+ {"max int64" , "1b7fffffffffffffff" , math .MaxInt64 , math .MaxInt64 , "" , "" },
133
+ {"1" , "01" , 1 , 1 , "" , "" },
134
+ {"0" , "00" , 0 , 0 , "" , "" },
135
+ {"-1" , "20" , 0 , - 1 , "" , "" },
136
+ {"min int64" , "3b7fffffffffffffff" , 0 , math .MinInt64 , "" , "" },
137
+ {"~min uint64" , "3bfffffffffffffffe" , 0 , 0 , "" , "cbor: negative integer out of rage of int64 type" },
138
+ // TODO: 3bffffffffffffffff isn't properly handled by refmt, it's coerced to zero
139
+ // MaxUint64 gets overflowed here: https://github.com/polydawn/refmt/blob/30ac6d18308e584ca6a2e74ba81475559db94c5f/cbor/cborDecoderTerminals.go#L75
140
+ }
141
+
142
+ for _ , td := range data {
143
+ t .Run (td .name , func (t * testing.T ) {
144
+ buf , err := hex .DecodeString (td .hex ) // max uint64
145
+ qt .Assert (t , err , qt .IsNil )
146
+ nb := basicnode .Prototype .Any .NewBuilder ()
147
+ err = Decode (nb , bytes .NewReader (buf ))
148
+ if td .decodeErr != "" {
149
+ qt .Assert (t , err , qt .IsNotNil )
150
+ qt .Assert (t , err .Error (), qt .Equals , td .decodeErr )
151
+ return
152
+ }
153
+ qt .Assert (t , err , qt .IsNil )
154
+ n := nb .Build ()
155
+
156
+ ii , err := n .AsInt ()
157
+ if td .intErr != "" {
158
+ qt .Assert (t , err .Error (), qt .Equals , td .intErr )
159
+ } else {
160
+ qt .Assert (t , err , qt .IsNil )
161
+ qt .Assert (t , ii , qt .Equals , int64 (td .intValue ))
162
+ }
163
+
164
+ // if the number is outside of the positive int64 range, we should be able
165
+ // to access it as a UintNode and be able to access the full int64 range
166
+ uin , ok := n .(datamodel.UintNode )
167
+ if td .value <= math .MaxInt64 {
168
+ qt .Assert (t , ok , qt .IsFalse )
169
+ } else {
170
+ qt .Assert (t , ok , qt .IsTrue )
171
+ val , err := uin .AsUint ()
172
+ qt .Assert (t , err , qt .IsNil )
173
+ qt .Assert (t , val , qt .Equals , uint64 (td .value ))
174
+ }
175
+
176
+ var byts bytes.Buffer
177
+ err = Encode (n , & byts )
178
+ qt .Assert (t , err , qt .IsNil )
179
+ qt .Assert (t , hex .EncodeToString (byts .Bytes ()), qt .Equals , td .hex )
180
+ })
181
+ }
182
+ }
0 commit comments