1 package storage
2
3 import (
4 "encoding/json"
5 "sort"
6 "testing"
7 )
8
9 func TestEntries(t *testing.T) {
10 e := Entries{
11 Entry{
12 Type: SegmentType,
13 Payload: []byte("y'all"),
14 Position: 1,
15 },
16 Entry{
17 Type: SegmentType,
18 Payload: []byte("doin"),
19 Position: 3,
20 },
21 Entry{
22 Type: FileType,
23 Name: "./hurr.txt",
24 Payload: []byte("deadbeef"),
25 Position: 2,
26 },
27 Entry{
28 Type: SegmentType,
29 Payload: []byte("how"),
30 Position: 0,
31 },
32 }
33 sort.Sort(e)
34 if e[0].Position != 0 {
35 t.Errorf("expected Position 0, but got %d", e[0].Position)
36 }
37 }
38
39 func TestFile(t *testing.T) {
40 f := Entry{
41 Type: FileType,
42 Size: 100,
43 Position: 2,
44 }
45 f.SetName("./hello.txt")
46
47 buf, err := json.Marshal(f)
48 if err != nil {
49 t.Fatal(err)
50 }
51
52 f1 := Entry{}
53 if err = json.Unmarshal(buf, &f1); err != nil {
54 t.Fatal(err)
55 }
56
57 if f.GetName() != f1.GetName() {
58 t.Errorf("expected Name %q, got %q", f.GetName(), f1.GetName())
59 }
60 if f.Size != f1.Size {
61 t.Errorf("expected Size %q, got %q", f.Size, f1.Size)
62 }
63 if f.Position != f1.Position {
64 t.Errorf("expected Position %q, got %q", f.Position, f1.Position)
65 }
66 }
67
68 func TestFileRaw(t *testing.T) {
69 f := Entry{
70 Type: FileType,
71 Size: 100,
72 Position: 2,
73 }
74 f.SetNameBytes([]byte{0x2E, 0x2F, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0xE4, 0x2E, 0x74, 0x78, 0x74})
75
76 buf, err := json.Marshal(f)
77 if err != nil {
78 t.Fatal(err)
79 }
80
81 f1 := Entry{}
82 if err = json.Unmarshal(buf, &f1); err != nil {
83 t.Fatal(err)
84 }
85
86 if f.GetName() != f1.GetName() {
87 t.Errorf("expected Name %q, got %q", f.GetName(), f1.GetName())
88 }
89 if f.Size != f1.Size {
90 t.Errorf("expected Size %q, got %q", f.Size, f1.Size)
91 }
92 if f.Position != f1.Position {
93 t.Errorf("expected Position %q, got %q", f.Position, f1.Position)
94 }
95 }
96
View as plain text