@cryptotaxi247 / kubo / commits / 2517d0b9d

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 This commit was moved from ipfs/boxo@7bcd64373825dfda1ef91c9d9157a5328928cc09

Petar Maymounkov committed Jul 22, 2020 at 09:05 UTC 2517d0b9d49911add8b923a48030e4f7cbf0a0b9
3 files changed +66 -32
core/coreiface/idfmt.go new
+19
@@ -0,0 +1,19 @@
1 +package iface
2 +
3 +import (
4 + peer "github.com/libp2p/go-libp2p-core/peer"
5 + mbase "github.com/multiformats/go-multibase"
6 +)
7 +
8 +func FormatKeyID(id peer.ID) string {
9 + if s, err := peer.ToCid(id).StringOfBase(mbase.Base36); err != nil {
10 + panic(err)
11 + } else {
12 + return s
13 + }
14 +}
15 +
16 +// FormatKey formats the given IPNS key in a canonical way.
17 +func FormatKey(key Key) string {
18 + return FormatKeyID(key.ID())
19 +}
core/coreiface/tests/key.go
+32 -17
@@ -5,8 +5,11 @@ import (
5 "strings"
6 "testing"
7
8 - "github.com/ipfs/interface-go-ipfs-core"
8 + cid "github.com/ipfs/go-cid"
9 + coreiface "github.com/ipfs/interface-go-ipfs-core"
10 + iface "github.com/ipfs/interface-go-ipfs-core"
11 opt "github.com/ipfs/interface-go-ipfs-core/options"
12 + mbase "github.com/multiformats/go-multibase"
13 )
14
15 func (tp *TestSuite) TestKey(t *testing.T) {
@@ -64,8 +67,8 @@ func (tp *TestSuite) TestListSelf(t *testing.T) {
67 t.Errorf("expected the key to be called 'self', got '%s'", keys[0].Name())
68 }
69
67 - if keys[0].Path().String() != "/ipns/"+self.ID().Pretty() {
68 - t.Errorf("expected the key to have path '/ipns/%s', got '%s'", self.ID().Pretty(), keys[0].Path().String())
70 + if keys[0].Path().String() != "/ipns/"+coreiface.FormatKeyID(self.ID()) {
71 + t.Errorf("expected the key to have path '/ipns/%s', got '%s'", coreiface.FormatKeyID(self.ID()), keys[0].Path().String())
72 }
73 }
74
@@ -134,9 +137,30 @@ func (tp *TestSuite) TestGenerate(t *testing.T) {
137 t.Errorf("expected the key to be called 'foo', got '%s'", k.Name())
138 }
139
137 - if !strings.HasPrefix(k.Path().String(), "/ipns/Qm") {
138 - t.Errorf("expected the key to be prefixed with '/ipns/Qm', got '%s'", k.Path().String())
140 + verifyIPNSPath(t, k.Path().String())
141 +}
142 +
143 +func verifyIPNSPath(t *testing.T, p string) bool {
144 + t.Helper()
145 + if !strings.HasPrefix(p, "/ipns/") {
146 + t.Errorf("path %q does not look like an IPNS path", p)
147 + return false
148 + }
149 + k := p[len("/ipns/"):]
150 + c, err := cid.Decode(k)
151 + if err != nil {
152 + t.Errorf("failed to decode IPNS key %q (%v)", k, err)
153 + return false
154 + }
155 + b36, err := c.StringOfBase(mbase.Base36)
156 + if err != nil {
157 + t.Fatalf("cid cannot format itself in b36")
158 + return false
159 + }
160 + if b36 != k {
161 + t.Errorf("IPNS key is not base36")
162 }
163 + return true
164 }
165
166 func (tp *TestSuite) TestGenerateSize(t *testing.T) {
@@ -157,9 +181,7 @@ func (tp *TestSuite) TestGenerateSize(t *testing.T) {
181 t.Errorf("expected the key to be called 'foo', got '%s'", k.Name())
182 }
183
160 - if !strings.HasPrefix(k.Path().String(), "/ipns/Qm") {
161 - t.Errorf("expected the key to be prefixed with '/ipns/Qm', got '%s'", k.Path().String())
162 - }
184 + verifyIPNSPath(t, k.Path().String())
185 }
186
187 func (tp *TestSuite) TestGenerateType(t *testing.T) {
@@ -256,15 +278,8 @@ func (tp *TestSuite) TestList(t *testing.T) {
278 return
279 }
280
259 - if !strings.HasPrefix(l[0].Path().String(), "/ipns/Qm") {
260 - t.Fatalf("expected key 0 to be prefixed with '/ipns/Qm', got '%s'", l[0].Name())
261 - return
262 - }
263 -
264 - if !strings.HasPrefix(l[1].Path().String(), "/ipns/Qm") {
265 - t.Fatalf("expected key 1 to be prefixed with '/ipns/Qm', got '%s'", l[1].Name())
266 - return
267 - }
281 + verifyIPNSPath(t, l[0].Path().String())
282 + verifyIPNSPath(t, l[1].Path().String())
283 }
284
285 func (tp *TestSuite) TestRename(t *testing.T) {
core/coreiface/tests/name.go
+15 -15
@@ -2,15 +2,15 @@ package tests
2
3 import (
4 "context"
5 - path "github.com/ipfs/interface-go-ipfs-core/path"
5 "io"
6 "math/rand"
7 gopath "path"
8 "testing"
9 "time"
10
12 - "github.com/ipfs/go-ipfs-files"
13 - ipath "github.com/ipfs/go-path"
11 + path "github.com/ipfs/interface-go-ipfs-core/path"
12 +
13 + files "github.com/ipfs/go-ipfs-files"
14
15 coreiface "github.com/ipfs/interface-go-ipfs-core"
16 opt "github.com/ipfs/interface-go-ipfs-core/options"
@@ -70,8 +70,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) {
70 t.Fatal(err)
71 }
72
73 - if e.Name() != self.ID().Pretty() {
74 - t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
73 + if e.Name() != coreiface.FormatKeyID(self.ID()) {
74 + t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
75 }
76
77 if e.Value().String() != p.String() {
@@ -100,8 +100,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) {
100 t.Fatal(err)
101 }
102
103 - if e.Name() != self.ID().Pretty() {
104 - t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
103 + if e.Name() != coreiface.FormatKeyID(self.ID()) {
104 + t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
105 }
106
107 if e.Value().String() != p.String()+"/test" {
@@ -130,8 +130,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) {
130 t.Fatal(err)
131 }
132
133 - if e.Name() != self.ID().Pretty() {
134 - t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
133 + if e.Name() != coreiface.FormatKeyID(self.ID()) {
134 + t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
135 }
136
137 if e.Value().String() != p.String() {
@@ -160,8 +160,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) {
160 t.Fatal(err)
161 }
162
163 - if e.Name() != self.ID().Pretty() {
164 - t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
163 + if e.Name() != coreiface.FormatKeyID(self.ID()) {
164 + t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
165 }
166
167 if e.Value().String() != p.String()+"/a" {
@@ -212,8 +212,8 @@ func (tp *TestSuite) TestBasicPublishResolveKey(t *testing.T) {
212 t.Fatal(err)
213 }
214
215 - if ipath.Join([]string{"/ipns", e.Name()}) != k.Path().String() {
216 - t.Errorf("expected e.Name to equal '%s', got '%s'", e.Name(), k.Path().String())
215 + if e.Name() != coreiface.FormatKey(k) {
216 + t.Errorf("expected e.Name to equal %s, got '%s'", e.Name(), coreiface.FormatKey(k))
217 }
218
219 if e.Value().String() != p.String() {
@@ -255,8 +255,8 @@ func (tp *TestSuite) TestBasicPublishResolveTimeout(t *testing.T) {
255 t.Fatal(err)
256 }
257
258 - if e.Name() != self.ID().Pretty() {
259 - t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
258 + if e.Name() != coreiface.FormatKeyID(self.ID()) {
259 + t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
260 }
261
262 if e.Value().String() != p.String() {