-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmigrate_test.go
91 lines (87 loc) · 2.06 KB
/
migrate_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
package main
import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"testing"
"github.com/rusq/wipemychat/internal/session"
)
func copyfile(t *testing.T, src, dst string) error {
t.Helper()
sf, err := os.Open(src)
if err != nil {
t.Fatalf("source: %s", err)
}
defer sf.Close()
df, err := os.Create(dst)
if err != nil {
t.Fatalf("destination: %s", err)
}
defer df.Close()
if _, err := io.Copy(df, sf); err != nil {
t.Fatalf("copy: %s", err)
}
return nil
}
func Test_migratev120(t *testing.T) {
type args struct {
sessfile string
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{
name: "migrates v1 file",
args: args{sessfile: "testdata/testsessionv100.dat"},
want: true,
},
{
name: "doesn't touch v1.20 file",
args: args{sessfile: "testdata/testsessionv120.dat"},
want: false,
},
{
name: "invalid file",
args: args{sessfile: "testdata/invalidsession.dat"},
want: false,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// substitute the source file with it's copy in a temporary directory
tmpdir := t.TempDir()
srcfilename := filepath.Base(tt.args.sessfile)
dstfile := filepath.Join(tmpdir, srcfilename)
copyfile(t, tt.args.sessfile, filepath.Join(tmpdir, srcfilename))
migrated, err := migratev120(dstfile)
if (err != nil) != tt.wantErr {
t.Errorf("migratev120() error = %v, wantErr %v", err, tt.wantErr)
return
}
if migrated != tt.want {
t.Errorf("migratev120() = %v, want %v", migrated, tt.want)
}
if migrated {
// verify the file is valid v1.20
sess := session.FileStorage{Path: dstfile}
data, err := sess.LoadSession(context.Background())
if err != nil {
t.Errorf("migratev120() = %v, want %v", err, nil)
}
if data == nil {
t.Errorf("migratev120() = %v, want %v", data, "not nil")
}
sigSz := len(v1signature)
if !bytes.Equal(data[:sigSz], []byte(v1signature)) {
t.Errorf("migratev120() = %v, want %v", data[:sigSz], v1signature)
}
}
})
}
}