forked from CUIT-CBI/merkle-dag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdag_test.go
161 lines (143 loc) · 3.65 KB
/
dag_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
package merkledag
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
"testing"
)
func TestDagStructure(t *testing.T) {
store := &HashMap{
mp: make(map[string][]byte),
}
hasher := sha256.New()
// 一个小文件的测试
smallFile := &TestFile{
name: "tiny",
data: []byte("这是一个用于测试的小文件"),
}
rootHash := Add(store, smallFile, hasher)
fmt.Printf("%x\n", rootHash)
// 一个大文件的测试
store = &HashMap{
mp: make(map[string][]byte),
}
hasher.Reset()
bigFileContent, err := os.ReadFile("D:\\Information\\作业=-=\\分布式\\merkle-dag\\213_2021131120_陈思州_1.rar")
if err != nil {
t.Error(err)
}
bigFile := &TestFile{
name: "large",
data: bigFileContent,
}
rootHash = Add(store, bigFile, hasher)
fmt.Printf("%x\n", rootHash)
// 一个文件夹的测试
store = &HashMap{
mp: make(map[string][]byte),
}
hasher.Reset()
dirPath := "D:\\Information\\作业=-=\\分布式\\merkle-dag"
entries, _ := ioutil.ReadDir(dirPath)
directory := &TestDir{
list: make([]Node, len(entries)),
name: "Docs",
}
for i, entry := range entries {
entryPath := dirPath + "/" + entry.Name()
if entry.IsDir() {
subDir := explore(entryPath)
subDir.name = entry.Name()
directory.list[i] = subDir
} else {
fileContent, err := os.ReadFile(entryPath)
if err != nil {
t.Fatal(err)
}
entryFile := &TestFile{
name: entry.Name(),
data: fileContent,
}
directory.list[i] = entryFile
}
}
rootHash = Add(store, directory, hasher)
fmt.Printf("%x\n", rootHash)
}
func TestDagToFile(t *testing.T) {
hashMapInstance := &HashMap{
mp: make(map[string][]byte),
}
hashFunc := sha256.New()
// 定义文件夹
folderPath := "D:\\Information\\作业=-=\\分布式\\merkle-dag"
filesInDir, _ := ioutil.ReadDir(folderPath)
directory := &TestDir{
list: make([]Node, len(filesInDir)),
name: "/",
}
for idx, fileItem := range filesInDir {
filePath := folderPath + "/" + fileItem.Name()
if fileItem.IsDir() {
dirContent := explore(filePath)
dirContent.name = fileItem.Name()
directory.list[idx] = dirContent
} else {
fileContent, err := os.ReadFile(filePath)
if err != nil {
t.Fatal(err)
}
fileNode := &TestFile{
name: fileItem.Name(),
data: fileContent,
}
directory.list[idx] = fileNode
}
}
rootHash := Add(hashMapInstance, directory, hashFunc)
fmt.Printf("%x\n", rootHash)
// Retrieve file from the DAG
bufferGo := Hash2File(hashMapInstance, rootHash, "/pkg/mod/bazil.org/[email protected]/buffer.go", nil)
fmt.Println(string(bufferGo))
// Retrieve a new folder from the DAG
newFolderContent := Hash2File(hashMapInstance, rootHash, "newfolder", nil)
originalContent, _ := os.ReadFile("D:\\Information\\作业=-=\\分布式\\merkle-dag\\newfolder")
hashFunc.Reset()
hashFunc.Write(originalContent)
hash1 := hashFunc.Sum(nil)
hashFunc.Reset()
hashFunc.Write(newFolderContent)
hash2 := hashFunc.Sum(nil)
fmt.Println(hash1)
fmt.Println(hash2)
fmt.Println(string(hash1) == string(hash2))
}
func explore(dirPath string) *TestDir {
entries, _ := ioutil.ReadDir(dirPath)
directory := &TestDir{
list: make([]Node, len(entries)),
}
for i, entry := range entries {
entryPath := dirPath + "/" + entry.Name()
if entry.IsDir() {
subDir := explore(entryPath)
subDir.name = entry.Name()
directory.list[i] = subDir
} else {
fileContent, err := os.ReadFile(entryPath)
if err != nil {
subDir := explore(entryPath)
subDir.name = entry.Name()
directory.list[i] = subDir
continue
}
entryFile := &TestFile{
name: entry.Name(),
data: fileContent,
}
directory.list[i] = entryFile
}
}
return directory
}