-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwsafe_test.go
76 lines (68 loc) · 1.42 KB
/
pwsafe_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
package pwsafe_test
import (
"io/ioutil"
"os"
"testing"
pwsafe "github.com/colinnewell/pwsafe-de-dup"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
)
func TestRoundTrip(t *testing.T) {
zeros := [16]byte{}
blankUUID, err := uuid.FromBytes(zeros[:])
if err != nil {
t.Error(err)
}
pwFile := pwsafe.V3File{
Headers: []pwsafe.HeaderRecord{
pwsafe.HeaderRecord{
Type: pwsafe.UUID,
Data: blankUUID,
},
},
Passwords: []pwsafe.PasswordRecord{
pwsafe.PasswordRecord{
Fields: map[byte]pwsafe.Field{
pwsafe.UUID: pwsafe.Field{
Type: pwsafe.UUID,
Data: uuid.New(),
},
pwsafe.Username: pwsafe.Field{
Type: pwsafe.Username,
Data: "user",
},
pwsafe.Password: pwsafe.Field{
Type: pwsafe.Password,
Data: "test password",
},
},
},
},
}
op, err := ioutil.TempFile("", "psafe3-test")
if err != nil {
t.Error(err)
}
defer os.Remove(op.Name())
password := []byte("test password")
if err := pwFile.Write(op, password); err != nil {
t.Error(err)
}
if err := op.Close(); err != nil {
t.Error(err)
}
// then read back in.
file, err := os.Open(op.Name())
if err != nil {
t.Error(err)
}
defer file.Close()
readFile, err := pwsafe.Load(file, password)
if err != nil {
t.Error(err)
}
// then compare
if diff := cmp.Diff(pwFile, readFile); diff != "" {
t.Errorf("Round trip not identical (-wrote +read):\n%s\n", diff)
}
}