| 1 | package rpc |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/ipfs/go-cid" |
| 9 | ipld "github.com/ipfs/go-ipld-format" |
| 10 | mbase "github.com/multiformats/go-multibase" |
| 11 | mh "github.com/multiformats/go-multihash" |
| 12 | ) |
| 13 | |
| 14 | var randomSha256MH = mh.Multihash{0x12, 0x20, 0x88, 0x82, 0x73, 0x37, 0x7c, 0xc1, 0xc9, 0x96, 0xad, 0xee, 0xd, 0x26, 0x84, 0x2, 0xc9, 0xc9, 0x5c, 0xf9, 0x5c, 0x4d, 0x9b, 0xc3, 0x3f, 0xfb, 0x4a, 0xd8, 0xaf, 0x28, 0x6b, 0xca, 0x1a, 0xf2} |
| 15 | |
| 16 | func doParseIpldNotFoundTest(t *testing.T, original error) { |
| 17 | originalMsg := original.Error() |
| 18 | |
| 19 | rebuilt := parseErrNotFoundWithFallbackToMSG(originalMsg) |
| 20 | |
| 21 | rebuiltMsg := rebuilt.Error() |
| 22 | |
| 23 | if originalMsg != rebuiltMsg { |
| 24 | t.Errorf("expected message to be %q; got %q", originalMsg, rebuiltMsg) |
| 25 | } |
| 26 | |
| 27 | originalNotFound := ipld.IsNotFound(original) |
| 28 | rebuiltNotFound := ipld.IsNotFound(rebuilt) |
| 29 | if originalNotFound != rebuiltNotFound { |
| 30 | t.Errorf("for %q expected Ipld.IsNotFound to be %t; got %t", originalMsg, originalNotFound, rebuiltNotFound) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestParseIPLDNotFound(t *testing.T) { |
| 35 | t.Parallel() |
| 36 | |
| 37 | if err := parseErrNotFoundWithFallbackToMSG(""); err != nil { |
| 38 | t.Errorf("expected empty string to give no error; got %T %q", err, err.Error()) |
| 39 | } |
| 40 | |
| 41 | cidBreaks := make([]string, len(cidBreakSet)) |
| 42 | for i, v := range cidBreakSet { |
| 43 | cidBreaks[i] = "%w" + string(v) |
| 44 | } |
| 45 | |
| 46 | base58BTCEncoder, err := mbase.NewEncoder(mbase.Base58BTC) |
| 47 | if err != nil { |
| 48 | t.Fatalf("expected to find Base58BTC encoder; got error %q", err.Error()) |
| 49 | } |
| 50 | |
| 51 | for _, wrap := range append(cidBreaks, |
| 52 | "", |
| 53 | "merkledag: %w", |
| 54 | "testing: %w the test", |
| 55 | "%w is wrong", |
| 56 | ) { |
| 57 | for _, err := range [...]error{ |
| 58 | errors.New("ipld: could not find "), |
| 59 | errors.New("ipld: could not find Bad_CID"), |
| 60 | errors.New("ipld: could not find " + cid.NewCidV1(cid.Raw, randomSha256MH).Encode(base58BTCEncoder)), // Test that we only accept CIDv0 and base32 CIDs |
| 61 | errors.New("network connection timeout"), |
| 62 | ipld.ErrNotFound{Cid: cid.Undef}, |
| 63 | ipld.ErrNotFound{Cid: cid.NewCidV0(randomSha256MH)}, |
| 64 | ipld.ErrNotFound{Cid: cid.NewCidV1(cid.Raw, randomSha256MH)}, |
| 65 | } { |
| 66 | if wrap != "" { |
| 67 | err = fmt.Errorf(wrap, err) |
| 68 | } |
| 69 | |
| 70 | doParseIpldNotFoundTest(t, err) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestBlockstoreNotFoundMatchingIPLDErrNotFound(t *testing.T) { |
| 76 | t.Parallel() |
| 77 | |
| 78 | if !ipld.IsNotFound(blockstoreNotFoundMatchingIPLDErrNotFound{}) { |
| 79 | t.Fatalf("expected blockstoreNotFoundMatchingIPLDErrNotFound to match ipld.IsNotFound; got false") |
| 80 | } |
| 81 | |
| 82 | for _, wrap := range [...]string{ |
| 83 | "", |
| 84 | "merkledag: %w", |
| 85 | "testing: %w the test", |
| 86 | "%w is wrong", |
| 87 | } { |
| 88 | for _, err := range [...]error{ |
| 89 | errors.New("network connection timeout"), |
| 90 | blockstoreNotFoundMatchingIPLDErrNotFound{"blockstore: block not found"}, |
| 91 | } { |
| 92 | if wrap != "" { |
| 93 | err = fmt.Errorf(wrap, err) |
| 94 | } |
| 95 | |
| 96 | doParseIpldNotFoundTest(t, err) |
| 97 | } |
| 98 | } |
| 99 | } |