This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
unixfs_test.go
186 lines (156 loc) · 3.33 KB
/
unixfs_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package unixfs
import (
"bytes"
"testing"
proto "github.com/gogo/protobuf/proto"
pb "github.com/ipfs/go-unixfs/pb"
)
func TestFSNode(t *testing.T) {
fsn := NewFSNode(TFile)
for i := 0; i < 16; i++ {
fsn.AddBlockSize(100)
}
fsn.RemoveBlockSize(15)
fsn.SetData(make([]byte, 128))
b, err := fsn.GetBytes()
if err != nil {
t.Fatal(err)
}
pbn := new(pb.Data)
err = proto.Unmarshal(b, pbn)
if err != nil {
t.Fatal(err)
}
ds, err := DataSize(b)
if err != nil {
t.Fatal(err)
}
nKids := fsn.NumChildren()
if nKids != 15 {
t.Fatal("Wrong number of child nodes")
}
if ds != (100*15)+128 {
t.Fatal("Datasize calculations incorrect!")
}
nfsn, err := FSNodeFromBytes(b)
if err != nil {
t.Fatal(err)
}
if nfsn.FileSize() != (100*15)+128 {
t.Fatal("fsNode FileSize calculations incorrect")
}
}
func TestPBdataTools(t *testing.T) {
raw := []byte{0x00, 0x01, 0x02, 0x17, 0xA1}
rawPB := WrapData(raw)
pbDataSize, err := DataSize(rawPB)
if err != nil {
t.Fatal(err)
}
same := len(raw) == int(pbDataSize)
if !same {
t.Fatal("WrapData changes the size of data.")
}
rawPBBytes, err := UnwrapData(rawPB)
if err != nil {
t.Fatal(err)
}
same = bytes.Equal(raw, rawPBBytes)
if !same {
t.Fatal("Unwrap failed to produce the correct wrapped data.")
}
rawPBdata, err := FSNodeFromBytes(rawPB)
if err != nil {
t.Fatal(err)
}
isRaw := rawPBdata.Type() == TRaw
if !isRaw {
t.Fatal("WrapData does not create pb.Data_Raw!")
}
catFile := []byte("Mr_Meowgie.gif")
catPBfile := FilePBData(catFile, 17)
catSize, err := DataSize(catPBfile)
if catSize != 17 {
t.Fatal("FilePBData is the wrong size.")
}
if err != nil {
t.Fatal(err)
}
dirPB := FolderPBData()
dir, err := FSNodeFromBytes(dirPB)
isDir := dir.Type() == TDirectory
if !isDir {
t.Fatal("FolderPBData does not create a directory!")
}
if err != nil {
t.Fatal(err)
}
_, dirErr := DataSize(dirPB)
if dirErr == nil {
t.Fatal("DataSize didn't throw an error when taking the size of a directory.")
}
catSym, err := SymlinkData("/ipfs/adad123123/meowgie.gif")
if err != nil {
t.Fatal(err)
}
catSymPB, err := FSNodeFromBytes(catSym)
isSym := catSymPB.Type() == TSymlink
if !isSym {
t.Fatal("Failed to make a Symlink.")
}
if err != nil {
t.Fatal(err)
}
}
func TestSymlinkFilesize(t *testing.T) {
path := "/ipfs/adad123123/meowgie.gif"
sym, err := SymlinkData(path)
if err != nil {
t.Fatal(err)
}
size, err := DataSize(sym)
if err != nil {
t.Fatal(err)
}
if int(size) != len(path) {
t.Fatalf("size mismatch: %d != %d", size, len(path))
}
}
func TestMetadata(t *testing.T) {
meta := &Metadata{
MimeType: "audio/aiff",
Size: 12345,
}
_, err := meta.Bytes()
if err != nil {
t.Fatal(err)
}
metaPB, err := BytesForMetadata(meta)
if err != nil {
t.Fatal(err)
}
meta, err = MetadataFromBytes(metaPB)
if err != nil {
t.Fatal(err)
}
mimeAiff := meta.MimeType == "audio/aiff"
if !mimeAiff {
t.Fatal("Metadata does not Marshal and Unmarshal properly!")
}
}
func TestIsDir(t *testing.T) {
prepares := map[pb.Data_DataType]bool{
TDirectory: true,
THAMTShard: true,
TFile: false,
TMetadata: false,
TRaw: false,
TSymlink: false,
}
for typ, v := range prepares {
fsn := NewFSNode(typ)
if fsn.IsDir() != v {
t.Fatalf("type %v, IsDir() should be %v, but %v", typ, v, fsn.IsDir())
}
}
}