forked from ipfs/go-ipfs-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell_test.go
118 lines (97 loc) · 3.78 KB
/
shell_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package shell
import (
"bytes"
"crypto/md5"
"fmt"
"io"
"testing"
"github.com/cheekybits/is"
)
const (
examplesHash = "QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe"
shellUrl = "localhost:5001"
)
func TestAdd(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
mhash, err := s.Add(bytes.NewBufferString("Hello IPFS Shell tests"))
is.Nil(err)
is.Equal(mhash, "QmUfZ9rAdhV5ioBzXKdUTh2ZNsz9bzbkaLVyQ8uc8pj21F")
}
func TestCat(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
rc, err := s.Cat(fmt.Sprintf("/ipfs/%s/readme", examplesHash))
is.Nil(err)
md5 := md5.New()
_, err = io.Copy(md5, rc)
is.Nil(err)
is.Equal(fmt.Sprintf("%x", md5.Sum(nil)), "3fdcaad186e79983a6920b4c7eeda949")
}
func TestList(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
list, err := s.List(fmt.Sprintf("/ipfs/%s", examplesHash))
is.Nil(err)
is.Equal(len(list), 6)
// TODO: document difference in size between 'ipfs ls' and 'ipfs file ls -v'. additional object encoding in data block?
expected := map[string]LsLink{
"about": {Type: TFile, Hash: "QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V", Name: "about", Size: 1688},
"contact": {Type: TFile, Hash: "QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y", Name: "contact", Size: 200},
"help": {Type: TFile, Hash: "QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7", Name: "help", Size: 322},
"quick-start": {Type: TFile, Hash: "QmUzLxaXnM8RYCPEqLDX5foToi5aNZHqfYr285w2BKhkft", Name: "quick-start", Size: 1697},
"readme": {Type: TFile, Hash: "QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB", Name: "readme", Size: 1102},
"security-notes": {Type: TFile, Hash: "QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ", Name: "security-notes", Size: 1027},
}
for _, l := range list {
el, ok := expected[l.Name]
is.True(ok)
is.NotNil(el)
is.Equal(*l, el)
}
}
func TestFileList(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
list, err := s.FileList(fmt.Sprintf("/ipfs/%s", examplesHash))
is.Nil(err)
is.Equal(list.Type, "Directory")
is.Equal(list.Size, 0)
is.Equal(len(list.Links), 6)
// TODO: document difference in sice betwen 'ipfs ls' and 'ipfs file ls -v'. additional object encoding in data block?
expected := map[string]UnixLsLink{
"about": {Type: "File", Hash: "QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V", Name: "about", Size: 1677},
"contact": {Type: "File", Hash: "QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y", Name: "contact", Size: 189},
"help": {Type: "File", Hash: "QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7", Name: "help", Size: 311},
"quick-start": {Type: "File", Hash: "QmUzLxaXnM8RYCPEqLDX5foToi5aNZHqfYr285w2BKhkft", Name: "quick-start", Size: 1686},
"readme": {Type: "File", Hash: "QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB", Name: "readme", Size: 1091},
"security-notes": {Type: "File", Hash: "QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ", Name: "security-notes", Size: 1016},
}
for _, l := range list.Links {
el, ok := expected[l.Name]
is.True(ok)
is.NotNil(el)
is.Equal(*l, el)
}
}
func TestPatch_rmLink(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
newRoot, err := s.Patch(examplesHash, "rm-link", "about")
is.Nil(err)
is.Equal(newRoot, "QmNjJ3naRhHCn14E895R1xtGmDgKQb8vnVvQar6RrnraC1")
}
func TestPatchLink(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
newRoot, err := s.PatchLink(examplesHash, "about", "QmUXTtySmd7LD4p6RG6rZW6RuUuPZXTtNMmRQ6DSQo3aMw", true)
is.Nil(err)
is.Equal(newRoot, "QmQwWjFnEPxwmkb5Ukn6UnbrBVebSAYnM11nmMs89e7zH9")
}
func TestResolvePath(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
childHash, err := s.ResolvePath(fmt.Sprintf("/ipfs/%s/about", examplesHash))
is.Nil(err)
is.Equal(childHash, "QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V")
}