Debian Codegraph @

ipfs/client/client_test.go

raw
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 // This test should run via "make test-ipfs".
18
19 package client
20
21 import (
22 "bytes"
23 "flag"
24 "io"
25 "testing"
26 )
27
28 var ipfsAPI string
29
30 func init() {
31 flag.StringVar(&ipfsAPI, "ipfs-api", "", "Address of IPFS API")
32 }
33
34 func TestIPFSClient(t *testing.T) {
35 if ipfsAPI == "" {
36 t.Log("Specify IPFS API address for IPFS client tests")
37 t.Skip()
38 return
39 }
40 t.Logf("IPFS API address: %q", ipfsAPI)
41 c := New(ipfsAPI)
42 sampleString := "hello world 0123456789"
43 d := bytes.NewReader([]byte(sampleString))
44 cid, err := c.Add(d)
45 if err != nil {
46 t.Errorf("failed to add data to IPFS: %v", err)
47 return
48 }
49 checkData(t, c, cid, 0, len(sampleString), sampleString, len(sampleString))
50 checkData(t, c, cid, 10, 4, sampleString[10:14], len(sampleString))
51 }
52
53 func checkData(t *testing.T, c *Client, cid string, off, len int, wantData string, allSize int) {
54 st, err := c.StatCID(cid)
55 if err != nil {
56 t.Errorf("failed to stat data from IPFS: %v", err)
57 return
58 }
59 if st.Size != uint64(allSize) {
60 t.Errorf("unexpected size got from IPFS %v; wanted %v", st.Size, allSize)
61 return
62 }
63 dGotR, err := c.Get("/ipfs/"+cid, &off, &len)
64 if err != nil {
65 t.Errorf("failed to get data from IPFS: %v", err)
66 return
67 }
68 dGot, err := io.ReadAll(dGotR)
69 if err != nil {
70 t.Errorf("failed to read data from IPFS: %v", err)
71 return
72 }
73 if string(dGot) != wantData {
74 t.Errorf("unexpected data got from IPFS %q; wanted %q", string(dGot), wantData)
75 return
76 }
77 }