master
go 146 lines 3.3 KB
Raw
1 package cmdenv
2
3 import (
4 "testing"
5
6 cidenc "github.com/ipfs/go-cidutil/cidenc"
7 cmds "github.com/ipfs/go-ipfs-cmds"
8 mbase "github.com/multiformats/go-multibase"
9 )
10
11 func TestGetCidEncoder(t *testing.T) {
12 makeReq := func(opts map[string]any) *cmds.Request {
13 if opts == nil {
14 opts = map[string]any{}
15 }
16 return &cmds.Request{Options: opts}
17 }
18
19 t.Run("no options returns default encoder", func(t *testing.T) {
20 enc, err := GetCidEncoder(makeReq(nil))
21 if err != nil {
22 t.Fatal(err)
23 }
24 if enc.Upgrade {
25 t.Error("expected Upgrade=false with no options")
26 }
27 })
28
29 t.Run("non-base58btc base auto-upgrades CIDv0", func(t *testing.T) {
30 enc, err := GetCidEncoder(makeReq(map[string]any{
31 "cid-base": "base32",
32 }))
33 if err != nil {
34 t.Fatal(err)
35 }
36 if !enc.Upgrade {
37 t.Error("expected Upgrade=true for base32")
38 }
39 if enc.Base.Encoding() != mbase.Base32 {
40 t.Errorf("expected base32 encoding, got %v", enc.Base.Encoding())
41 }
42 })
43
44 t.Run("base58btc does not auto-upgrade", func(t *testing.T) {
45 enc, err := GetCidEncoder(makeReq(map[string]any{
46 "cid-base": "base58btc",
47 }))
48 if err != nil {
49 t.Fatal(err)
50 }
51 if enc.Upgrade {
52 t.Error("expected Upgrade=false for base58btc")
53 }
54 })
55
56 t.Run("deprecated flag still works as override", func(t *testing.T) {
57 // Explicitly disable upgrade even with non-base58btc base
58 enc, err := GetCidEncoder(makeReq(map[string]any{
59 "cid-base": "base32",
60 "upgrade-cidv0-in-output": false,
61 }))
62 if err != nil {
63 t.Fatal(err)
64 }
65 if enc.Upgrade {
66 t.Error("expected Upgrade=false when explicitly disabled")
67 }
68
69 // Explicitly enable upgrade even with base58btc
70 enc, err = GetCidEncoder(makeReq(map[string]any{
71 "cid-base": "base58btc",
72 "upgrade-cidv0-in-output": true,
73 }))
74 if err != nil {
75 t.Fatal(err)
76 }
77 if !enc.Upgrade {
78 t.Error("expected Upgrade=true when explicitly enabled")
79 }
80 })
81 }
82
83 func TestEncoderFromPath(t *testing.T) {
84 test := func(path string, expected cidenc.Encoder) {
85 actual, err := CidEncoderFromPath(path)
86 if err != nil {
87 t.Error(err)
88 }
89 if actual != expected {
90 t.Errorf("CidEncoderFromPath(%s) failed: expected %#v but got %#v", path, expected, actual)
91 }
92 }
93 p := "QmRqVG8VGdKZ7KARqR96MV7VNHgWvEQifk94br5HpURpfu"
94 enc := cidenc.Default()
95 test(p, enc)
96 test(p+"/a", enc)
97 test(p+"/a/b", enc)
98 test(p+"/a/b/", enc)
99 test(p+"/a/b/c", enc)
100 test("/ipfs/"+p, enc)
101 test("/ipfs/"+p+"/b", enc)
102
103 p = "zb2rhfkM4FjkMLaUnygwhuqkETzbYXnUDf1P9MSmdNjW1w1Lk"
104 enc = cidenc.Encoder{
105 Base: mbase.MustNewEncoder(mbase.Base58BTC),
106 Upgrade: true,
107 }
108 test(p, enc)
109 test(p+"/a", enc)
110 test(p+"/a/b", enc)
111 test(p+"/a/b/", enc)
112 test(p+"/a/b/c", enc)
113 test("/ipfs/"+p, enc)
114 test("/ipfs/"+p+"/b", enc)
115 test("/ipld/"+p, enc)
116 test("/ipns/"+p, enc) // even IPNS should work.
117
118 p = "bafyreifrcnyjokuw4i4ggkzg534tjlc25lqgt3ttznflmyv5fftdgu52hm"
119 enc = cidenc.Encoder{
120 Base: mbase.MustNewEncoder(mbase.Base32),
121 Upgrade: true,
122 }
123 test(p, enc)
124 test("/ipfs/"+p, enc)
125 test("/ipld/"+p, enc)
126
127 for _, badPath := range []string{
128 "/ipld/",
129 "/ipld",
130 "/ipld//",
131 "ipld//",
132 "ipld",
133 "",
134 "ipns",
135 "/ipfs/asdf",
136 "/ipfs/...",
137 "...",
138 "abcdefg",
139 "boo",
140 } {
141 _, err := CidEncoderFromPath(badPath)
142 if err == nil {
143 t.Errorf("expected error extracting encoder from bad path: %s", badPath)
144 }
145 }
146 }