| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | cmds "github.com/ipfs/go-ipfs-cmds" |
| 7 | "github.com/multiformats/go-multibase" |
| 8 | ) |
| 9 | |
| 10 | func TestCidFmtCmd(t *testing.T) { |
| 11 | t.Parallel() |
| 12 | |
| 13 | // Test 'error when -v 0 is present and a custom -b is passed' |
| 14 | t.Run("ipfs cid format <cid> -b z -v 0", func(t *testing.T) { |
| 15 | t.Parallel() |
| 16 | |
| 17 | type testV0PresentAndCustomBaseCase struct { |
| 18 | MultibaseName string |
| 19 | ExpectedErrMsg string |
| 20 | } |
| 21 | |
| 22 | var testV0PresentAndCustomBaseCases []testV0PresentAndCustomBaseCase |
| 23 | |
| 24 | for _, e := range multibase.EncodingToStr { |
| 25 | var testCase testV0PresentAndCustomBaseCase |
| 26 | |
| 27 | if e == "base58btc" { |
| 28 | testCase.MultibaseName = e |
| 29 | testCase.ExpectedErrMsg = "" |
| 30 | testV0PresentAndCustomBaseCases = append(testV0PresentAndCustomBaseCases, testCase) |
| 31 | continue |
| 32 | } |
| 33 | testCase.MultibaseName = e |
| 34 | testCase.ExpectedErrMsg = "cannot convert to CIDv0 with any multibase other than the implicit base58btc" |
| 35 | testV0PresentAndCustomBaseCases = append(testV0PresentAndCustomBaseCases, testCase) |
| 36 | } |
| 37 | |
| 38 | for _, e := range testV0PresentAndCustomBaseCases { |
| 39 | |
| 40 | // Mock request |
| 41 | req := &cmds.Request{ |
| 42 | Options: map[string]any{ |
| 43 | cidToVersionOptionName: "0", |
| 44 | cidMultibaseOptionName: e.MultibaseName, |
| 45 | cidFormatOptionName: "%s", |
| 46 | }, |
| 47 | } |
| 48 | |
| 49 | // Response emitter |
| 50 | resp := cmds.ResponseEmitter(nil) |
| 51 | |
| 52 | // Call the CidFmtCmd function with the mock request and response |
| 53 | err := cidFmtCmd.Run(req, resp, nil) |
| 54 | if err == nil && e.MultibaseName == "base58btc" { |
| 55 | continue |
| 56 | } |
| 57 | |
| 58 | errMsg := err.Error() |
| 59 | if errMsg != e.ExpectedErrMsg { |
| 60 | t.Errorf("Expected %s, got %s instead", e.ExpectedErrMsg, errMsg) |
| 61 | } |
| 62 | } |
| 63 | }) |
| 64 | |
| 65 | // Test 'upgrade CID to v1 when passing a custom -b and no -v is specified' |
| 66 | t.Run("ipfs cid format <cid-version-0> -b z", func(t *testing.T) { |
| 67 | t.Parallel() |
| 68 | |
| 69 | type testImplicitVersionAndCustomMultibaseCase struct { |
| 70 | Ver string |
| 71 | CidV1 string |
| 72 | CidV0 string |
| 73 | MultibaseName string |
| 74 | } |
| 75 | |
| 76 | var testCases = []testImplicitVersionAndCustomMultibaseCase{ |
| 77 | { |
| 78 | Ver: "", |
| 79 | CidV1: "zdj7WWwMSWGoyxYkkT7mHgYvr6tV8CYd77aYxxqSbg9HsiMcE", |
| 80 | CidV0: "QmPr755CxWUwt39C2Yiw4UGKrv16uZhSgeZJmoHUUS9TSJ", |
| 81 | MultibaseName: "z", |
| 82 | }, |
| 83 | { |
| 84 | Ver: "", |
| 85 | CidV1: "CAFYBEIDI7ZABPGG3S63QW3AJG2XAZNE4NJQPN777WLWYRAIDG3TE5QFN3A======", |
| 86 | CidV0: "QmVQVyEijmLb2cBQrowNQsaPbnUnJhfDK1sYe3wepm6ySf", |
| 87 | MultibaseName: "base32padupper", |
| 88 | }, |
| 89 | } |
| 90 | for _, e := range testCases { |
| 91 | // Mock request |
| 92 | req := &cmds.Request{ |
| 93 | Options: map[string]any{ |
| 94 | cidToVersionOptionName: e.Ver, |
| 95 | cidMultibaseOptionName: e.MultibaseName, |
| 96 | cidFormatOptionName: "%s", |
| 97 | }, |
| 98 | } |
| 99 | |
| 100 | // Response emitter |
| 101 | resp := cmds.ResponseEmitter(nil) |
| 102 | |
| 103 | // Call the CidFmtCmd function with the mock request and response |
| 104 | err := cidFmtCmd.Run(req, resp, nil) |
| 105 | |
| 106 | if err != nil { |
| 107 | t.Error(err) |
| 108 | } |
| 109 | } |
| 110 | }) |
| 111 | } |