forked from filecoin-project/go-hamt-ipld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hamt_utils_test.go
58 lines (49 loc) · 1.11 KB
/
hamt_utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package hamt
import (
"fmt"
"io"
cbg "github.com/whyrusleeping/cbor-gen"
)
// test utilities
// from https://github.com/polydawn/refmt/blob/3d65705ee9f12dc0dfcc0dc6cf9666e97b93f339/cbor/cborFixtures_test.go#L81-L93
func bcat(bss ...[]byte) []byte {
l := 0
for _, bs := range bss {
l += len(bs)
}
rbs := make([]byte, 0, l)
for _, bs := range bss {
rbs = append(rbs, bs...)
}
return rbs
}
func b(b byte) []byte { return []byte{b} }
// A CBOR-marshalable byte array.
type CborByteArray []byte
func (c *CborByteArray) MarshalCBOR(w io.Writer) error {
if err := cbg.WriteMajorTypeHeader(w, cbg.MajByteString, uint64(len(*c))); err != nil {
return err
}
_, err := w.Write(*c)
return err
}
func (c *CborByteArray) UnmarshalCBOR(r io.Reader) error {
maj, extra, err := cbg.CborReadHeader(r)
if err != nil {
return err
}
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array")
}
if uint64(cap(*c)) < extra {
*c = make([]byte, extra)
}
if _, err := io.ReadFull(r, *c); err != nil {
return err
}
return nil
}
func cborstr(s string) *CborByteArray {
v := CborByteArray(s)
return &v
}