From 3d1a32e0e8d749e91d09d0a5164857be4f191172 Mon Sep 17 00:00:00 2001 From: Mildred Ki'Lya Date: Fri, 11 Mar 2016 20:58:29 +0100 Subject: [PATCH] Rework codec protocol buffer package - add protocol buffer reader (and use the /mdagv1 multicodec prefix) - regenerate protocol buffer code - make the codec package stream oriented - remove use of the Multicodec type as it is buffer oriented - general cleanup --- coding/Makefile | 29 +- coding/coding.go | 125 ++-- coding/coding_test.go | 195 +++--- coding/pb/Makefile | 27 +- coding/pb/ipld.pb.go | 1293 +++++++++++++++++++++--------------- coding/pb/ipld.pb.go.patch | 28 + coding/pb/ipld.proto | 2 +- coding/pb/pbcodec.go | 289 ++++---- coding/pb/pbcodec_test.go | 158 ----- coding/protobuf.testfile | Bin 0 -> 120 bytes 10 files changed, 1122 insertions(+), 1024 deletions(-) create mode 100644 coding/pb/ipld.pb.go.patch delete mode 100644 coding/pb/pbcodec_test.go create mode 100644 coding/protobuf.testfile diff --git a/coding/Makefile b/coding/Makefile index 680521e..33b8e8d 100644 --- a/coding/Makefile +++ b/coding/Makefile @@ -1,19 +1,34 @@ -pb/bin/multicodec: - $(MAKE) -C pb bin/multicodec +bin/multicodec: + mkdir -p bin + go get -d github.com/jbenet/go-multicodec/multicodec + go build -o "$@" github.com/jbenet/go-multicodec/multicodec bin/convert: cd bin; go build convert.go -json.testfile: pb/bin/multicodec Makefile +bin/msgio: + mkdir -p bin + go get -d github.com/jbenet/go-msgio/msgio + go build -o "$@" github.com/jbenet/go-msgio/msgio + +json.testfile: bin/multicodec Makefile : >$@ - pb/bin/multicodec header /multicodec >>$@ - pb/bin/multicodec header /json >>$@ + bin/multicodec header /multicodec >>$@ + bin/multicodec header /json >>$@ echo '{"@codec":"/json","abc":{"mlink":"QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V"}}' >>$@ -cbor.testfile: json.testfile - pb/bin/multicodec header /multicodec >$@ +cbor.testfile: bin/multicodec json.testfile + bin/multicodec header /multicodec >$@ ./convert -i $< -o $@.tmp -c '/cbor' cat $@.tmp >>$@ rm -f $@.tmp +protobuf.testfile: bin/multicodec bin/msgio + bin/multicodec header /mdagv1 >$@ + bin/multicodec header /protobuf/msgio >>$@ + mkdir -p dir + echo a >dir/a + echo b >dir/b + hash=`ipfs add -q -r dir | tail -n1` && \ + ipfs object get "$$hash" --enc=protobuf | bin/msgio wrap >>$@ diff --git a/coding/coding.go b/coding/coding.go index 96beda5..2765d5c 100644 --- a/coding/coding.go +++ b/coding/coding.go @@ -1,76 +1,101 @@ package coding import ( - mc "github.com/jbenet/go-multicodec" - mccbor "github.com/jbenet/go-multicodec/cbor" - mcmux "github.com/jbenet/go-multicodec/mux" + "bytes" + "fmt" + "io" - ipld "github.com/ipfs/go-ipld" pb "github.com/ipfs/go-ipld/coding/pb" - memory "github.com/ipfs/go-ipld/memory" + + ipld "github.com/ipfs/go-ipld" + stream "github.com/ipfs/go-ipld/coding/stream" + mc "github.com/jbenet/go-multicodec" +) + +var Header []byte + +const ( + HeaderPath = "/mdagv1" ) -// defaultCodec is the default applied if user does not specify a codec. -// Most new objects will never specify a codec. We track the codecs with -// the object so that multiple people using the same object will continue -// to marshal using the same codec. the only reason this is important is -// that the hashes must be the same. -var defaultCodec string +var StreamCodecs map[string]func(io.Reader) (stream.NodeReader, error) -var muxCodec *mcmux.Multicodec +type Codec int + +const ( + NoCodec Codec = 0 + CodecProtobuf Codec = iota +) func init() { - // by default, always encode things as cbor - defaultCodec = string(mc.HeaderPath(mccbor.Header)) - muxCodec = mcmux.MuxMulticodec([]mc.Multicodec{ - CborMulticodec(), - JsonMulticodec(), - pb.Multicodec(), - }, selectCodec) -} + Header = mc.Header([]byte(HeaderPath)) -// Multicodec returns a muxing codec that marshals to -// whatever codec makes sense depending on what information -// the IPLD object itself carries -func Multicodec() mc.Multicodec { - return muxCodec + StreamCodecs = map[string]func(io.Reader) (stream.NodeReader, error){ + pb.MsgIOHeaderPath: func(r io.Reader) (stream.NodeReader, error) { + return pb.Decode(mc.WrapHeaderReader(pb.MsgIOHeader, r)) + }, + } } -func selectCodec(v interface{}, codecs []mc.Multicodec) mc.Multicodec { - vn, ok := v.(*memory.Node) - if !ok { - return nil +func DecodeReader(r io.Reader) (stream.NodeReader, error) { + // get multicodec first header, should be mcmux.Header + err := mc.ConsumeHeader(r, Header) + if err != nil { + return nil, err } - codecKey, err := codecKey(*vn) + // get next header, to select codec + hdr, err := mc.ReadHeader(r) if err != nil { - return nil + return nil, err } - for _, c := range codecs { - if codecKey == string(mc.HeaderPath(c.Header())) { - return c - } - } + hdrPath := string(mc.HeaderPath(hdr)) - return nil // no codec + fun, ok := StreamCodecs[hdrPath] + if !ok { + return nil, fmt.Errorf("no codec for %s", hdrPath) + } + return fun(r) } -func codecKey(n memory.Node) (string, error) { - chdr, ok := (n)[ipld.CodecKey] - if !ok { - // if no codec is defined, use our default codec - chdr = defaultCodec - if pb.IsOldProtobufNode(n) { - chdr = string(pb.Header) - } +func Decode(r io.Reader) (interface{}, error) { + rd, err := DecodeReader(r) + if err != nil { + return nil, err } - chdrs, ok := chdr.(string) - if !ok { - // if chdr is not a string, cannot read codec. - return "", mc.ErrType + return stream.NewNodeFromReader(rd) +} + +func DecodeBytes(data []byte) (interface{}, error) { + return Decode(bytes.NewReader(data)) +} + +func HasHeader(data []byte) bool { + return len(data) >= len(Header) && bytes.Equal(data[:len(Header)], Header) +} + +func DecodeLegacyProtobufBytes(data []byte) (stream.NodeReader, error) { + return pb.RawDecode(data) +} + +func EncodeRaw(codec Codec, w io.Writer, node ipld.NodeIterator) error { + switch codec { + case CodecProtobuf: + return pb.Encode(w, node, true) + default: + return fmt.Errorf("Unknown codec %v", codec) } +} + +func Encode(codec Codec, w io.Writer, node ipld.NodeIterator) error { + w.Write(Header) + return EncodeRaw(codec, w, node) +} - return chdrs, nil +func EncodeBytes(codec Codec, node ipld.NodeIterator) ([]byte, error) { + var buf bytes.Buffer + err := Encode(codec, &buf, node) + return buf.Bytes(), err } diff --git a/coding/coding_test.go b/coding/coding_test.go index 20beb44..556d848 100644 --- a/coding/coding_test.go +++ b/coding/coding_test.go @@ -3,18 +3,18 @@ package coding import ( "bytes" "io/ioutil" - "reflect" + "os" "testing" + ipld "github.com/ipfs/go-ipld" + reader "github.com/ipfs/go-ipld/coding/stream" + rt "github.com/ipfs/go-ipld/coding/stream/test" memory "github.com/ipfs/go-ipld/memory" - - mc "github.com/jbenet/go-multicodec" - mctest "github.com/jbenet/go-multicodec/test" + assrt "github.com/mildred/assrt" ) var codedFiles map[string][]byte = map[string][]byte{ - "json.testfile": []byte{}, - "cbor.testfile": []byte{}, + "protobuf.testfile": []byte{}, } func init() { @@ -35,115 +35,108 @@ type TC struct { ctx interface{} } -var testCases []TC - -func init() { - testCases = append(testCases, TC{ - []byte{}, - memory.Node{ - "foo": "bar", - "bar": []int{1, 2, 3}, - "baz": memory.Node{ - "@type": "mlink", - "hash": "QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo", - }, - }, - map[string]memory.Link{ - "baz": {"@type": "mlink", "hash": ("QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo")}, - }, - "", - nil, - }) - - testCases = append(testCases, TC{ - []byte{}, - memory.Node{ - "foo": "bar", - "@type": "commit", - "@context": "/ipfs/QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo/mdag", - "baz": memory.Node{ - "@type": "mlink", - "hash": "QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo", - }, - "bazz": memory.Node{ - "@type": "mlink", - "hash": "QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo", - }, - "bar": memory.Node{ - "@type": "mlinkoo", - "hash": "QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo", - }, - "bar2": memory.Node{ - "foo": memory.Node{ - "@type": "mlink", - "hash": "QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo", - }, - }, - }, - map[string]memory.Link{ - "baz": {"@type": "mlink", "hash": ("QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo")}, - "bazz": {"@type": "mlink", "hash": ("QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo")}, - "bar2/foo": {"@type": "mlink", "hash": ("QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo")}, - }, - "", - "/ipfs/QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo/mdag", - }) - -} - -func TestHeaderMC(t *testing.T) { - codec := Multicodec() - for _, tc := range testCases { - mctest.HeaderTest(t, codec, &tc.src) - } -} - -func TestRoundtripBasicMC(t *testing.T) { - codec := Multicodec() - for _, tca := range testCases { - var tcb memory.Node - mctest.RoundTripTest(t, codec, &(tca.src), &tcb) - } -} - // Test decoding and encoding a json and cbor file -func TestCodecsDecodeEncode(t *testing.T) { +func TestCodecsEncodeDecode(t *testing.T) { for fname, testfile := range codedFiles { - var n memory.Node - codec := Multicodec() - if err := mc.Unmarshal(codec, testfile, &n); err != nil { - t.Log(testfile) + r, err := DecodeBytes(testfile) + if err != nil { t.Error(err) continue } - linksExpected := map[string]memory.Link{ - "abc": memory.Link{ - "mlink": "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V", - }, - } - linksActual := memory.Links(n) - if !reflect.DeepEqual(linksExpected, linksActual) { - t.Logf("Expected: %#v", linksExpected) - t.Logf("Actual: %#v", linksActual) - t.Logf("node: %#v\n", n) - t.Error("Links are not expected in " + fname) - continue + var codec Codec + switch fname { + case "protobuf.testfile": + codec = CodecProtobuf + default: + panic("should not arrive here") } - encoded, err := mc.Marshal(codec, &n) + t.Logf("Decoded %s: %#v", fname, r) + + outData, err := EncodeBytes(codec, r.(ipld.NodeIterator)) if err != nil { t.Error(err) - return + continue } - if !bytes.Equal(testfile, encoded) { - t.Error("marshalled values not equal in " + fname) - t.Log(string(testfile)) - t.Log(string(encoded)) + if !bytes.Equal(outData, testfile) { + t.Errorf("%s: encoded is not the same as original", fname) t.Log(testfile) - t.Log(encoded) + t.Log(string(testfile)) + t.Log(outData) + t.Log(string(outData)) + f, err := os.Create(fname + ".error") + if err != nil { + t.Error(err) + } else { + defer f.Close() + _, err := f.Write(outData) + if err != nil { + t.Error(err) + } + } } } } + +func TestPbStream(t *testing.T) { + a := assrt.NewAssert(t) + t.Logf("Reading protobuf.testfile") + t.Logf("Bytes: %v", codedFiles["protobuf.testfile"]) + pb, err := DecodeReader(bytes.NewReader(codedFiles["protobuf.testfile"])) + a.MustNil(err) + + rt.CheckReader(t, pb, []rt.Callback{ + rt.Cb(rt.Path(), reader.TokenNode, nil), + rt.Cb(rt.Path(), reader.TokenKey, "data"), + rt.Cb(rt.Path("data"), reader.TokenValue, []byte{0x08, 0x01}), + rt.Cb(rt.Path(), reader.TokenKey, "links"), + rt.Cb(rt.Path("links"), reader.TokenArray, nil), + rt.Cb(rt.Path("links"), reader.TokenIndex, 0), + rt.Cb(rt.Path("links", 0), reader.TokenNode, nil), + rt.Cb(rt.Path("links", 0), reader.TokenKey, ipld.LinkKey), + rt.Cb(rt.Path("links", 0, ipld.LinkKey), reader.TokenValue, "Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9"), + rt.Cb(rt.Path("links", 0), reader.TokenKey, "name"), + rt.Cb(rt.Path("links", 0, "name"), reader.TokenValue, "a"), + rt.Cb(rt.Path("links", 0), reader.TokenKey, "size"), + rt.Cb(rt.Path("links", 0, "size"), reader.TokenValue, uint64(10)), + rt.Cb(rt.Path("links", 0), reader.TokenEndNode, nil), + rt.Cb(rt.Path("links"), reader.TokenIndex, 1), + rt.Cb(rt.Path("links", 1), reader.TokenNode, nil), + rt.Cb(rt.Path("links", 1), reader.TokenKey, ipld.LinkKey), + rt.Cb(rt.Path("links", 1, ipld.LinkKey), reader.TokenValue, "QmR9pC5uCF3UExca8RSrCVL8eKv7nHMpATzbEQkAHpXmVM"), + rt.Cb(rt.Path("links", 1), reader.TokenKey, "name"), + rt.Cb(rt.Path("links", 1, "name"), reader.TokenValue, "b"), + rt.Cb(rt.Path("links", 1), reader.TokenKey, "size"), + rt.Cb(rt.Path("links", 1, "size"), reader.TokenValue, uint64(10)), + rt.Cb(rt.Path("links", 1), reader.TokenEndNode, nil), + rt.Cb(rt.Path("links"), reader.TokenEndArray, nil), + rt.Cb(rt.Path(), reader.TokenEndNode, nil), + }) +} + +func TestPbStreamSkip(t *testing.T) { + a := assrt.NewAssert(t) + t.Logf("Reading protobuf.testfile") + t.Logf("Bytes: %v", codedFiles["protobuf.testfile"]) + pb, err := DecodeReader(bytes.NewReader(codedFiles["protobuf.testfile"])) + a.MustNil(err) + + rt.CheckReader(t, pb, []rt.Callback{ + rt.Cb(rt.Path(), reader.TokenNode, nil), + rt.Cb(rt.Path(), reader.TokenKey, "data"), + rt.Cb(rt.Path("data"), reader.TokenValue, []byte{0x08, 0x01}), + rt.Cb(rt.Path(), reader.TokenKey, "links"), + rt.Cb(rt.Path("links"), reader.TokenArray, nil), + rt.Cb(rt.Path("links"), reader.TokenIndex, 0, reader.NodeReadSkip), + rt.Cb(rt.Path("links"), reader.TokenIndex, 1), + rt.Cb(rt.Path("links", 1), reader.TokenNode, nil), + rt.Cb(rt.Path("links", 1), reader.TokenKey, ipld.LinkKey), + rt.Cb(rt.Path("links", 1, ipld.LinkKey), reader.TokenValue, "QmR9pC5uCF3UExca8RSrCVL8eKv7nHMpATzbEQkAHpXmVM"), + rt.Cb(rt.Path("links", 1), reader.TokenKey, "name", reader.NodeReadSkip), + rt.Cb(rt.Path("links", 1), reader.TokenKey, "size"), + rt.Cb(rt.Path("links", 1, "size"), reader.TokenValue, uint64(10), reader.NodeReadAbort), + }) +} diff --git a/coding/pb/Makefile b/coding/pb/Makefile index 9c635b2..a855ca5 100644 --- a/coding/pb/Makefile +++ b/coding/pb/Makefile @@ -1,27 +1,26 @@ PB = $(wildcard *.proto) GO = $(PB:.proto=.pb.go) -all: $(GO) test.pb +all: $(GO) ipld.pb.go %.pb.go: %.proto - protoc --gogo_out=. --proto_path=../../../../../../:/usr/local/opt/protobuf/include:. $< + go get github.com/gogo/protobuf/protoc-gen-gogo + go get github.com/gogo/protobuf/protoc-gen-gofast + protoc --gogo_out=. --proto_path=../../../../../:/usr/include:/usr/local/opt/protobuf/include:. $< + patch $@ <$@.patch clean: rm -f *.pb.go rm -f *.go -testfile: bin/multicodec bin/msgio - bin/multicodec header /mdagv1 >testfile - bin/multicodec header /protobuf/msgio >>testfile +testfile: ../bin/multicodec ../bin/msgio + ../bin/multicodec header /mdagv1 >testfile + ../bin/multicodec header /protobuf/msgio >>testfile hash=`ipfs add -q -r . | tail -n1` && \ - ipfs object get "$$hash" --enc=protobuf | bin/msgio wrap >>testfile + ipfs object get "$$hash" --enc=protobuf | ../bin/msgio wrap >>testfile -bin/multicodec: - mkdir -p bin - go get -d github.com/jbenet/go-multicodec/multicodec - go build -o "$@" github.com/jbenet/go-multicodec/multicodec +../bin/msgio: + $(MAKE) -C .. bin/msgio -bin/msgio: - mkdir -p bin - go get -d github.com/jbenet/go-msgio/msgio - go build -o "$@" github.com/jbenet/go-msgio/msgio +../bin/multicodec: + $(MAKE) -C .. bin/multicodec diff --git a/coding/pb/ipld.pb.go b/coding/pb/ipld.pb.go index 39bbae6..edd15fd 100644 --- a/coding/pb/ipld.pb.go +++ b/coding/pb/ipld.pb.go @@ -1,12 +1,12 @@ // Code generated by protoc-gen-gogo. -// source: merkledag.proto +// source: ipld.proto // DO NOT EDIT! /* - Package merkledag_pb is a generated protocol buffer package. + Package ipldpb is a generated protocol buffer package. It is generated from these files: - merkledag.proto + ipld.proto It has these top-level messages: PBLink @@ -14,35 +14,34 @@ */ package ipldpb -import proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto" +import proto "github.com/gogo/protobuf/proto" +import fmt "fmt" import math "math" +import _ "github.com/gogo/protobuf/gogoproto" -// discarding unused import gogoproto "code.google.com/p/gogoprotobuf/gogoproto/gogo.pb" - -import io "io" -import fmt "fmt" -import github_com_gogo_protobuf_proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto" +import bytes "bytes" import strings "strings" -import reflect "reflect" - +import github_com_gogo_protobuf_proto "github.com/gogo/protobuf/proto" import sort "sort" import strconv "strconv" +import reflect "reflect" -import bytes "bytes" +import io "io" // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal +var _ = fmt.Errorf var _ = math.Inf // An IPFS MerkleDAG Link type PBLink struct { // multihash of the target object - Hash []byte `protobuf:"bytes,1,opt" json:"Hash,omitempty"` + Hash []byte `protobuf:"bytes,1,opt,name=Hash" json:"Hash,omitempty"` // utf string name. should be unique per object - Name *string `protobuf:"bytes,2,opt" json:"Name,omitempty"` + Name *string `protobuf:"bytes,2,opt,name=Name" json:"Name,omitempty"` // cumulative size of target object - Tsize *uint64 `protobuf:"varint,3,opt" json:"Tsize,omitempty"` + Tsize *uint64 `protobuf:"varint,3,opt,name=Tsize" json:"Tsize,omitempty"` XXX_unrecognized []byte `json:"-"` } @@ -73,9 +72,9 @@ func (m *PBLink) GetTsize() uint64 { // An IPFS MerkleDAG Node type PBNode struct { // refs to other objects - Links []*PBLink `protobuf:"bytes,2,rep" json:"Links,omitempty"` + Links []*PBLink `protobuf:"bytes,2,rep,name=Links" json:"Links,omitempty"` // opaque user data - Data []byte `protobuf:"bytes,1,opt" json:"Data,omitempty"` + Data []byte `protobuf:"bytes,1,opt,name=Data" json:"Data,omitempty"` XXX_unrecognized []byte `json:"-"` } @@ -97,393 +96,256 @@ func (m *PBNode) GetData() []byte { } func init() { + proto.RegisterType((*PBLink)(nil), "ipldpb.PBLink") + proto.RegisterType((*PBNode)(nil), "ipldpb.PBNode") } -func (m *PBLink) Unmarshal(data []byte) error { - l := len(data) - index := 0 - for index < l { - var wire uint64 - for shift := uint(0); ; shift += 7 { - if index >= l { - return io.ErrUnexpectedEOF - } - b := data[index] - index++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Hash", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if index >= l { - return io.ErrUnexpectedEOF - } - b := data[index] - index++ - byteLen |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - postIndex := index + byteLen - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Hash = append([]byte{}, data[index:postIndex]...) - index = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Name", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if index >= l { - return io.ErrUnexpectedEOF - } - b := data[index] - index++ - stringLen |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - postIndex := index + int(stringLen) - if postIndex > l { - return io.ErrUnexpectedEOF - } - s := string(data[index:postIndex]) - m.Name = &s - index = postIndex - case 3: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Tsize", wireType) - } - var v uint64 - for shift := uint(0); ; shift += 7 { - if index >= l { - return io.ErrUnexpectedEOF - } - b := data[index] - index++ - v |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - m.Tsize = &v - default: - var sizeOfWire int - for { - sizeOfWire++ - wire >>= 7 - if wire == 0 { - break - } - } - index -= sizeOfWire - skippy, err := github_com_gogo_protobuf_proto.Skip(data[index:]) - if err != nil { - return err - } - if (index + skippy) > l { - return io.ErrUnexpectedEOF - } - m.XXX_unrecognized = append(m.XXX_unrecognized, data[index:index+skippy]...) - index += skippy +func (this *PBLink) VerboseEqual(that interface{}) error { + if that == nil { + if this == nil { + return nil } + return fmt.Errorf("that == nil && this != nil") } - return nil -} -func (m *PBNode) Unmarshal(data []byte) error { - l := len(data) - index := 0 - for index < l { - var wire uint64 - for shift := uint(0); ; shift += 7 { - if index >= l { - return io.ErrUnexpectedEOF - } - b := data[index] - index++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - switch fieldNum { - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Links", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if index >= l { - return io.ErrUnexpectedEOF - } - b := data[index] - index++ - msglen |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - postIndex := index + msglen - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Links = append(m.Links, &PBLink{}) - m.Links[len(m.Links)-1].Unmarshal(data[index:postIndex]) - index = postIndex - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Data", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if index >= l { - return io.ErrUnexpectedEOF - } - b := data[index] - index++ - byteLen |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - postIndex := index + byteLen - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Data = append([]byte{}, data[index:postIndex]...) - index = postIndex - default: - var sizeOfWire int - for { - sizeOfWire++ - wire >>= 7 - if wire == 0 { - break - } - } - index -= sizeOfWire - skippy, err := github_com_gogo_protobuf_proto.Skip(data[index:]) - if err != nil { - return err - } - if (index + skippy) > l { - return io.ErrUnexpectedEOF - } - m.XXX_unrecognized = append(m.XXX_unrecognized, data[index:index+skippy]...) - index += skippy + + that1, ok := that.(*PBLink) + if !ok { + that2, ok := that.(PBLink) + if ok { + that1 = &that2 + } else { + return fmt.Errorf("that is not of type *PBLink") } } - return nil -} -func (this *PBLink) String() string { - if this == nil { - return "nil" - } - s := strings.Join([]string{`&PBLink{`, - `Hash:` + valueToStringMerkledag(this.Hash) + `,`, - `Name:` + valueToStringMerkledag(this.Name) + `,`, - `Tsize:` + valueToStringMerkledag(this.Tsize) + `,`, - `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, - `}`, - }, "") - return s -} -func (this *PBNode) String() string { - if this == nil { - return "nil" - } - s := strings.Join([]string{`&PBNode{`, - `Links:` + strings.Replace(fmt.Sprintf("%v", this.Links), "PBLink", "PBLink", 1) + `,`, - `Data:` + valueToStringMerkledag(this.Data) + `,`, - `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, - `}`, - }, "") - return s -} -func valueToStringMerkledag(v interface{}) string { - rv := reflect.ValueOf(v) - if rv.IsNil() { - return "nil" + if that1 == nil { + if this == nil { + return nil + } + return fmt.Errorf("that is type *PBLink but is nil && this != nil") + } else if this == nil { + return fmt.Errorf("that is type *PBLink but is not nil && this == nil") } - pv := reflect.Indirect(rv).Interface() - return fmt.Sprintf("*%v", pv) -} -func (m *PBLink) Size() (n int) { - var l int - _ = l - if m.Hash != nil { - l = len(m.Hash) - n += 1 + l + sovMerkledag(uint64(l)) + if !bytes.Equal(this.Hash, that1.Hash) { + return fmt.Errorf("Hash this(%v) Not Equal that(%v)", this.Hash, that1.Hash) } - if m.Name != nil { - l = len(*m.Name) - n += 1 + l + sovMerkledag(uint64(l)) + if this.Name != nil && that1.Name != nil { + if *this.Name != *that1.Name { + return fmt.Errorf("Name this(%v) Not Equal that(%v)", *this.Name, *that1.Name) + } + } else if this.Name != nil { + return fmt.Errorf("this.Name == nil && that.Name != nil") + } else if that1.Name != nil { + return fmt.Errorf("Name this(%v) Not Equal that(%v)", this.Name, that1.Name) } - if m.Tsize != nil { - n += 1 + sovMerkledag(uint64(*m.Tsize)) + if this.Tsize != nil && that1.Tsize != nil { + if *this.Tsize != *that1.Tsize { + return fmt.Errorf("Tsize this(%v) Not Equal that(%v)", *this.Tsize, *that1.Tsize) + } + } else if this.Tsize != nil { + return fmt.Errorf("this.Tsize == nil && that.Tsize != nil") + } else if that1.Tsize != nil { + return fmt.Errorf("Tsize this(%v) Not Equal that(%v)", this.Tsize, that1.Tsize) } - if m.XXX_unrecognized != nil { - n += len(m.XXX_unrecognized) + if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { + return fmt.Errorf("XXX_unrecognized this(%v) Not Equal that(%v)", this.XXX_unrecognized, that1.XXX_unrecognized) } - return n + return nil } - -func (m *PBNode) Size() (n int) { - var l int - _ = l - if len(m.Links) > 0 { - for _, e := range m.Links { - l = e.Size() - n += 1 + l + sovMerkledag(uint64(l)) +func (this *PBLink) Equal(that interface{}) bool { + if that == nil { + if this == nil { + return true } + return false } - if m.Data != nil { - l = len(m.Data) - n += 1 + l + sovMerkledag(uint64(l)) - } - if m.XXX_unrecognized != nil { - n += len(m.XXX_unrecognized) - } - return n -} -func sovMerkledag(x uint64) (n int) { - for { - n++ - x >>= 7 - if x == 0 { - break + that1, ok := that.(*PBLink) + if !ok { + that2, ok := that.(PBLink) + if ok { + that1 = &that2 + } else { + return false } } - return n -} -func sozMerkledag(x uint64) (n int) { - return sovMerkledag(uint64((x << 1) ^ uint64((int64(x) >> 63)))) -} -func NewPopulatedPBLink(r randyMerkledag, easy bool) *PBLink { - this := &PBLink{} - if r.Intn(10) != 0 { - v1 := r.Intn(100) - this.Hash = make([]byte, v1) - for i := 0; i < v1; i++ { - this.Hash[i] = byte(r.Intn(256)) + if that1 == nil { + if this == nil { + return true } + return false + } else if this == nil { + return false } - if r.Intn(10) != 0 { - v2 := randStringMerkledag(r) - this.Name = &v2 - } - if r.Intn(10) != 0 { - v3 := uint64(r.Uint32()) - this.Tsize = &v3 + if !bytes.Equal(this.Hash, that1.Hash) { + return false } - if !easy && r.Intn(10) != 0 { - this.XXX_unrecognized = randUnrecognizedMerkledag(r, 4) + if this.Name != nil && that1.Name != nil { + if *this.Name != *that1.Name { + return false + } + } else if this.Name != nil { + return false + } else if that1.Name != nil { + return false } - return this + if this.Tsize != nil && that1.Tsize != nil { + if *this.Tsize != *that1.Tsize { + return false + } + } else if this.Tsize != nil { + return false + } else if that1.Tsize != nil { + return false + } + if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { + return false + } + return true } +func (this *PBNode) VerboseEqual(that interface{}) error { + if that == nil { + if this == nil { + return nil + } + return fmt.Errorf("that == nil && this != nil") + } -func NewPopulatedPBNode(r randyMerkledag, easy bool) *PBNode { - this := &PBNode{} - if r.Intn(10) != 0 { - v4 := r.Intn(10) - this.Links = make([]*PBLink, v4) - for i := 0; i < v4; i++ { - this.Links[i] = NewPopulatedPBLink(r, easy) + that1, ok := that.(*PBNode) + if !ok { + that2, ok := that.(PBNode) + if ok { + that1 = &that2 + } else { + return fmt.Errorf("that is not of type *PBNode") } } - if r.Intn(10) != 0 { - v5 := r.Intn(100) - this.Data = make([]byte, v5) - for i := 0; i < v5; i++ { - this.Data[i] = byte(r.Intn(256)) + if that1 == nil { + if this == nil { + return nil } + return fmt.Errorf("that is type *PBNode but is nil && this != nil") + } else if this == nil { + return fmt.Errorf("that is type *PBNode but is not nil && this == nil") } - if !easy && r.Intn(10) != 0 { - this.XXX_unrecognized = randUnrecognizedMerkledag(r, 3) + if len(this.Links) != len(that1.Links) { + return fmt.Errorf("Links this(%v) Not Equal that(%v)", len(this.Links), len(that1.Links)) } - return this -} - -type randyMerkledag interface { - Float32() float32 - Float64() float64 - Int63() int64 - Int31() int32 - Uint32() uint32 - Intn(n int) int + for i := range this.Links { + if !this.Links[i].Equal(that1.Links[i]) { + return fmt.Errorf("Links this[%v](%v) Not Equal that[%v](%v)", i, this.Links[i], i, that1.Links[i]) + } + } + if !bytes.Equal(this.Data, that1.Data) { + return fmt.Errorf("Data this(%v) Not Equal that(%v)", this.Data, that1.Data) + } + if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { + return fmt.Errorf("XXX_unrecognized this(%v) Not Equal that(%v)", this.XXX_unrecognized, that1.XXX_unrecognized) + } + return nil } +func (this *PBNode) Equal(that interface{}) bool { + if that == nil { + if this == nil { + return true + } + return false + } -func randUTF8RuneMerkledag(r randyMerkledag) rune { - return rune(r.Intn(126-43) + 43) + that1, ok := that.(*PBNode) + if !ok { + that2, ok := that.(PBNode) + if ok { + that1 = &that2 + } else { + return false + } + } + if that1 == nil { + if this == nil { + return true + } + return false + } else if this == nil { + return false + } + if len(this.Links) != len(that1.Links) { + return false + } + for i := range this.Links { + if !this.Links[i].Equal(that1.Links[i]) { + return false + } + } + if !bytes.Equal(this.Data, that1.Data) { + return false + } + if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { + return false + } + return true } -func randStringMerkledag(r randyMerkledag) string { - v6 := r.Intn(100) - tmps := make([]rune, v6) - for i := 0; i < v6; i++ { - tmps[i] = randUTF8RuneMerkledag(r) +func (this *PBLink) GoString() string { + if this == nil { + return "nil" } - return string(tmps) + s := make([]string, 0, 7) + s = append(s, "&ipldpb.PBLink{") + if this.Hash != nil { + s = append(s, "Hash: "+valueToGoStringIpld(this.Hash, "byte")+",\n") + } + if this.Name != nil { + s = append(s, "Name: "+valueToGoStringIpld(this.Name, "string")+",\n") + } + if this.Tsize != nil { + s = append(s, "Tsize: "+valueToGoStringIpld(this.Tsize, "uint64")+",\n") + } + if this.XXX_unrecognized != nil { + s = append(s, "XXX_unrecognized:"+fmt.Sprintf("%#v", this.XXX_unrecognized)+",\n") + } + s = append(s, "}") + return strings.Join(s, "") } -func randUnrecognizedMerkledag(r randyMerkledag, maxFieldNumber int) (data []byte) { - l := r.Intn(5) - for i := 0; i < l; i++ { - wire := r.Intn(4) - if wire == 3 { - wire = 5 - } - fieldNumber := maxFieldNumber + r.Intn(100) - data = randFieldMerkledag(data, r, fieldNumber, wire) +func (this *PBNode) GoString() string { + if this == nil { + return "nil" } - return data + s := make([]string, 0, 6) + s = append(s, "&ipldpb.PBNode{") + if this.Links != nil { + s = append(s, "Links: "+fmt.Sprintf("%#v", this.Links)+",\n") + } + if this.Data != nil { + s = append(s, "Data: "+valueToGoStringIpld(this.Data, "byte")+",\n") + } + if this.XXX_unrecognized != nil { + s = append(s, "XXX_unrecognized:"+fmt.Sprintf("%#v", this.XXX_unrecognized)+",\n") + } + s = append(s, "}") + return strings.Join(s, "") } -func randFieldMerkledag(data []byte, r randyMerkledag, fieldNumber int, wire int) []byte { - key := uint32(fieldNumber)<<3 | uint32(wire) - switch wire { - case 0: - data = encodeVarintPopulateMerkledag(data, uint64(key)) - v7 := r.Int63() - if r.Intn(2) == 0 { - v7 *= -1 - } - data = encodeVarintPopulateMerkledag(data, uint64(v7)) - case 1: - data = encodeVarintPopulateMerkledag(data, uint64(key)) - data = append(data, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256))) - case 2: - data = encodeVarintPopulateMerkledag(data, uint64(key)) - ll := r.Intn(100) - data = encodeVarintPopulateMerkledag(data, uint64(ll)) - for j := 0; j < ll; j++ { - data = append(data, byte(r.Intn(256))) - } - default: - data = encodeVarintPopulateMerkledag(data, uint64(key)) - data = append(data, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256))) +func valueToGoStringIpld(v interface{}, typ string) string { + rv := reflect.ValueOf(v) + if rv.IsNil() { + return "nil" } - return data + pv := reflect.Indirect(rv).Interface() + return fmt.Sprintf("func(v %v) *%v { return &v } ( %#v )", typ, typ, pv) } -func encodeVarintPopulateMerkledag(data []byte, v uint64) []byte { - for v >= 1<<7 { - data = append(data, uint8(uint64(v)&0x7f|0x80)) - v >>= 7 +func extensionToGoStringIpld(e map[int32]github_com_gogo_protobuf_proto.Extension) string { + if e == nil { + return "nil" } - data = append(data, uint8(v)) - return data + s := "map[int32]proto.Extension{" + keys := make([]int, 0, len(e)) + for k := range e { + keys = append(keys, int(k)) + } + sort.Ints(keys) + ss := []string{} + for _, k := range keys { + ss = append(ss, strconv.Itoa(k)+": "+e[int32(k)].GoString()) + } + s += strings.Join(ss, ",") + "}" + return s } func (m *PBLink) Marshal() (data []byte, err error) { size := m.Size() @@ -495,7 +357,7 @@ func (m *PBLink) Marshal() (data []byte, err error) { return data[:n], nil } -func (m *PBLink) MarshalTo(data []byte) (n int, err error) { +func (m *PBLink) MarshalTo(data []byte) (int, error) { var i int _ = i var l int @@ -503,19 +365,19 @@ func (m *PBLink) MarshalTo(data []byte) (n int, err error) { if m.Hash != nil { data[i] = 0xa i++ - i = encodeVarintMerkledag(data, i, uint64(len(m.Hash))) + i = encodeVarintIpld(data, i, uint64(len(m.Hash))) i += copy(data[i:], m.Hash) } if m.Name != nil { data[i] = 0x12 i++ - i = encodeVarintMerkledag(data, i, uint64(len(*m.Name))) + i = encodeVarintIpld(data, i, uint64(len(*m.Name))) i += copy(data[i:], *m.Name) } if m.Tsize != nil { data[i] = 0x18 i++ - i = encodeVarintMerkledag(data, i, uint64(*m.Tsize)) + i = encodeVarintIpld(data, i, uint64(*m.Tsize)) } if m.XXX_unrecognized != nil { i += copy(data[i:], m.XXX_unrecognized) @@ -533,7 +395,7 @@ func (m *PBNode) Marshal() (data []byte, err error) { return data[:n], nil } -func (m *PBNode) MarshalTo(data []byte) (n int, err error) { +func (m *PBNode) MarshalTo(data []byte) (int, error) { var i int _ = i var l int @@ -542,7 +404,7 @@ func (m *PBNode) MarshalTo(data []byte) (n int, err error) { for _, msg := range m.Links { data[i] = 0x12 i++ - i = encodeVarintMerkledag(data, i, uint64(msg.Size())) + i = encodeVarintIpld(data, i, uint64(msg.Size())) n, err := msg.MarshalTo(data[i:]) if err != nil { return 0, err @@ -553,7 +415,7 @@ func (m *PBNode) MarshalTo(data []byte) (n int, err error) { if m.Data != nil { data[i] = 0xa i++ - i = encodeVarintMerkledag(data, i, uint64(len(m.Data))) + i = encodeVarintIpld(data, i, uint64(len(m.Data))) i += copy(data[i:], m.Data) } if m.XXX_unrecognized != nil { @@ -562,7 +424,7 @@ func (m *PBNode) MarshalTo(data []byte) (n int, err error) { return i, nil } -func encodeFixed64Merkledag(data []byte, offset int, v uint64) int { +func encodeFixed64Ipld(data []byte, offset int, v uint64) int { data[offset] = uint8(v) data[offset+1] = uint8(v >> 8) data[offset+2] = uint8(v >> 16) @@ -573,14 +435,14 @@ func encodeFixed64Merkledag(data []byte, offset int, v uint64) int { data[offset+7] = uint8(v >> 56) return offset + 8 } -func encodeFixed32Merkledag(data []byte, offset int, v uint32) int { +func encodeFixed32Ipld(data []byte, offset int, v uint32) int { data[offset] = uint8(v) data[offset+1] = uint8(v >> 8) data[offset+2] = uint8(v >> 16) data[offset+3] = uint8(v >> 24) return offset + 4 } -func encodeVarintMerkledag(data []byte, offset int, v uint64) int { +func encodeVarintIpld(data []byte, offset int, v uint64) int { for v >= 1<<7 { data[offset] = uint8(v&0x7f | 0x80) v >>= 7 @@ -589,213 +451,554 @@ func encodeVarintMerkledag(data []byte, offset int, v uint64) int { data[offset] = uint8(v) return offset + 1 } -func (this *PBLink) GoString() string { - if this == nil { - return "nil" - } - s := strings.Join([]string{`&merkledag_pb.PBLink{` + - `Hash:` + valueToGoStringMerkledag(this.Hash, "byte"), - `Name:` + valueToGoStringMerkledag(this.Name, "string"), - `Tsize:` + valueToGoStringMerkledag(this.Tsize, "uint64"), - `XXX_unrecognized:` + fmt.Sprintf("%#v", this.XXX_unrecognized) + `}`}, ", ") - return s -} -func (this *PBNode) GoString() string { - if this == nil { - return "nil" - } - s := strings.Join([]string{`&merkledag_pb.PBNode{` + - `Links:` + fmt.Sprintf("%#v", this.Links), - `Data:` + valueToGoStringMerkledag(this.Data, "byte"), - `XXX_unrecognized:` + fmt.Sprintf("%#v", this.XXX_unrecognized) + `}`}, ", ") - return s -} -func valueToGoStringMerkledag(v interface{}, typ string) string { - rv := reflect.ValueOf(v) - if rv.IsNil() { - return "nil" +func NewPopulatedPBLink(r randyIpld, easy bool) *PBLink { + this := &PBLink{} + if r.Intn(10) != 0 { + v1 := r.Intn(100) + this.Hash = make([]byte, v1) + for i := 0; i < v1; i++ { + this.Hash[i] = byte(r.Intn(256)) + } } - pv := reflect.Indirect(rv).Interface() - return fmt.Sprintf("func(v %v) *%v { return &v } ( %#v )", typ, typ, pv) -} -func extensionToGoStringMerkledag(e map[int32]github_com_gogo_protobuf_proto.Extension) string { - if e == nil { - return "nil" + if r.Intn(10) != 0 { + v2 := randStringIpld(r) + this.Name = &v2 } - s := "map[int32]proto.Extension{" - keys := make([]int, 0, len(e)) - for k := range e { - keys = append(keys, int(k)) + if r.Intn(10) != 0 { + v3 := uint64(uint64(r.Uint32())) + this.Tsize = &v3 } - sort.Ints(keys) - ss := []string{} - for _, k := range keys { - ss = append(ss, strconv.Itoa(k)+": "+e[int32(k)].GoString()) + if !easy && r.Intn(10) != 0 { + this.XXX_unrecognized = randUnrecognizedIpld(r, 4) } - s += strings.Join(ss, ",") + "}" - return s + return this } -func (this *PBLink) VerboseEqual(that interface{}) error { - if that == nil { - if this == nil { - return nil - } - return fmt.Errorf("that == nil && this != nil") - } - that1, ok := that.(*PBLink) - if !ok { - return fmt.Errorf("that is not of type *PBLink") - } - if that1 == nil { - if this == nil { - return nil +func NewPopulatedPBNode(r randyIpld, easy bool) *PBNode { + this := &PBNode{} + if r.Intn(10) != 0 { + v4 := r.Intn(100) + this.Data = make([]byte, v4) + for i := 0; i < v4; i++ { + this.Data[i] = byte(r.Intn(256)) } - return fmt.Errorf("that is type *PBLink but is nil && this != nil") - } else if this == nil { - return fmt.Errorf("that is type *PBLinkbut is not nil && this == nil") } - if !bytes.Equal(this.Hash, that1.Hash) { - return fmt.Errorf("Hash this(%v) Not Equal that(%v)", this.Hash, that1.Hash) - } - if this.Name != nil && that1.Name != nil { - if *this.Name != *that1.Name { - return fmt.Errorf("Name this(%v) Not Equal that(%v)", *this.Name, *that1.Name) + if r.Intn(10) != 0 { + v5 := r.Intn(5) + this.Links = make([]*PBLink, v5) + for i := 0; i < v5; i++ { + this.Links[i] = NewPopulatedPBLink(r, easy) } - } else if this.Name != nil { - return fmt.Errorf("this.Name == nil && that.Name != nil") - } else if that1.Name != nil { - return fmt.Errorf("Name this(%v) Not Equal that(%v)", this.Name, that1.Name) } - if this.Tsize != nil && that1.Tsize != nil { - if *this.Tsize != *that1.Tsize { - return fmt.Errorf("Tsize this(%v) Not Equal that(%v)", *this.Tsize, *that1.Tsize) - } - } else if this.Tsize != nil { - return fmt.Errorf("this.Tsize == nil && that.Tsize != nil") - } else if that1.Tsize != nil { - return fmt.Errorf("Tsize this(%v) Not Equal that(%v)", this.Tsize, that1.Tsize) - } - if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { - return fmt.Errorf("XXX_unrecognized this(%v) Not Equal that(%v)", this.XXX_unrecognized, that1.XXX_unrecognized) + if !easy && r.Intn(10) != 0 { + this.XXX_unrecognized = randUnrecognizedIpld(r, 3) } - return nil + return this } -func (this *PBLink) Equal(that interface{}) bool { - if that == nil { - if this == nil { - return true - } - return false - } - that1, ok := that.(*PBLink) - if !ok { - return false - } - if that1 == nil { - if this == nil { - return true - } - return false - } else if this == nil { - return false +type randyIpld interface { + Float32() float32 + Float64() float64 + Int63() int64 + Int31() int32 + Uint32() uint32 + Intn(n int) int +} + +func randUTF8RuneIpld(r randyIpld) rune { + ru := r.Intn(62) + if ru < 10 { + return rune(ru + 48) + } else if ru < 36 { + return rune(ru + 55) } - if !bytes.Equal(this.Hash, that1.Hash) { - return false + return rune(ru + 61) +} +func randStringIpld(r randyIpld) string { + v6 := r.Intn(100) + tmps := make([]rune, v6) + for i := 0; i < v6; i++ { + tmps[i] = randUTF8RuneIpld(r) } - if this.Name != nil && that1.Name != nil { - if *this.Name != *that1.Name { - return false + return string(tmps) +} +func randUnrecognizedIpld(r randyIpld, maxFieldNumber int) (data []byte) { + l := r.Intn(5) + for i := 0; i < l; i++ { + wire := r.Intn(4) + if wire == 3 { + wire = 5 } - } else if this.Name != nil { - return false - } else if that1.Name != nil { - return false + fieldNumber := maxFieldNumber + r.Intn(100) + data = randFieldIpld(data, r, fieldNumber, wire) } - if this.Tsize != nil && that1.Tsize != nil { - if *this.Tsize != *that1.Tsize { - return false + return data +} +func randFieldIpld(data []byte, r randyIpld, fieldNumber int, wire int) []byte { + key := uint32(fieldNumber)<<3 | uint32(wire) + switch wire { + case 0: + data = encodeVarintPopulateIpld(data, uint64(key)) + v7 := r.Int63() + if r.Intn(2) == 0 { + v7 *= -1 } - } else if this.Tsize != nil { - return false - } else if that1.Tsize != nil { - return false + data = encodeVarintPopulateIpld(data, uint64(v7)) + case 1: + data = encodeVarintPopulateIpld(data, uint64(key)) + data = append(data, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256))) + case 2: + data = encodeVarintPopulateIpld(data, uint64(key)) + ll := r.Intn(100) + data = encodeVarintPopulateIpld(data, uint64(ll)) + for j := 0; j < ll; j++ { + data = append(data, byte(r.Intn(256))) + } + default: + data = encodeVarintPopulateIpld(data, uint64(key)) + data = append(data, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256))) } - if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { - return false + return data +} +func encodeVarintPopulateIpld(data []byte, v uint64) []byte { + for v >= 1<<7 { + data = append(data, uint8(uint64(v)&0x7f|0x80)) + v >>= 7 } - return true + data = append(data, uint8(v)) + return data } -func (this *PBNode) VerboseEqual(that interface{}) error { - if that == nil { - if this == nil { - return nil - } - return fmt.Errorf("that == nil && this != nil") +func (m *PBLink) Size() (n int) { + var l int + _ = l + if m.Hash != nil { + l = len(m.Hash) + n += 1 + l + sovIpld(uint64(l)) + } + if m.Name != nil { + l = len(*m.Name) + n += 1 + l + sovIpld(uint64(l)) + } + if m.Tsize != nil { + n += 1 + sovIpld(uint64(*m.Tsize)) } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} - that1, ok := that.(*PBNode) - if !ok { - return fmt.Errorf("that is not of type *PBNode") +func (m *PBNode) Size() (n int) { + var l int + _ = l + if m.Data != nil { + l = len(m.Data) + n += 1 + l + sovIpld(uint64(l)) } - if that1 == nil { - if this == nil { - return nil + if len(m.Links) > 0 { + for _, e := range m.Links { + l = e.Size() + n += 1 + l + sovIpld(uint64(l)) } - return fmt.Errorf("that is type *PBNode but is nil && this != nil") - } else if this == nil { - return fmt.Errorf("that is type *PBNodebut is not nil && this == nil") } - if len(this.Links) != len(that1.Links) { - return fmt.Errorf("Links this(%v) Not Equal that(%v)", len(this.Links), len(that1.Links)) + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) } - for i := range this.Links { - if !this.Links[i].Equal(that1.Links[i]) { - return fmt.Errorf("Links this[%v](%v) Not Equal that[%v](%v)", i, this.Links[i], i, that1.Links[i]) + return n +} + +func sovIpld(x uint64) (n int) { + for { + n++ + x >>= 7 + if x == 0 { + break } } - if !bytes.Equal(this.Data, that1.Data) { - return fmt.Errorf("Data this(%v) Not Equal that(%v)", this.Data, that1.Data) + return n +} +func sozIpld(x uint64) (n int) { + return sovIpld(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (this *PBLink) String() string { + if this == nil { + return "nil" } - if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { - return fmt.Errorf("XXX_unrecognized this(%v) Not Equal that(%v)", this.XXX_unrecognized, that1.XXX_unrecognized) + s := strings.Join([]string{`&PBLink{`, + `Hash:` + valueToStringIpld(this.Hash) + `,`, + `Name:` + valueToStringIpld(this.Name) + `,`, + `Tsize:` + valueToStringIpld(this.Tsize) + `,`, + `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, + `}`, + }, "") + return s +} +func (this *PBNode) String() string { + if this == nil { + return "nil" } - return nil + s := strings.Join([]string{`&PBNode{`, + `Data:` + valueToStringIpld(this.Data) + `,`, + `Links:` + strings.Replace(fmt.Sprintf("%v", this.Links), "PBLink", "PBLink", 1) + `,`, + `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, + `}`, + }, "") + return s } -func (this *PBNode) Equal(that interface{}) bool { - if that == nil { - if this == nil { - return true +func valueToStringIpld(v interface{}) string { + rv := reflect.ValueOf(v) + if rv.IsNil() { + return "nil" + } + pv := reflect.Indirect(rv).Interface() + return fmt.Sprintf("*%v", pv) +} +func (m *PBLink) Unmarshal(data []byte) error { + l := len(data) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowIpld + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := data[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: PBLink: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: PBLink: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Hash", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowIpld + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := data[iNdEx] + iNdEx++ + byteLen |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthIpld + } + postIndex := iNdEx + byteLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Hash = append(m.Hash[:0], data[iNdEx:postIndex]...) + if m.Hash == nil { + m.Hash = []byte{} + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Name", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowIpld + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := data[iNdEx] + iNdEx++ + stringLen |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthIpld + } + postIndex := iNdEx + intStringLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + s := string(data[iNdEx:postIndex]) + m.Name = &s + iNdEx = postIndex + case 3: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Tsize", wireType) + } + var v uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowIpld + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := data[iNdEx] + iNdEx++ + v |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + m.Tsize = &v + default: + iNdEx = preIndex + skippy, err := skipIpld(data[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthIpld + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, data[iNdEx:iNdEx+skippy]...) + iNdEx += skippy } - return false } - that1, ok := that.(*PBNode) - if !ok { - return false + if iNdEx > l { + return io.ErrUnexpectedEOF } - if that1 == nil { - if this == nil { - return true + return nil +} +func (m *PBNode) Unmarshal(data []byte) error { + l := len(data) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowIpld + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := data[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } } - return false - } else if this == nil { - return false - } - if len(this.Links) != len(that1.Links) { - return false - } - for i := range this.Links { - if !this.Links[i].Equal(that1.Links[i]) { - return false + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: PBNode: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: PBNode: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Data", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowIpld + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := data[iNdEx] + iNdEx++ + byteLen |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthIpld + } + postIndex := iNdEx + byteLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Data = append(m.Data[:0], data[iNdEx:postIndex]...) + if m.Data == nil { + m.Data = []byte{} + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Links", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowIpld + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := data[iNdEx] + iNdEx++ + msglen |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthIpld + } + postIndex := iNdEx + msglen + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Links = append(m.Links, &PBLink{}) + if err := m.Links[len(m.Links)-1].Unmarshal(data[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipIpld(data[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthIpld + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, data[iNdEx:iNdEx+skippy]...) + iNdEx += skippy } } - if !bytes.Equal(this.Data, that1.Data) { - return false + + if iNdEx > l { + return io.ErrUnexpectedEOF } - if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { - return false + return nil +} +func skipIpld(data []byte) (n int, err error) { + l := len(data) + iNdEx := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowIpld + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := data[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowIpld + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if data[iNdEx-1] < 0x80 { + break + } + } + return iNdEx, nil + case 1: + iNdEx += 8 + return iNdEx, nil + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowIpld + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := data[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + iNdEx += length + if length < 0 { + return 0, ErrInvalidLengthIpld + } + return iNdEx, nil + case 3: + for { + var innerWire uint64 + var start int = iNdEx + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowIpld + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := data[iNdEx] + iNdEx++ + innerWire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + innerWireType := int(innerWire & 0x7) + if innerWireType == 4 { + break + } + next, err := skipIpld(data[start:]) + if err != nil { + return 0, err + } + iNdEx = start + next + } + return iNdEx, nil + case 4: + return iNdEx, nil + case 5: + iNdEx += 4 + return iNdEx, nil + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } } - return true + panic("unreachable") } + +var ( + ErrInvalidLengthIpld = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowIpld = fmt.Errorf("proto: integer overflow") +) diff --git a/coding/pb/ipld.pb.go.patch b/coding/pb/ipld.pb.go.patch new file mode 100644 index 0000000..9811088 --- /dev/null +++ b/coding/pb/ipld.pb.go.patch @@ -0,0 +1,28 @@ +--- ipld.pb.go.old 2016-02-24 10:29:39.045261549 +0100 ++++ ipld.pb.go.new 2016-02-24 10:29:45.038594961 +0100 +@@ -400,12 +400,6 @@ + _ = i + var l int + _ = l +- if m.Data != nil { +- data[i] = 0xa +- i++ +- i = encodeVarintIpld(data, i, uint64(len(m.Data))) +- i += copy(data[i:], m.Data) +- } + if len(m.Links) > 0 { + for _, msg := range m.Links { + data[i] = 0x12 +@@ -418,6 +412,12 @@ + i += n + } + } ++ if m.Data != nil { ++ data[i] = 0xa ++ i++ ++ i = encodeVarintIpld(data, i, uint64(len(m.Data))) ++ i += copy(data[i:], m.Data) ++ } + if m.XXX_unrecognized != nil { + i += copy(data[i:], m.XXX_unrecognized) + } diff --git a/coding/pb/ipld.proto b/coding/pb/ipld.proto index 5b619a8..59e65a3 100644 --- a/coding/pb/ipld.proto +++ b/coding/pb/ipld.proto @@ -1,6 +1,6 @@ package ipldpb; -import "code.google.com/p/gogoprotobuf/gogoproto/gogo.proto"; +import "github.com/gogo/protobuf/gogoproto/gogo.proto"; option (gogoproto.gostring_all) = true; option (gogoproto.equal_all) = true; diff --git a/coding/pb/pbcodec.go b/coding/pb/pbcodec.go index d3464be..9e93580 100644 --- a/coding/pb/pbcodec.go +++ b/coding/pb/pbcodec.go @@ -1,151 +1,156 @@ package ipldpb import ( - "errors" "fmt" "io" + ipld "github.com/ipfs/go-ipld" memory "github.com/ipfs/go-ipld/memory" - paths "github.com/ipfs/go-ipld/paths" + base58 "github.com/jbenet/go-base58" + msgio "github.com/jbenet/go-msgio" mc "github.com/jbenet/go-multicodec" - mcproto "github.com/jbenet/go-multicodec/protobuf" ) +const HeaderPath = "/mdagv1" +const MsgIOHeaderPath = "/protobuf/msgio" + var Header []byte +var MsgIOHeader []byte -var ( - errInvalidData = fmt.Errorf("invalid merkledag v1 protobuf, Data not bytes") - errInvalidLink = fmt.Errorf("invalid merkledag v1 protobuf, invalid Links") -) +var errInvalidLink = fmt.Errorf("invalid merkledag v1 protobuf, invalid Links") func init() { - Header = mc.Header([]byte("/mdagv1")) + Header = mc.Header([]byte(HeaderPath)) + MsgIOHeader = mc.Header([]byte(MsgIOHeaderPath)) } -type codec struct { - pbc mc.Multicodec -} +func Decode(r io.Reader) (memory.Node, error) { + err := mc.ConsumeHeader(r, MsgIOHeader) + if err != nil { + return nil, err + } -func Multicodec() mc.Multicodec { - var n *PBNode - return &codec{mcproto.Multicodec(n)} -} + length, err := msgio.ReadLen(r, nil) + if err != nil { + return nil, err + } -func (c *codec) Encoder(w io.Writer) mc.Encoder { - return &encoder{w: w, c: c, pbe: c.pbc.Encoder(w)} -} + data := make([]byte, length) + _, err = io.ReadFull(r, data) + if err != nil { + return nil, err + } -func (c *codec) Decoder(r io.Reader) mc.Decoder { - return &decoder{r: r, c: c, pbd: c.pbc.Decoder(r)} + return RawDecode(data) } -func (c *codec) Header() []byte { - return Header -} +func RawDecode(data []byte) (memory.Node, error) { + var pbn *PBNode = new(PBNode) -type encoder struct { - w io.Writer - c *codec - pbe mc.Encoder -} + err := pbn.Unmarshal(data) + if err != nil { + return nil, err + } -type decoder struct { - r io.Reader - c *codec - pbd mc.Decoder -} + n := make(memory.Node) + pb2ldNode(pbn, &n) -func (c *encoder) Encode(v interface{}) error { - nv, ok := v.(*memory.Node) - if !ok { - return errors.New("must encode *memory.Node") - } + return n, err +} - if _, err := c.w.Write(c.c.Header()); err != nil { +func Encode(w io.Writer, n ipld.NodeIterator, strict bool) error { + _, err := w.Write(MsgIOHeader) + if err != nil { return err } - n, err := ld2pbNode(nv) + data, err := RawEncode(n, strict) if err != nil { return err } - return c.pbe.Encode(n) + msgio.WriteLen(w, len(data)) + _, err = w.Write(data) + return err } -func (c *decoder) Decode(v interface{}) error { - nv, ok := v.(*memory.Node) - if !ok { - return errors.New("must decode to *memory.Node") - } - - if err := mc.ConsumeHeader(c.r, c.c.Header()); err != nil { - return err - } - - var pbn PBNode - if err := c.pbd.Decode(&pbn); err != nil { - return err +func RawEncode(n ipld.NodeIterator, strict bool) ([]byte, error) { + pbn, err := ld2pbNode(n, strict) + if err != nil { + return nil, err } - pb2ldNode(&pbn, nv) - return nil + return pbn.Marshal() } -func ld2pbNode(in *memory.Node) (*PBNode, error) { - n := *in +func ld2pbNode(n ipld.NodeIterator, strict bool) (*PBNode, error) { var pbn PBNode - var attrs memory.Node - - if attrsvalue, hasattrs := n["@attrs"]; hasattrs { - var ok bool - attrs, ok = attrsvalue.(memory.Node) - if !ok { - return nil, errInvalidData + has_data := false + has_links := false + + for n.Next() { + switch n.Key() { + case "data": + has_data = true + data, err := n.Value() + if err != nil { + return nil, err + } + pbn.Data, err = ipld.ToBytesErr(data) + if err != nil { + return nil, err + } + case "links": + has_links = true + linkit, err := n.Iterate() + if err != nil { + return nil, err + } + for linkit.Next() { + l, err := linkit.Iterate() + if err != nil { + return nil, err + } + pblink, err := ld2pbLink(l, strict) + if err != nil { + return nil, err + } + pbn.Links = append(pbn.Links, pblink) + } + if err := n.Error(); err != nil { + return nil, err + } + default: + if strict { + return nil, fmt.Errorf("Invalid merkledag v1 protobuf: node contains extra field (%s)", n.Key()) + } } - } else { - return &pbn, nil } - if data, hasdata := attrs["data"]; hasdata { - data, ok := data.([]byte) - if !ok { - return nil, errInvalidData - } - pbn.Data = data + if err := n.Error(); err != nil { + return nil, err } - if links, haslinks := attrs["links"]; haslinks { - links, ok := links.([]memory.Node) - if !ok { - return nil, errInvalidLink - } + if strict && !has_data { + return nil, fmt.Errorf("Invalid merkledag v1 protobuf: no data") + } - for _, link := range links { - pblink := ld2pbLink(link) - if pblink == nil { - return nil, fmt.Errorf("%s (%s)", errInvalidLink, link["name"]) - } - pbn.Links = append(pbn.Links, pblink) - } + if strict && !has_links { + return nil, fmt.Errorf("Invalid merkledag v1 protobuf: no links") } + return &pbn, nil } func pb2ldNode(pbn *PBNode, in *memory.Node) { - *in = make(memory.Node) - n := *in + var ordered_links []interface{} - links := make([]memory.Node, len(pbn.Links)) - for i, link := range pbn.Links { - links[i] = pb2ldLink(link) - n[paths.EscapePathComponent(link.GetName())] = links[i] + for _, link := range pbn.Links { + ordered_links = append(ordered_links, pb2ldLink(link)) } - n["@attrs"] = memory.Node{ - "links": links, - "data": pbn.Data, - } + (*in)["data"] = pbn.GetData() + (*in)["links"] = ordered_links } func pb2ldLink(pbl *PBLink) (link memory.Node) { @@ -156,68 +161,56 @@ func pb2ldLink(pbl *PBLink) (link memory.Node) { }() link = make(memory.Node) - link["hash"] = pbl.Hash + link[ipld.LinkKey] = base58.Encode(pbl.Hash) link["name"] = *pbl.Name link["size"] = uint64(*pbl.Tsize) return link } -func ld2pbLink(link memory.Node) (pbl *PBLink) { - defer func() { - if recover() != nil { - pbl = nil - } - }() - - hash := link["hash"].([]byte) - name := link["name"].(string) - size := link["size"].(uint64) - +func ld2pbLink(n ipld.NodeIterator, strict bool) (pbl *PBLink, err error) { pbl = &PBLink{} - pbl.Hash = hash - pbl.Name = &name - pbl.Tsize = &size - return pbl -} - -func IsOldProtobufNode(n memory.Node) bool { - if len(n) > 2 { // short circuit - return false - } - links, hasLinks := n["links"] - _, hasData := n["data"] - - switch len(n) { - case 2: // must be links and data - if !hasLinks || !hasData { - return false - } - case 1: // must be links or data - if !(hasLinks || hasData) { - return false - } - default: // nope. - return false - } - - if len(n) > 2 { - return false // only links and data. - } - - if hasLinks { - links, ok := links.([]memory.Node) - if !ok { - return false // invalid links. - } - - // every link must be a mlink - for _, link := range links { - if !memory.IsLink(link) { - return false + for n.Next() { + switch n.Key() { + case ipld.LinkKey: + data, err := n.Value() + if err != nil { + return nil, err + } + hash := ipld.ToString(data) + if hash == nil { + return nil, fmt.Errorf("Invalid merkledag v1 protobuf: link is of incorect type") + } + pbl.Hash = base58.Decode(*hash) + if strict && base58.Encode(pbl.Hash) != *hash { + return nil, errInvalidLink + } + case "name": + data, err := n.Value() + if err != nil { + return nil, err + } + name := ipld.ToString(data) + if name == nil { + return nil, fmt.Errorf("Invalid merkledag v1 protobuf: name is of incorect type") + } + pbl.Name = name + case "size": + data, err := n.Value() + if err != nil { + return nil, err + } + size := ipld.ToUint(data) + if size == nil { + return nil, fmt.Errorf("Invalid merkledag v1 protobuf: size is of incorect type") + } + pbl.Tsize = size + default: + if strict { + return nil, fmt.Errorf("Invalid merkledag v1 protobuf: node contains extra field (%s)", n.Key()) } } } - return true // ok looks like an old protobuf node + return pbl, err } diff --git a/coding/pb/pbcodec_test.go b/coding/pb/pbcodec_test.go deleted file mode 100644 index 3a9b95f..0000000 --- a/coding/pb/pbcodec_test.go +++ /dev/null @@ -1,158 +0,0 @@ -package ipldpb - -import ( - "bytes" - "encoding/hex" - "io/ioutil" - "reflect" - "testing" - - mc "github.com/jbenet/go-multicodec" - mcproto "github.com/jbenet/go-multicodec/protobuf" - - ipld "github.com/ipfs/go-ipld/memory" -) - -var testfile []byte - -func init() { - var err error - testfile, err = ioutil.ReadFile("testfile") - if err != nil { - panic("could not read testfile. please run: make testfile") - } -} - -func TestPBDecode(t *testing.T) { - c := mcproto.Multicodec(&PBNode{}) - buf := bytes.NewBuffer(testfile) - dec := c.Decoder(buf) - - // pass the /mdagv1 - if err := mc.ConsumeHeader(buf, Header); err != nil { - t.Fatal("failed to consume header", err) - } - - var pbn PBNode - if err := dec.Decode(&pbn); err != nil { - t.Fatal("failed to decode", err) - } - - if len(pbn.Links) < 7 { - t.Fatal("incorrect number of links") - } - if len(pbn.Data) == 0 { - t.Error("should have some data") - } - - findLink := func(s string) *PBLink { - for _, l := range pbn.Links { - if *l.Name == s { - return l - } - } - return nil - } - - makefile := findLink("Makefile") - if makefile == nil { - t.Error("did not find Makefile") - } else { - if *makefile.Tsize < 700 || *makefile.Tsize > 4096 { - t.Error("makefile incorrect size") - } - } -} - -func TestPB2LD(t *testing.T) { - buf := bytes.NewBuffer(testfile) - dec := Multicodec().Decoder(buf) - - var n ipld.Node - if err := dec.Decode(&n); err != nil { - t.Fatal("failed to decode", err) - } - - attrs, ok := n["@attrs"].(ipld.Node) - if !ok { - t.Log(n) - t.Fatal("invalid ipld.@attrs") - } - - data, ok := attrs["data"].([]byte) - if !ok { - t.Log(n) - t.Fatal("invalid ipld.@attrs.data") - } - if len(data) == 0 { - t.Error("should have some data") - } - - links, ok := attrs["links"].([]ipld.Node) - if !ok { - t.Fatal("invalid ipld.@attrs.links") - } - if len(links) < 7 { - t.Fatal("incorrect number of links") - } - - findLink := func(s string) ipld.Node { - for i, l := range links { - s2, ok := l["name"].(string) - if !ok { - t.Log(l) - t.Fatalf("invalid ipld.links[%d].name", i) - } - if s2 == s { - return l - } - } - return nil - } - - makefileLink := findLink("Makefile") - if makefileLink == nil { - t.Error("did not find Makefile") - } - - makefile := n["Makefile"].(ipld.Node) - if makefile == nil { - t.Error("did not find Makefile") - } else { - size, ok := makefile["size"].(uint64) - if !ok { - t.Log(makefile) - t.Fatal("invalid ipld.links[makefile].size") - } - if size < 700 || size > 4096 { - t.Log(makefile) - t.Error("makefile incorrect size") - } - if !reflect.DeepEqual(makefile, makefileLink) { - t.Error("makefile and @attrs.links[name=makefile] are not the same") - } - } -} - -func TestLD2PB(t *testing.T) { - decbuf := bytes.NewBuffer(testfile) - encbuf := bytes.NewBuffer(nil) - dec := Multicodec().Decoder(decbuf) - enc := Multicodec().Encoder(encbuf) - - var n ipld.Node - if err := dec.Decode(&n); err != nil { - t.Fatal("failed to decode", err) - } - - if err := enc.Encode(&n); err != nil { - t.Log(n) - t.Fatal("failed to encode", err) - } - - if !bytes.Equal(testfile, encbuf.Bytes()) { - t.Log(hex.Dump(testfile)) - t.Log(hex.Dump(encbuf.Bytes())) - t.Fatal("decoded bytes != encoded bytes") - } -} diff --git a/coding/protobuf.testfile b/coding/protobuf.testfile new file mode 100644 index 0000000000000000000000000000000000000000..6d0af733269cfd0218fb4d471b1bc6bfe4f3243f GIT binary patch literal 120 zcmdhz%q$BP?ne0Uce@*F5 zla9p)6JsvCneu4fZV#@e?q8c;2{9&0a6y!6o=G{DslG=;y0hNq`+kR)dFCtbFFF3b WLf~!5q~^Q4e}ov5B)GVkI2Zv}r7i0K literal 0 HcmV?d00001