Unverified Commit b935dfe5 authored by Adin Schmahmann's avatar Adin Schmahmann Committed by GitHub

Merge pull request #65 from ipfs/petar/namefmt

Add ID formatting functions, used by various IPFS cli commands
parents ee0d435c c604c5b0
......@@ -4,7 +4,7 @@ os:
language: go
go:
- 1.11.x
- 1.13.x
env:
global:
......
This diff is collapsed.
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())
}
......@@ -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) {
......
......@@ -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() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment