-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
67 lines (54 loc) · 1.75 KB
/
db_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
package main
import (
"fmt"
"github.com/boltdb/bolt"
"io/ioutil"
"log"
"testing"
)
func TestDbSerialization(t *testing.T) {
filename := "./README.md"
by, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
mdata := marshal(by, Data_File)
mh := multiHash(mdata)
size := uint64(len(by))
l := Link{Name: "hehe", Hash: "haha", Tsize: 32}
n := Node{Encdata: mdata, Hash: mh, Size: size, Links: []*Link{&l}}
by1 := encode(&n)
n1 := decode(by1)
if multiHash(n.Encdata) != multiHash(n1.Encdata) {
t.Fatal("raw data multihash is not the same")
}
if n.Links[0].Name != n1.Links[0].Name {
t.Fatal("link name is not the same")
}
fmt.Printf("%s\n%s\n%v\n%s\n%s\n*********\n", multiHash(n.Encdata), n.Hash, n.Size, n.Links[0].Name, n.Links[0].Hash)
fmt.Printf("%s\n%s\n%v\n%s\n%s\n**********************\n", multiHash(n1.Encdata), n1.Hash, n1.Size, n1.Links[0].Name, n1.Links[0].Hash)
}
func TestDbService(t *testing.T) {
bucketName := "test"
dbName := "./dag.db"
db, err := bolt.Open(dbName, 0644, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
l := Link{Name: "hehe", Hash: "haha", Tsize: 32}
mdata := []byte("this is a test node")
hash := multiHash(mdata)
size := uint64(len(mdata))
n := Node{Encdata: mdata, Hash: hash, Size: size, Links: []*Link{&l}}
FlushNode(&n, db, bucketName)
n1, err := GetNode(hash, db, bucketName)
if err != nil {
log.Fatal(err)
}
if n.Hash != n1.Hash || n.Links[0].Name != n1.Links[0].Name {
log.Fatal("nodes are not equal")
}
fmt.Printf("%s\n%s\n%v\n%s\n%s\n*********\n", multiHash(n.Encdata), n.Hash, n.Size, n.Links[0].Name, n.Links[0].Hash)
fmt.Printf("%s\n%s\n%v\n%s\n%s\n**********************\n", multiHash(n1.Encdata), n1.Hash, n1.Size, n1.Links[0].Name, n1.Links[0].Hash)
}