Compare commits

...

42 Commits

Author SHA1 Message Date
Whyrusleeping
8b9ff3906e Merge pull request #109 from ipfs/feat/optimize-prefix
feat: optimize cid.Prefix
2020-07-23 09:15:54 -07:00
Steven Allen
f7cb4c91eb feat: optimize cid.Prefix
We call this frequently and having to allocate here is really unfortunate.
2020-07-22 19:45:20 -07:00
Peter Rabbitson
85cd30874e Merge pull request #107 from ipfs/chore/base36_support
Chore/base36 support
2020-05-25 18:15:04 +02:00
Peter Rabbitson
12e2623f35 gx-era file no longer relevant 2020-05-25 18:08:48 +02:00
Marcin Rataj
d683e9243c test: lowercase base36
Base36 was introduced mainly for use in DNS, and various user agents
force lowercase, so tests should use that instead.

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
2020-05-25 17:46:16 +02:00
Peter Rabbitson
18af217de6 Add explicit base36 test 2020-05-25 14:06:26 +02:00
Peter Rabbitson
9830f823b5 Base36 support 2020-05-25 13:51:07 +02:00
Peter Rabbitson
d110f73c43 Remove gx remnants 2020-05-25 13:50:43 +02:00
Steven Allen
95a7ed4233 Merge pull request #104 from rvagg/rvagg/filecoin-codecs
feat: add Filecoin multicodecs
2020-05-25 00:46:19 -07:00
Rod Vagg
8dd6fe2778 feat: add Filecoin multicodecs
* fil-commitment-unsealed
* fil-commitment-sealed

Ref: https://github.com/multiformats/multicodec/pull/161
Ref: https://github.com/multiformats/multicodec/pull/172
2020-05-13 11:22:42 +10:00
Hector Sanjuan
a25d68f3e4 Add autocomment configuration 2020-05-04 12:05:06 +02:00
Whyrusleeping
7c82f3b81c Merge pull request #103 from ipfs/fix/write-to-interface
avoid calling the method WriteTo if we don't satisfy its contract
2020-05-01 16:06:55 -07:00
Jeromy
75caa6bdbd avoid calling the method WriteTo if we don't satisfy its contract 2020-05-01 16:04:36 -07:00
Whyrusleeping
c1c89c20c1 Merge pull request #102 from ipfs/feat/add-methods-for-using-less-memory
add a couple useful methods
2020-05-01 15:54:35 -07:00
Eric Myhre
628a0123ed Slightly more documentation comment. 2020-05-02 00:52:07 +02:00
Jeromy
266e76d591 properly satisfy writerTo interface 2020-05-01 15:44:06 -07:00
Jeromy
7d7cb88d78 add a couple useful methods 2020-05-01 15:01:28 -07:00
Steven Allen
723b4ab105 Merge pull request #101 from vmx/patch-1
chore: make comment reflect the code
2020-04-29 09:27:52 -07:00
Volker Mische
9cb3848e51 chore: make comment reflect the code
During the change to default CIDV1 to Base32, the corresponding code
comment wasn't updated.
2020-04-28 16:02:21 +02:00
Hector Sanjuan
f458e04107 Add standard issue template 2020-04-27 11:27:08 +02:00
Steven Allen
72cd3d39d7 Merge pull request #99 from ipfs/fix/minimal-encoding
fix: enforce minimal encoding
2020-02-04 12:12:58 -08:00
Steven Allen
c1b740035a fix: enforce minimal encoding
Note: this removes two errors that don't appear to be used by anyone.
2020-02-03 20:55:45 -08:00
Steven Allen
3da5bbbe45 Merge pull request #92 from b5/chore_remove_gx
docs(readme): remove reference to gx
2019-12-09 06:47:25 -06:00
Steven Allen
9d598bbdbd Merge pull request #98 from ipfs/dependabot/go_modules/github.com/multiformats/go-multihash-0.0.10
Bump github.com/multiformats/go-multihash from 0.0.9 to 0.0.10
2019-12-09 06:24:09 -06:00
dependabot-preview[bot]
b420452400 Bump github.com/multiformats/go-multihash from 0.0.9 to 0.0.10
Bumps [github.com/multiformats/go-multihash](https://github.com/multiformats/go-multihash) from 0.0.9 to 0.0.10.
- [Release notes](https://github.com/multiformats/go-multihash/releases)
- [Commits](https://github.com/multiformats/go-multihash/compare/v0.0.9...v0.0.10)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2019-12-09 11:21:09 +00:00
Steven Allen
ed7dab1755 Merge pull request #97 from ipfs/fix/parse-panic
fix parsing issues and nits
2019-12-03 09:50:46 -05:00
Steven Allen
5df89959a0 test: test parsing non-sha256 hashes 2019-12-02 20:41:59 -05:00
Steven Allen
60ab0f84f0 chore: avoid magic numbers 2019-12-02 20:35:36 -05:00
Steven Allen
08e15f8a6c chore: avoid re-validated already validated CIDs 2019-12-02 20:35:27 -05:00
Steven Allen
58b483a841 fix: avoid panicing if we try to parse a non-sha multihash as a CIDv0
I'm pretty sure we never call `Parse` multihashes anyways, but we should be careful.
2019-12-02 20:34:30 -05:00
Whyrusleeping
79e75dffeb Merge pull request #95 from ipfs/feat/cid-from-bytes
Add a function to read a cid from an array of bytes
2019-11-12 10:17:18 +09:00
whyrusleeping
d93d4baeab add a simple test for reading cids from a buffer 2019-11-11 16:48:19 -08:00
whyrusleeping
cf8cf8856e Use optimized multihash method for parsing 2019-11-11 16:33:49 -08:00
Steven Allen
aa8072eeb5 Merge pull request #96 from ipfs/fix/verif-v0
Verify that prefix is correct v0 prefix
2019-11-11 11:29:02 -08:00
Jakub Sztandera
51871ccaa9 Disable gx builds
License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
2019-11-11 20:04:40 +01:00
Jakub Sztandera
9238d1a533 Verify that prefix is correct v0 prefix
License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
2019-11-11 20:00:31 +01:00
whyrusleeping
16bbe1cf14 Add a command to read a cid from an array of bytes 2019-11-10 22:17:41 -08:00
b5
c91a795815 docs(readme): remove reference to gx 2019-10-24 10:06:43 -04:00
Steven Allen
64acce09e1 Merge pull request #91 from ipfs/dependabot/go_modules/github.com/multiformats/go-multihash-0.0.8
Bump github.com/multiformats/go-multihash from 0.0.1 to 0.0.8
2019-09-24 16:11:26 -07:00
dependabot-preview[bot]
2a2f67d2c4 Bump github.com/multiformats/go-multihash from 0.0.1 to 0.0.8
Bumps [github.com/multiformats/go-multihash](https://github.com/multiformats/go-multihash) from 0.0.1 to 0.0.8.
- [Release notes](https://github.com/multiformats/go-multihash/releases)
- [Commits](https://github.com/multiformats/go-multihash/compare/v0.0.1...v0.0.8)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2019-09-24 23:01:51 +00:00
Steven Allen
86ca2a8300 Merge pull request #90 from ipfs/feat/lead-maintainer
doc: add a lead maintainer
2019-09-18 22:09:20 -07:00
Steven Allen
e0a778be45 doc: add a lead maintainer 2019-09-18 22:08:21 -07:00
15 changed files with 471 additions and 255 deletions

8
.github/ISSUE_TEMPLATE/config.yml vendored Normal file
View File

@@ -0,0 +1,8 @@
blank_issues_enabled: false
contact_links:
- name: Getting Help on IPFS
url: https://ipfs.io/help
about: All information about how and where to get help on IPFS.
- name: IPFS Official Forum
url: https://discuss.ipfs.io
about: Please post general questions, support requests, and discussions here.

19
.github/ISSUE_TEMPLATE/open_an_issue.md vendored Normal file
View File

@@ -0,0 +1,19 @@
---
name: Open an issue
about: Only for actionable issues relevant to this repository.
title: ''
labels: need/triage
assignees: ''
---
<!--
Hello! To ensure this issue is correctly addressed as soon as possible by the IPFS team, please try to make sure:
- This issue is relevant to this repository's topic or codebase.
- A clear description is provided. It should includes as much relevant information as possible and clear scope for the issue to be actionable.
FOR GENERAL DISCUSSION, HELP OR QUESTIONS, please see the options at https://ipfs.io/help or head directly to https://discuss.ipfs.io.
(you can delete this section after reading)
-->

68
.github/config.yml vendored Normal file
View File

@@ -0,0 +1,68 @@
# Configuration for welcome - https://github.com/behaviorbot/welcome
# Configuration for new-issue-welcome - https://github.com/behaviorbot/new-issue-welcome
# Comment to be posted to on first time issues
newIssueWelcomeComment: >
Thank you for submitting your first issue to this repository! A maintainer
will be here shortly to triage and review.
In the meantime, please double-check that you have provided all the
necessary information to make this process easy! Any information that can
help save additional round trips is useful! We currently aim to give
initial feedback within **two business days**. If this does not happen, feel
free to leave a comment.
Please keep an eye on how this issue will be labeled, as labels give an
overview of priorities, assignments and additional actions requested by the
maintainers:
- "Priority" labels will show how urgent this is for the team.
- "Status" labels will show if this is ready to be worked on, blocked, or in progress.
- "Need" labels will indicate if additional input or analysis is required.
Finally, remember to use https://discuss.ipfs.io if you just need general
support.
# Configuration for new-pr-welcome - https://github.com/behaviorbot/new-pr-welcome
# Comment to be posted to on PRs from first time contributors in your repository
newPRWelcomeComment: >
Thank you for submitting this PR!
A maintainer will be here shortly to review it.
We are super grateful, but we are also overloaded! Help us by making sure
that:
* The context for this PR is clear, with relevant discussion, decisions
and stakeholders linked/mentioned.
* Your contribution itself is clear (code comments, self-review for the
rest) and in its best form. Follow the [code contribution
guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md#code-contribution-guidelines)
if they apply.
Getting other community members to do a review would be great help too on
complex PRs (you can ask in the chats/forums). If you are unsure about
something, just leave us a comment.
Next steps:
* A maintainer will triage and assign priority to this PR, commenting on
any missing things and potentially assigning a reviewer for high
priority items.
* The PR gets reviews, discussed and approvals as needed.
* The PR is merged by maintainers when it has been approved and comments addressed.
We currently aim to provide initial feedback/triaging within **two business
days**. Please keep an eye on any labelling actions, as these will indicate
priorities and status of your contribution.
We are very grateful for your contribution!
# Configuration for first-pr-merge - https://github.com/behaviorbot/first-pr-merge
# Comment to be posted to on pull requests merged by a first time user
# Currently disabled
#firstPRMergeComment: ""

View File

@@ -1 +0,0 @@
0.9.3: QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN

View File

@@ -10,7 +10,6 @@ env:
global: global:
- GOTFLAGS="-race" - GOTFLAGS="-race"
matrix: matrix:
- BUILD_DEPTYPE=gx
- BUILD_DEPTYPE=gomod - BUILD_DEPTYPE=gomod
@@ -24,7 +23,6 @@ script:
cache: cache:
directories: directories:
- $GOPATH/src/gx
- $GOPATH/pkg/mod - $GOPATH/pkg/mod
- $HOME/.cache/go-build - $HOME/.cache/go-build

View File

@@ -1,16 +1,5 @@
all: deps all: deps
gx: deps:
go get github.com/whyrusleeping/gx
go get github.com/whyrusleeping/gx-go
covertools:
go get github.com/mattn/goveralls go get github.com/mattn/goveralls
go get golang.org/x/tools/cmd/cover go get golang.org/x/tools/cmd/cover
deps: gx covertools
gx --verbose install --global
gx-go rewrite
publish:
gx-go rewrite --undo

View File

@@ -14,6 +14,9 @@ go-cid
This is an implementation in Go of the [CID spec](https://github.com/ipld/cid). This is an implementation in Go of the [CID spec](https://github.com/ipld/cid).
It is used in `go-ipfs` and related packages to refer to a typed hunk of data. It is used in `go-ipfs` and related packages to refer to a typed hunk of data.
## Lead Maintainer
[Eric Myhre](https://github.com/warpfork)
## Table of Contents ## Table of Contents
@@ -31,36 +34,16 @@ It is used in `go-ipfs` and related packages to refer to a typed hunk of data.
go get github.com/ipfs/go-cid go get github.com/ipfs/go-cid
``` ```
Note that `go-cid` is packaged with Gx, so it is recommended to use Gx to install and use it (see Usage section).
## Usage ## Usage
### Using Gx and Gx-go
This module is packaged with [Gx](https://github.com/whyrusleeping/gx). In order to use it in your own project it is recommended that you:
```sh
go get -u github.com/whyrusleeping/gx
go get -u github.com/whyrusleeping/gx-go
cd <your-project-repository>
gx init
gx import github.com/ipfs/go-cid
gx install --global
gx-go --rewrite
```
Please check [Gx](https://github.com/whyrusleeping/gx) and [Gx-go](https://github.com/whyrusleeping/gx-go) documentation for more information.
### Running tests ### Running tests
Before running tests, please run: Run tests with `go test` from the directory root
```sh ```sh
make deps go test
``` ```
This will make sure that dependencies are rewritten to known working versions.
### Examples ### Examples
#### Parsing string input from users #### Parsing string input from users

View File

@@ -38,7 +38,7 @@ func (p V0Builder) Sum(data []byte) (Cid, error) {
if err != nil { if err != nil {
return Undef, err return Undef, err
} }
return NewCidV0(hash), nil return Cid{string(hash)}, nil
} }
func (p V0Builder) GetCodec() uint64 { func (p V0Builder) GetCodec() uint64 {

356
cid.go
View File

@@ -22,29 +22,21 @@ package cid
import ( import (
"bytes" "bytes"
"encoding" "encoding"
"encoding/binary"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"strings" "strings"
mbase "github.com/multiformats/go-multibase" mbase "github.com/multiformats/go-multibase"
mh "github.com/multiformats/go-multihash" mh "github.com/multiformats/go-multihash"
varint "github.com/multiformats/go-varint"
) )
// UnsupportedVersionString just holds an error message // UnsupportedVersionString just holds an error message
const UnsupportedVersionString = "<unsupported cid version>" const UnsupportedVersionString = "<unsupported cid version>"
var ( var (
// ErrVarintBuffSmall means that a buffer passed to the cid parser was not
// long enough, or did not contain an invalid cid
ErrVarintBuffSmall = errors.New("reading varint: buffer too small")
// ErrVarintTooBig means that the varint in the given cid was above the
// limit of 2^64
ErrVarintTooBig = errors.New("reading varint: varint bigger than 64bits" +
" and not supported")
// ErrCidTooShort means that the cid passed to decode was not long // ErrCidTooShort means that the cid passed to decode was not long
// enough to be a valid Cid // enough to be a valid Cid
ErrCidTooShort = errors.New("cid too short") ErrCidTooShort = errors.New("cid too short")
@@ -66,102 +58,122 @@ const (
GitRaw = 0x78 GitRaw = 0x78
EthBlock = 0x90 EthBlock = 0x90
EthBlockList = 0x91 EthBlockList = 0x91
EthTxTrie = 0x92 EthTxTrie = 0x92
EthTx = 0x93 EthTx = 0x93
EthTxReceiptTrie = 0x94 EthTxReceiptTrie = 0x94
EthTxReceipt = 0x95 EthTxReceipt = 0x95
EthStateTrie = 0x96 EthStateTrie = 0x96
EthAccountSnapshot = 0x97 EthAccountSnapshot = 0x97
EthStorageTrie = 0x98 EthStorageTrie = 0x98
BitcoinBlock = 0xb0 BitcoinBlock = 0xb0
BitcoinTx = 0xb1 BitcoinTx = 0xb1
ZcashBlock = 0xc0 ZcashBlock = 0xc0
ZcashTx = 0xc1 ZcashTx = 0xc1
DecredBlock = 0xe0 DecredBlock = 0xe0
DecredTx = 0xe1 DecredTx = 0xe1
DashBlock = 0xf0 DashBlock = 0xf0
DashTx = 0xf1 DashTx = 0xf1
FilCommitmentUnsealed = 0xf101
FilCommitmentSealed = 0xf102
) )
// Codecs maps the name of a codec to its type // Codecs maps the name of a codec to its type
var Codecs = map[string]uint64{ var Codecs = map[string]uint64{
"v0": DagProtobuf, "v0": DagProtobuf,
"raw": Raw, "raw": Raw,
"protobuf": DagProtobuf, "protobuf": DagProtobuf,
"cbor": DagCBOR, "cbor": DagCBOR,
"libp2p-key": Libp2pKey, "libp2p-key": Libp2pKey,
"git-raw": GitRaw, "git-raw": GitRaw,
"eth-block": EthBlock, "eth-block": EthBlock,
"eth-block-list": EthBlockList, "eth-block-list": EthBlockList,
"eth-tx-trie": EthTxTrie, "eth-tx-trie": EthTxTrie,
"eth-tx": EthTx, "eth-tx": EthTx,
"eth-tx-receipt-trie": EthTxReceiptTrie, "eth-tx-receipt-trie": EthTxReceiptTrie,
"eth-tx-receipt": EthTxReceipt, "eth-tx-receipt": EthTxReceipt,
"eth-state-trie": EthStateTrie, "eth-state-trie": EthStateTrie,
"eth-account-snapshot": EthAccountSnapshot, "eth-account-snapshot": EthAccountSnapshot,
"eth-storage-trie": EthStorageTrie, "eth-storage-trie": EthStorageTrie,
"bitcoin-block": BitcoinBlock, "bitcoin-block": BitcoinBlock,
"bitcoin-tx": BitcoinTx, "bitcoin-tx": BitcoinTx,
"zcash-block": ZcashBlock, "zcash-block": ZcashBlock,
"zcash-tx": ZcashTx, "zcash-tx": ZcashTx,
"decred-block": DecredBlock, "decred-block": DecredBlock,
"decred-tx": DecredTx, "decred-tx": DecredTx,
"dash-block": DashBlock, "dash-block": DashBlock,
"dash-tx": DashTx, "dash-tx": DashTx,
"fil-commitment-unsealed": FilCommitmentUnsealed,
"fil-commitment-sealed": FilCommitmentSealed,
} }
// CodecToStr maps the numeric codec to its name // CodecToStr maps the numeric codec to its name
var CodecToStr = map[uint64]string{ var CodecToStr = map[uint64]string{
Raw: "raw", Raw: "raw",
DagProtobuf: "protobuf", DagProtobuf: "protobuf",
DagCBOR: "cbor", DagCBOR: "cbor",
GitRaw: "git-raw", GitRaw: "git-raw",
EthBlock: "eth-block", EthBlock: "eth-block",
EthBlockList: "eth-block-list", EthBlockList: "eth-block-list",
EthTxTrie: "eth-tx-trie", EthTxTrie: "eth-tx-trie",
EthTx: "eth-tx", EthTx: "eth-tx",
EthTxReceiptTrie: "eth-tx-receipt-trie", EthTxReceiptTrie: "eth-tx-receipt-trie",
EthTxReceipt: "eth-tx-receipt", EthTxReceipt: "eth-tx-receipt",
EthStateTrie: "eth-state-trie", EthStateTrie: "eth-state-trie",
EthAccountSnapshot: "eth-account-snapshot", EthAccountSnapshot: "eth-account-snapshot",
EthStorageTrie: "eth-storage-trie", EthStorageTrie: "eth-storage-trie",
BitcoinBlock: "bitcoin-block", BitcoinBlock: "bitcoin-block",
BitcoinTx: "bitcoin-tx", BitcoinTx: "bitcoin-tx",
ZcashBlock: "zcash-block", ZcashBlock: "zcash-block",
ZcashTx: "zcash-tx", ZcashTx: "zcash-tx",
DecredBlock: "decred-block", DecredBlock: "decred-block",
DecredTx: "decred-tx", DecredTx: "decred-tx",
DashBlock: "dash-block", DashBlock: "dash-block",
DashTx: "dash-tx", DashTx: "dash-tx",
FilCommitmentUnsealed: "fil-commitment-unsealed",
FilCommitmentSealed: "fil-commitment-sealed",
}
// tryNewCidV0 tries to convert a multihash into a CIDv0 CID and returns an
// error on failure.
func tryNewCidV0(mhash mh.Multihash) (Cid, error) {
// Need to make sure hash is valid for CidV0 otherwise we will
// incorrectly detect it as CidV1 in the Version() method
dec, err := mh.Decode(mhash)
if err != nil {
return Undef, err
}
if dec.Code != mh.SHA2_256 || dec.Length != 32 {
return Undef, fmt.Errorf("invalid hash for cidv0 %d-%d", dec.Code, dec.Length)
}
return Cid{string(mhash)}, nil
} }
// NewCidV0 returns a Cid-wrapped multihash. // NewCidV0 returns a Cid-wrapped multihash.
// They exist to allow IPFS to work with Cids while keeping // They exist to allow IPFS to work with Cids while keeping
// compatibility with the plain-multihash format used used in IPFS. // compatibility with the plain-multihash format used used in IPFS.
// NewCidV1 should be used preferentially. // NewCidV1 should be used preferentially.
//
// Panics if the multihash isn't sha2-256.
func NewCidV0(mhash mh.Multihash) Cid { func NewCidV0(mhash mh.Multihash) Cid {
// Need to make sure hash is valid for CidV0 otherwise we will c, err := tryNewCidV0(mhash)
// incorrectly detect it as CidV1 in the Version() method
dec, err := mh.Decode(mhash)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if dec.Code != mh.SHA2_256 || dec.Length != 32 { return c
panic("invalid hash for cidv0")
}
return Cid{string(mhash)}
} }
// NewCidV1 returns a new Cid using the given multicodec-packed // NewCidV1 returns a new Cid using the given multicodec-packed
// content type. // content type.
//
// Panics if the multihash is invalid.
func NewCidV1(codecType uint64, mhash mh.Multihash) Cid { func NewCidV1(codecType uint64, mhash mh.Multihash) Cid {
hashlen := len(mhash) hashlen := len(mhash)
// two 8 bytes (max) numbers plus hash // two 8 bytes (max) numbers plus hash
buf := make([]byte, 2*binary.MaxVarintLen64+hashlen) buf := make([]byte, 1+varint.UvarintSize(codecType)+hashlen)
n := binary.PutUvarint(buf, 1) n := varint.PutUvarint(buf, 1)
n += binary.PutUvarint(buf[n:], codecType) n += varint.PutUvarint(buf[n:], codecType)
cn := copy(buf[n:], mhash) cn := copy(buf[n:], mhash)
if cn != hashlen { if cn != hashlen {
panic("copy hash length is inconsistent") panic("copy hash length is inconsistent")
@@ -203,7 +215,7 @@ func Parse(v interface{}) (Cid, error) {
case []byte: case []byte:
return Cast(v2) return Cast(v2)
case mh.Multihash: case mh.Multihash:
return NewCidV0(v2), nil return tryNewCidV0(v2)
case Cid: case Cid:
return v2, nil return v2, nil
default: default:
@@ -234,7 +246,7 @@ func Decode(v string) (Cid, error) {
return Undef, err return Undef, err
} }
return NewCidV0(hash), nil return tryNewCidV0(hash)
} }
_, data, err := mbase.Decode(v) _, data, err := mbase.Decode(v)
@@ -267,17 +279,6 @@ func ExtractEncoding(v string) (mbase.Encoding, error) {
return encoding, nil return encoding, nil
} }
func uvError(read int) error {
switch {
case read == 0:
return ErrVarintBuffSmall
case read < 0:
return ErrVarintTooBig
default:
return nil
}
}
// Cast takes a Cid data slice, parses it and returns a Cid. // Cast takes a Cid data slice, parses it and returns a Cid.
// For CidV1, the data buffer is in the form: // For CidV1, the data buffer is in the form:
// //
@@ -290,36 +291,16 @@ func uvError(read int) error {
// Please use decode when parsing a regular Cid string, as Cast does not // Please use decode when parsing a regular Cid string, as Cast does not
// expect multibase-encoded data. Cast accepts the output of Cid.Bytes(). // expect multibase-encoded data. Cast accepts the output of Cid.Bytes().
func Cast(data []byte) (Cid, error) { func Cast(data []byte) (Cid, error) {
if len(data) == 34 && data[0] == 18 && data[1] == 32 { nr, c, err := CidFromBytes(data)
h, err := mh.Cast(data)
if err != nil {
return Undef, err
}
return NewCidV0(h), nil
}
vers, n := binary.Uvarint(data)
if err := uvError(n); err != nil {
return Undef, err
}
if vers != 1 {
return Undef, fmt.Errorf("expected 1 as the cid version number, got: %d", vers)
}
_, cn := binary.Uvarint(data[n:])
if err := uvError(cn); err != nil {
return Undef, err
}
rest := data[n+cn:]
h, err := mh.Cast(rest)
if err != nil { if err != nil {
return Undef, err return Undef, err
} }
return Cid{string(data[0 : n+cn+len(h)])}, nil if nr != len(data) {
return Undef, fmt.Errorf("trailing bytes in data buffer passed to cid Cast")
}
return c, nil
} }
// UnmarshalBinary is equivalent to Cast(). It implements the // UnmarshalBinary is equivalent to Cast(). It implements the
@@ -357,14 +338,14 @@ func (c Cid) Type() uint64 {
if c.Version() == 0 { if c.Version() == 0 {
return DagProtobuf return DagProtobuf
} }
_, n := uvarint(c.str) _, n, _ := uvarint(c.str)
codec, _ := uvarint(c.str[n:]) codec, _, _ := uvarint(c.str[n:])
return codec return codec
} }
// String returns the default string representation of a // String returns the default string representation of a
// Cid. Currently, Base58 is used as the encoding for the // Cid. Currently, Base32 is used for CIDV1 as the encoding for the
// multibase string. // multibase string, Base58 is used for CIDV0.
func (c Cid) String() string { func (c Cid) String() string {
switch c.Version() { switch c.Version() {
case 0: case 0:
@@ -420,9 +401,9 @@ func (c Cid) Hash() mh.Multihash {
} }
// skip version length // skip version length
_, n1 := binary.Uvarint(bytes) _, n1, _ := varint.FromUvarint(bytes)
// skip codec length // skip codec length
_, n2 := binary.Uvarint(bytes[n1:]) _, n2, _ := varint.FromUvarint(bytes[n1:])
return mh.Multihash(bytes[n1+n2:]) return mh.Multihash(bytes[n1+n2:])
} }
@@ -434,6 +415,30 @@ func (c Cid) Bytes() []byte {
return []byte(c.str) return []byte(c.str)
} }
// ByteLen returns the length of the CID in bytes.
// It's equivalent to `len(c.Bytes())`, but works without an allocation,
// and should therefore be preferred.
//
// (See also the WriteTo method for other important operations that work without allocation.)
func (c Cid) ByteLen() int {
return len(c.str)
}
// WriteBytes writes the CID bytes to the given writer.
// This method works without incurring any allocation.
//
// (See also the ByteLen method for other important operations that work without allocation.)
func (c Cid) WriteBytes(w io.Writer) (int, error) {
n, err := io.WriteString(w, c.str)
if err != nil {
return n, err
}
if n != len(c.str) {
return n, fmt.Errorf("failed to write entire cid string")
}
return n, nil
}
// MarshalBinary is equivalent to Bytes(). It implements the // MarshalBinary is equivalent to Bytes(). It implements the
// encoding.BinaryMarshaler interface. // encoding.BinaryMarshaler interface.
func (c Cid) MarshalBinary() ([]byte, error) { func (c Cid) MarshalBinary() ([]byte, error) {
@@ -513,12 +518,29 @@ func (c Cid) Loggable() map[string]interface{} {
// Prefix builds and returns a Prefix out of a Cid. // Prefix builds and returns a Prefix out of a Cid.
func (c Cid) Prefix() Prefix { func (c Cid) Prefix() Prefix {
dec, _ := mh.Decode(c.Hash()) // assuming we got a valid multiaddr, this will not error if c.Version() == 0 {
return Prefix{
MhType: mh.SHA2_256,
MhLength: 32,
Version: 0,
Codec: DagProtobuf,
}
}
offset := 0
version, n, _ := uvarint(c.str[offset:])
offset += n
codec, n, _ := uvarint(c.str[offset:])
offset += n
mhtype, n, _ := uvarint(c.str[offset:])
offset += n
mhlen, _, _ := uvarint(c.str[offset:])
return Prefix{ return Prefix{
MhType: dec.Code, MhType: mhtype,
MhLength: dec.Length, MhLength: int(mhlen),
Version: c.Version(), Version: version,
Codec: c.Type(), Codec: codec,
} }
} }
@@ -543,6 +565,12 @@ func (p Prefix) Sum(data []byte) (Cid, error) {
length = -1 length = -1
} }
if p.Version == 0 && (p.MhType != mh.SHA2_256 ||
(p.MhLength != 32 && p.MhLength != -1)) {
return Undef, fmt.Errorf("invalid v0 prefix")
}
hash, err := mh.Sum(data, p.MhType, length) hash, err := mh.Sum(data, p.MhType, length)
if err != nil { if err != nil {
return Undef, err return Undef, err
@@ -562,34 +590,42 @@ func (p Prefix) Sum(data []byte) (Cid, error) {
// //
// <version><codec><mh-type><mh-length> // <version><codec><mh-type><mh-length>
func (p Prefix) Bytes() []byte { func (p Prefix) Bytes() []byte {
buf := make([]byte, 4*binary.MaxVarintLen64) size := varint.UvarintSize(p.Version)
n := binary.PutUvarint(buf, p.Version) size += varint.UvarintSize(p.Codec)
n += binary.PutUvarint(buf[n:], p.Codec) size += varint.UvarintSize(p.MhType)
n += binary.PutUvarint(buf[n:], uint64(p.MhType)) size += varint.UvarintSize(uint64(p.MhLength))
n += binary.PutUvarint(buf[n:], uint64(p.MhLength))
return buf[:n] buf := make([]byte, size)
n := varint.PutUvarint(buf, p.Version)
n += varint.PutUvarint(buf[n:], p.Codec)
n += varint.PutUvarint(buf[n:], p.MhType)
n += varint.PutUvarint(buf[n:], uint64(p.MhLength))
if n != size {
panic("size mismatch")
}
return buf
} }
// PrefixFromBytes parses a Prefix-byte representation onto a // PrefixFromBytes parses a Prefix-byte representation onto a
// Prefix. // Prefix.
func PrefixFromBytes(buf []byte) (Prefix, error) { func PrefixFromBytes(buf []byte) (Prefix, error) {
r := bytes.NewReader(buf) r := bytes.NewReader(buf)
vers, err := binary.ReadUvarint(r) vers, err := varint.ReadUvarint(r)
if err != nil { if err != nil {
return Prefix{}, err return Prefix{}, err
} }
codec, err := binary.ReadUvarint(r) codec, err := varint.ReadUvarint(r)
if err != nil { if err != nil {
return Prefix{}, err return Prefix{}, err
} }
mhtype, err := binary.ReadUvarint(r) mhtype, err := varint.ReadUvarint(r)
if err != nil { if err != nil {
return Prefix{}, err return Prefix{}, err
} }
mhlen, err := binary.ReadUvarint(r) mhlen, err := varint.ReadUvarint(r)
if err != nil { if err != nil {
return Prefix{}, err return Prefix{}, err
} }
@@ -601,3 +637,41 @@ func PrefixFromBytes(buf []byte) (Prefix, error) {
MhLength: int(mhlen), MhLength: int(mhlen),
}, nil }, nil
} }
func CidFromBytes(data []byte) (int, Cid, error) {
if len(data) > 2 && data[0] == mh.SHA2_256 && data[1] == 32 {
if len(data) < 34 {
return 0, Undef, fmt.Errorf("not enough bytes for cid v0")
}
h, err := mh.Cast(data[:34])
if err != nil {
return 0, Undef, err
}
return 34, Cid{string(h)}, nil
}
vers, n, err := varint.FromUvarint(data)
if err != nil {
return 0, Undef, err
}
if vers != 1 {
return 0, Undef, fmt.Errorf("expected 1 as the cid version number, got: %d", vers)
}
_, cn, err := varint.FromUvarint(data[n:])
if err != nil {
return 0, Undef, err
}
mhnr, _, err := mh.MHFromBytes(data[n+cn:])
if err != nil {
return 0, Undef, err
}
l := n + cn + mhnr
return l, Cid{string(data[0:l])}, nil
}

View File

@@ -16,28 +16,30 @@ import (
// https://github.com/multiformats/go-multihash/blob/7aa9f26a231c6f34f4e9fad52bf580fd36627285/multihash_test.go#L13 // https://github.com/multiformats/go-multihash/blob/7aa9f26a231c6f34f4e9fad52bf580fd36627285/multihash_test.go#L13
// Makes it so changing the table accidentally has to happen twice. // Makes it so changing the table accidentally has to happen twice.
var tCodecs = map[uint64]string{ var tCodecs = map[uint64]string{
Raw: "raw", Raw: "raw",
DagProtobuf: "protobuf", DagProtobuf: "protobuf",
DagCBOR: "cbor", DagCBOR: "cbor",
Libp2pKey: "libp2p-key", Libp2pKey: "libp2p-key",
GitRaw: "git-raw", GitRaw: "git-raw",
EthBlock: "eth-block", EthBlock: "eth-block",
EthBlockList: "eth-block-list", EthBlockList: "eth-block-list",
EthTxTrie: "eth-tx-trie", EthTxTrie: "eth-tx-trie",
EthTx: "eth-tx", EthTx: "eth-tx",
EthTxReceiptTrie: "eth-tx-receipt-trie", EthTxReceiptTrie: "eth-tx-receipt-trie",
EthTxReceipt: "eth-tx-receipt", EthTxReceipt: "eth-tx-receipt",
EthStateTrie: "eth-state-trie", EthStateTrie: "eth-state-trie",
EthAccountSnapshot: "eth-account-snapshot", EthAccountSnapshot: "eth-account-snapshot",
EthStorageTrie: "eth-storage-trie", EthStorageTrie: "eth-storage-trie",
BitcoinBlock: "bitcoin-block", BitcoinBlock: "bitcoin-block",
BitcoinTx: "bitcoin-tx", BitcoinTx: "bitcoin-tx",
ZcashBlock: "zcash-block", ZcashBlock: "zcash-block",
ZcashTx: "zcash-tx", ZcashTx: "zcash-tx",
DecredBlock: "decred-block", DecredBlock: "decred-block",
DecredTx: "decred-tx", DecredTx: "decred-tx",
DashBlock: "dash-block", DashBlock: "dash-block",
DashTx: "dash-tx", DashTx: "dash-tx",
FilCommitmentUnsealed: "fil-commitment-unsealed",
FilCommitmentSealed: "fil-commitment-sealed",
} }
func assertEqual(t *testing.T, a, b Cid) { func assertEqual(t *testing.T, a, b Cid) {
@@ -333,6 +335,41 @@ func TestNewPrefixV0(t *testing.T) {
if c1.Prefix() != c2.Prefix() { if c1.Prefix() != c2.Prefix() {
t.Fatal("prefixes mismatch") t.Fatal("prefixes mismatch")
} }
}
func TestInvalidV0Prefix(t *testing.T) {
tests := []Prefix{
{
MhType: mh.SHA2_256,
MhLength: 31,
},
{
MhType: mh.SHA2_256,
MhLength: 33,
},
{
MhType: mh.SHA2_256,
MhLength: -2,
},
{
MhType: mh.SHA2_512,
MhLength: 32,
},
{
MhType: mh.SHA2_512,
MhLength: -1,
},
}
for i, p := range tests {
t.Log(i)
_, err := p.Sum([]byte("testdata"))
if err == nil {
t.Fatalf("should error (index %d)", i)
}
}
} }
func TestPrefixRoundtrip(t *testing.T) { func TestPrefixRoundtrip(t *testing.T) {
@@ -515,3 +552,49 @@ func BenchmarkStringV1(b *testing.B) {
b.FailNow() b.FailNow()
} }
} }
func TestReadCidsFromBuffer(t *testing.T) {
cidstr := []string{
"bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm",
"k2cwueckqkibutvhkr4p2ln2pjcaxaakpd9db0e7j7ax1lxhhxy3ekpv",
"Qmf5Qzp6nGBku7CEn2UQx4mgN8TW69YUok36DrGa6NN893",
"zb2rhZi1JR4eNc2jBGaRYJKYM8JEB4ovenym8L1CmFsRAytkz",
}
var cids []Cid
var buf []byte
for _, cs := range cidstr {
c, err := Decode(cs)
if err != nil {
t.Fatal(err)
}
cids = append(cids, c)
buf = append(buf, c.Bytes()...)
}
var cur int
for _, expc := range cids {
n, c, err := CidFromBytes(buf[cur:])
if err != nil {
t.Fatal(err)
}
if c != expc {
t.Fatal("cids mismatched")
}
cur += n
}
if cur != len(buf) {
t.Fatal("had trailing bytes")
}
}
func TestBadParse(t *testing.T) {
hash, err := mh.Sum([]byte("foobar"), mh.SHA3_256, -1)
if err != nil {
t.Fatal(err)
}
_, err = Parse(hash)
if err == nil {
t.Fatal("expected to fail to parse an invalid CIDv1 CID")
}
}

7
go.mod
View File

@@ -1,6 +1,9 @@
module github.com/ipfs/go-cid module github.com/ipfs/go-cid
require ( require (
github.com/multiformats/go-multibase v0.0.1 github.com/multiformats/go-multibase v0.0.3
github.com/multiformats/go-multihash v0.0.1 github.com/multiformats/go-multihash v0.0.13
github.com/multiformats/go-varint v0.0.5
) )
go 1.13

36
go.sum
View File

@@ -1,20 +1,28 @@
github.com/gxed/hashland/keccakpg v0.0.1 h1:wrk3uMNaMxbXiHibbPO4S0ymqJMm41WiudyFSs7UnsU=
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
github.com/gxed/hashland/murmur3 v0.0.1 h1:SheiaIt0sda5K+8FLz952/1iWS9zrnKsEJaOJu4ZbSc=
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g= github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ= github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16 h1:5W7KhL8HVF3XCFOweFD3BNESdnO8ewyYTFT2R+/b8FQ= github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 h1:MHkK1uRtFbVqvAgvWxafZe54+5uBxLluGylDiKgdhwo=
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ= github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ=
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc=
github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI= github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA= github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
github.com/multiformats/go-multibase v0.0.1 h1:PN9/v21eLywrFWdFNsFKaU04kLJzuYzmrJR+ubhT9qA= github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4=
github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs= github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM=
github.com/multiformats/go-multihash v0.0.1 h1:HHwN1K12I+XllBCrqKnhX949Orn4oawPkegHMu2vDqQ= github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk=
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U= github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE= github.com/multiformats/go-multihash v0.0.13 h1:06x+mk/zj1FoMsgNejLpy6QTvJqlSt/BhLEy87zidlc=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
golang.org/x/sys v0.0.0-20190219092855-153ac476189d h1:Z0Ahzd7HltpJtjAHHxX8QFP3j1yYgiuvjbjRzDj/KH0= github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg=
golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXTqk7U7zPQCb+T4rbU9ZEoU=
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View File

@@ -1,30 +0,0 @@
{
"author": "whyrusleeping",
"bugs": {
"url": "https://github.com/ipfs/go-cid"
},
"gx": {
"dvcsimport": "github.com/ipfs/go-cid"
},
"gxDependencies": [
{
"author": "whyrusleeping",
"hash": "QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW",
"name": "go-multihash",
"version": "1.0.9"
},
{
"author": "whyrusleeping",
"hash": "QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd",
"name": "go-multibase",
"version": "0.3.0"
}
],
"gxVersion": "0.8.0",
"language": "go",
"license": "MIT",
"name": "go-cid",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.9.3"
}

View File

@@ -1,5 +1,9 @@
package cid package cid
import (
"github.com/multiformats/go-varint"
)
// Version of varint function that work with a string rather than // Version of varint function that work with a string rather than
// []byte to avoid unnecessary allocation // []byte to avoid unnecessary allocation
@@ -15,7 +19,7 @@ package cid
// n < 0: value larger than 64 bits (overflow) // n < 0: value larger than 64 bits (overflow)
// and -n is the number of bytes read // and -n is the number of bytes read
// //
func uvarint(buf string) (uint64, int) { func uvarint(buf string) (uint64, int, error) {
var x uint64 var x uint64
var s uint var s uint
// we have a binary string so we can't use a range loope // we have a binary string so we can't use a range loope
@@ -23,12 +27,14 @@ func uvarint(buf string) (uint64, int) {
b := buf[i] b := buf[i]
if b < 0x80 { if b < 0x80 {
if i > 9 || i == 9 && b > 1 { if i > 9 || i == 9 && b > 1 {
return 0, -(i + 1) // overflow return 0, 0, varint.ErrOverflow
} else if b == 0 && i > 0 {
return 0, 0, varint.ErrNotMinimal
} }
return x | uint64(b)<<s, i + 1 return x | uint64(b)<<s, i + 1, nil
} }
x |= uint64(b&0x7f) << s x |= uint64(b&0x7f) << s
s += 7 s += 7
} }
return 0, 0 return 0, 0, varint.ErrUnderflow
} }

View File

@@ -1,17 +1,25 @@
package cid package cid
import ( import (
"encoding/binary"
"testing" "testing"
"github.com/multiformats/go-varint"
) )
func TestUvarintRoundTrip(t *testing.T) { func TestUvarintRoundTrip(t *testing.T) {
testCases := []uint64{0, 1, 2, 127, 128, 129, 255, 256, 257, 1<<63 - 1} testCases := []uint64{0, 1, 2, 127, 128, 129, 255, 256, 257, 1<<63 - 1}
for _, tc := range testCases { for _, tc := range testCases {
t.Log("testing", tc)
buf := make([]byte, 16) buf := make([]byte, 16)
binary.PutUvarint(buf, tc) varint.PutUvarint(buf, tc)
v, l1 := uvarint(string(buf)) v, l1, err := uvarint(string(buf))
_, l2 := binary.Uvarint(buf) if err != nil {
t.Fatalf("%v: %s", buf, err)
}
_, l2, err := varint.FromUvarint(buf)
if err != nil {
t.Fatal(err)
}
if tc != v { if tc != v {
t.Errorf("roundtrip failed expected %d but got %d", tc, v) t.Errorf("roundtrip failed expected %d but got %d", tc, v)
} }