From 91b27a6dd824ca65f08031fb42bd21d41d7f5636 Mon Sep 17 00:00:00 2001 From: Petar Maymounkov Date: Wed, 22 Jul 2020 09:05:14 -0700 Subject: [PATCH] add id and key formatting utils; format keys as b36cid by default; update tests This commit was moved from ipfs/interface-go-ipfs-core@c604c5b0338c075046d7ddf0b60c5927500607d3 --- coreapi/iface/idfmt.go | 19 ++++++++++++++ coreapi/iface/tests/key.go | 49 ++++++++++++++++++++++++------------- coreapi/iface/tests/name.go | 30 +++++++++++------------ 3 files changed, 66 insertions(+), 32 deletions(-) create mode 100644 coreapi/iface/idfmt.go diff --git a/coreapi/iface/idfmt.go b/coreapi/iface/idfmt.go new file mode 100644 index 000000000..1ba79e602 --- /dev/null +++ b/coreapi/iface/idfmt.go @@ -0,0 +1,19 @@ +package iface + +import ( + peer "github.com/libp2p/go-libp2p-core/peer" + mbase "github.com/multiformats/go-multibase" +) + +func FormatKeyID(id peer.ID) string { + if s, err := peer.ToCid(id).StringOfBase(mbase.Base36); err != nil { + panic(err) + } else { + return s + } +} + +// FormatKey formats the given IPNS key in a canonical way. +func FormatKey(key Key) string { + return FormatKeyID(key.ID()) +} diff --git a/coreapi/iface/tests/key.go b/coreapi/iface/tests/key.go index 265a8f060..c3cd8626f 100644 --- a/coreapi/iface/tests/key.go +++ b/coreapi/iface/tests/key.go @@ -5,8 +5,11 @@ import ( "strings" "testing" - "github.com/ipfs/interface-go-ipfs-core" + cid "github.com/ipfs/go-cid" + coreiface "github.com/ipfs/interface-go-ipfs-core" + iface "github.com/ipfs/interface-go-ipfs-core" opt "github.com/ipfs/interface-go-ipfs-core/options" + mbase "github.com/multiformats/go-multibase" ) func (tp *TestSuite) TestKey(t *testing.T) { @@ -64,8 +67,8 @@ func (tp *TestSuite) TestListSelf(t *testing.T) { t.Errorf("expected the key to be called 'self', got '%s'", keys[0].Name()) } - if keys[0].Path().String() != "/ipns/"+self.ID().Pretty() { - t.Errorf("expected the key to have path '/ipns/%s', got '%s'", self.ID().Pretty(), keys[0].Path().String()) + if keys[0].Path().String() != "/ipns/"+coreiface.FormatKeyID(self.ID()) { + t.Errorf("expected the key to have path '/ipns/%s', got '%s'", coreiface.FormatKeyID(self.ID()), keys[0].Path().String()) } } @@ -134,9 +137,30 @@ func (tp *TestSuite) TestGenerate(t *testing.T) { t.Errorf("expected the key to be called 'foo', got '%s'", k.Name()) } - if !strings.HasPrefix(k.Path().String(), "/ipns/Qm") { - t.Errorf("expected the key to be prefixed with '/ipns/Qm', got '%s'", k.Path().String()) + verifyIPNSPath(t, k.Path().String()) +} + +func verifyIPNSPath(t *testing.T, p string) bool { + t.Helper() + if !strings.HasPrefix(p, "/ipns/") { + t.Errorf("path %q does not look like an IPNS path", p) + return false + } + k := p[len("/ipns/"):] + c, err := cid.Decode(k) + if err != nil { + t.Errorf("failed to decode IPNS key %q (%v)", k, err) + return false + } + b36, err := c.StringOfBase(mbase.Base36) + if err != nil { + t.Fatalf("cid cannot format itself in b36") + return false + } + if b36 != k { + t.Errorf("IPNS key is not base36") } + return true } func (tp *TestSuite) TestGenerateSize(t *testing.T) { @@ -157,9 +181,7 @@ func (tp *TestSuite) TestGenerateSize(t *testing.T) { t.Errorf("expected the key to be called 'foo', got '%s'", k.Name()) } - if !strings.HasPrefix(k.Path().String(), "/ipns/Qm") { - t.Errorf("expected the key to be prefixed with '/ipns/Qm', got '%s'", k.Path().String()) - } + verifyIPNSPath(t, k.Path().String()) } func (tp *TestSuite) TestGenerateType(t *testing.T) { @@ -256,15 +278,8 @@ func (tp *TestSuite) TestList(t *testing.T) { return } - if !strings.HasPrefix(l[0].Path().String(), "/ipns/Qm") { - t.Fatalf("expected key 0 to be prefixed with '/ipns/Qm', got '%s'", l[0].Name()) - return - } - - if !strings.HasPrefix(l[1].Path().String(), "/ipns/Qm") { - t.Fatalf("expected key 1 to be prefixed with '/ipns/Qm', got '%s'", l[1].Name()) - return - } + verifyIPNSPath(t, l[0].Path().String()) + verifyIPNSPath(t, l[1].Path().String()) } func (tp *TestSuite) TestRename(t *testing.T) { diff --git a/coreapi/iface/tests/name.go b/coreapi/iface/tests/name.go index 31a5c1466..021c1bb97 100644 --- a/coreapi/iface/tests/name.go +++ b/coreapi/iface/tests/name.go @@ -2,15 +2,15 @@ package tests import ( "context" - path "github.com/ipfs/interface-go-ipfs-core/path" "io" "math/rand" gopath "path" "testing" "time" - "github.com/ipfs/go-ipfs-files" - ipath "github.com/ipfs/go-path" + path "github.com/ipfs/interface-go-ipfs-core/path" + + files "github.com/ipfs/go-ipfs-files" coreiface "github.com/ipfs/interface-go-ipfs-core" opt "github.com/ipfs/interface-go-ipfs-core/options" @@ -70,8 +70,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) { t.Fatal(err) } - if e.Name() != self.ID().Pretty() { - t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name()) + if e.Name() != coreiface.FormatKeyID(self.ID()) { + t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) } if e.Value().String() != p.String() { @@ -100,8 +100,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) { t.Fatal(err) } - if e.Name() != self.ID().Pretty() { - t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name()) + if e.Name() != coreiface.FormatKeyID(self.ID()) { + t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) } if e.Value().String() != p.String()+"/test" { @@ -130,8 +130,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) { t.Fatal(err) } - if e.Name() != self.ID().Pretty() { - t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name()) + if e.Name() != coreiface.FormatKeyID(self.ID()) { + t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) } if e.Value().String() != p.String() { @@ -160,8 +160,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) { t.Fatal(err) } - if e.Name() != self.ID().Pretty() { - t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name()) + if e.Name() != coreiface.FormatKeyID(self.ID()) { + t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) } if e.Value().String() != p.String()+"/a" { @@ -212,8 +212,8 @@ func (tp *TestSuite) TestBasicPublishResolveKey(t *testing.T) { t.Fatal(err) } - if ipath.Join([]string{"/ipns", e.Name()}) != k.Path().String() { - t.Errorf("expected e.Name to equal '%s', got '%s'", e.Name(), k.Path().String()) + if e.Name() != coreiface.FormatKey(k) { + t.Errorf("expected e.Name to equal %s, got '%s'", e.Name(), coreiface.FormatKey(k)) } if e.Value().String() != p.String() { @@ -255,8 +255,8 @@ func (tp *TestSuite) TestBasicPublishResolveTimeout(t *testing.T) { t.Fatal(err) } - if e.Name() != self.ID().Pretty() { - t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name()) + if e.Name() != coreiface.FormatKeyID(self.ID()) { + t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) } if e.Value().String() != p.String() {