coreapi unixfs: better add tests
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Sep 20, 2018 at 16:00 UTC
06a4218a0324beb4addff61ffa737a22294b17e0
1 file changed
+76
-81
core/coreapi/unixfs_test.go
+76
-81
@@ -11,15 +11,16 @@ import (
11
"strings"
12
"testing"
13
14
- core "github.com/ipfs/go-ipfs/core"
15
- coreapi "github.com/ipfs/go-ipfs/core/coreapi"
14
+ "github.com/ipfs/go-ipfs/core"
15
+ "github.com/ipfs/go-ipfs/core/coreapi"
16
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
17
- options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
18
- coreunix "github.com/ipfs/go-ipfs/core/coreunix"
17
+ "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
18
+ "github.com/ipfs/go-ipfs/core/coreunix"
19
mock "github.com/ipfs/go-ipfs/core/mock"
20
- keystore "github.com/ipfs/go-ipfs/keystore"
21
- repo "github.com/ipfs/go-ipfs/repo"
20
+ "github.com/ipfs/go-ipfs/keystore"
21
+ "github.com/ipfs/go-ipfs/repo"
22
23
+ mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
24
ci "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
25
cbor "gx/ipfs/QmSywXfm2v4Qkp4DcFqo8eehj49dJK3bdUnaLVxrdFLMQn/go-ipld-cbor"
26
unixfs "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
@@ -39,7 +40,6 @@ var hello = "/ipfs/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk"
40
var helloStr = "hello, world!"
41
42
// `echo -n | ipfs add`
42
-var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
43
44
func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]*core.IpfsNode, []coreiface.CoreAPI, error) {
45
mn := mocknet.New(ctx)
@@ -133,84 +133,79 @@ func TestAdd(t *testing.T) {
133
t.Error(err)
134
}
135
136
- str := strings.NewReader(helloStr)
137
- p, err := api.Unixfs().Add(ctx, ioutil.NopCloser(str))
138
- if err != nil {
139
- t.Error(err)
140
- }
141
-
142
- if p.String() != hello {
143
- t.Fatalf("expected path %s, got: %s", hello, p)
144
- }
145
-
146
- r, err := api.Unixfs().Cat(ctx, p)
147
- if err != nil {
148
- t.Fatal(err)
149
- }
150
- buf := make([]byte, len(helloStr))
151
- _, err = io.ReadFull(r, buf)
152
- if err != nil {
153
- t.Error(err)
154
- }
155
-
156
- if string(buf) != helloStr {
157
- t.Fatalf("expected [%s], got [%s] [err=%s]", helloStr, string(buf), err)
158
- }
159
-}
160
-
161
-func TestAddEmptyFile(t *testing.T) {
162
- ctx := context.Background()
163
- _, api, err := makeAPI(ctx)
164
- if err != nil {
165
- t.Error(err)
166
- }
167
-
168
- str := strings.NewReader("")
169
- p, err := api.Unixfs().Add(ctx, ioutil.NopCloser(str))
170
- if err != nil {
171
- t.Error(err)
172
- }
173
-
174
- if p.String() != emptyFile {
175
- t.Fatalf("expected path %s, got: %s", hello, p)
176
- }
177
-}
178
-
179
-func TestCatBasic(t *testing.T) {
180
- ctx := context.Background()
181
- node, api, err := makeAPI(ctx)
182
- if err != nil {
183
- t.Fatal(err)
184
- }
185
-
186
- hr := strings.NewReader(helloStr)
187
- p, err := coreunix.Add(node, hr)
188
- if err != nil {
189
- t.Fatal(err)
136
+ cases := []struct {
137
+ name string
138
+ data string
139
+ path string
140
+ err string
141
+ opts []options.UnixfsAddOption
142
+ }{
143
+ {
144
+ name: "simpleAdd",
145
+ data: helloStr,
146
+ path: hello,
147
+ opts: []options.UnixfsAddOption{},
148
+ },
149
+ {
150
+ name: "addEmpty",
151
+ data: "",
152
+ path: "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH",
153
+ },
154
+ {
155
+ name: "addCidV1",
156
+ data: helloStr,
157
+ path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd",
158
+ opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(1)},
159
+ },
160
+ {
161
+ name: "addCidSha3",
162
+ data: helloStr,
163
+ path: "/ipfs/zb2wwnYtXBxpndNABjtYxWAPt3cwWNRnc11iT63fvkYV78iRb",
164
+ opts: []options.UnixfsAddOption{options.Unixfs.Hash(mh.SHA3_256)},
165
+ },
166
+ {
167
+ name: "addCidSha3Cid0",
168
+ data: helloStr,
169
+ err: "CIDv0 only supports sha2-256",
170
+ opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(0), options.Unixfs.Hash(mh.SHA3_256)},
171
+ },
172
}
191
- p = "/ipfs/" + p
173
193
- if p != hello {
194
- t.Fatalf("expected CID %s, got: %s", hello, p)
195
- }
174
+ for _, testCase := range cases {
175
+ t.Run(testCase.name, func(t *testing.T) {
176
+ str := strings.NewReader(testCase.data)
177
+ p, err := api.Unixfs().Add(ctx, ioutil.NopCloser(str), testCase.opts...)
178
+ if testCase.err != "" {
179
+ if err == nil {
180
+ t.Fatalf("expected an error: %s", testCase.err)
181
+ }
182
+ if err.Error() != testCase.err {
183
+ t.Fatalf("expected an error: '%s' != '%s'", err.Error(), testCase.err)
184
+ }
185
+ return
186
+ }
187
+ if err != nil {
188
+ t.Error(err)
189
+ }
190
197
- helloPath, err := coreiface.ParsePath(hello)
198
- if err != nil {
199
- t.Fatal(err)
200
- }
191
+ if p.String() != testCase.path {
192
+ t.Fatalf("expected path %s, got: %s", hello, p)
193
+ }
194
202
- r, err := api.Unixfs().Cat(ctx, helloPath)
203
- if err != nil {
204
- t.Fatal(err)
205
- }
195
+ r, err := api.Unixfs().Cat(ctx, p)
196
+ if err != nil {
197
+ t.Fatal(err)
198
+ }
199
+ buf := make([]byte, len(testCase.data))
200
+ _, err = io.ReadFull(r, buf)
201
+ if err != nil {
202
+ t.Error(err)
203
+ }
204
207
- buf := make([]byte, len(helloStr))
208
- _, err = io.ReadFull(r, buf)
209
- if err != nil {
210
- t.Error(err)
211
- }
212
- if string(buf) != helloStr {
213
- t.Fatalf("expected [%s], got [%s] [err=%s]", helloStr, string(buf), err)
205
+ if string(buf) != testCase.data {
206
+ t.Fatalf("expected [%s], got [%s] [err=%s]", helloStr, string(buf), err)
207
+ }
208
+ })
209
}
210
}
211
@@ -226,7 +221,7 @@ func TestCatEmptyFile(t *testing.T) {
221
t.Fatal(err)
222
}
223
229
- emptyFilePath, err := coreiface.ParsePath(emptyFile)
224
+ emptyFilePath, err := coreiface.ParsePath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH")
225
if err != nil {
226
t.Fatal(err)
227
}