forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt_util_test.go
238 lines (216 loc) · 5.92 KB
/
encrypt_util_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright (c) 2021-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"bufio"
"compress/gzip"
"fmt"
"io"
"math/rand"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"testing"
"time"
)
const timeFormat = "2006-01-02T15:04:05"
type encryptDecryptTestFile struct {
numberOfBytesInEachRow int
numberOfLines int
}
func TestEncryptDecryptFile(t *testing.T) {
encMat := snowflakeFileEncryption{
"ztke8tIdVt1zmlQIZm0BMA==",
"123873c7-3a66-40c4-ab89-e3722fbccce1",
3112,
}
data := "test data"
inputFile := "test_encrypt_decrypt_file"
fd, err := os.Create(inputFile)
if err != nil {
t.Error(err)
}
defer fd.Close()
defer os.Remove(inputFile)
if _, err = fd.Write([]byte(data)); err != nil {
t.Error(err)
}
metadata, encryptedFile, err := encryptFile(&encMat, inputFile, 0, "")
if err != nil {
t.Error(err)
}
defer os.Remove(encryptedFile)
decryptedFile, err := decryptFile(metadata, &encMat, encryptedFile, 0, "")
if err != nil {
t.Error(err)
}
defer os.Remove(decryptedFile)
fd, err = os.Open(decryptedFile)
if err != nil {
t.Error(err)
}
defer fd.Close()
content, err := io.ReadAll(fd)
if err != nil {
t.Error(err)
}
if string(content) != data {
t.Fatalf("data did not match content. expected: %v, got: %v", data, string(content))
}
}
func TestEncryptDecryptFilePadding(t *testing.T) {
encMat := snowflakeFileEncryption{
"ztke8tIdVt1zmlQIZm0BMA==",
"123873c7-3a66-40c4-ab89-e3722fbccce1",
3112,
}
testcases := []encryptDecryptTestFile{
// File size is a multiple of 65536 bytes (chunkSize)
{numberOfBytesInEachRow: 8, numberOfLines: 16384},
{numberOfBytesInEachRow: 16, numberOfLines: 4096},
// File size is not a multiple of 65536 bytes (chunkSize)
{numberOfBytesInEachRow: 8, numberOfLines: 10240},
{numberOfBytesInEachRow: 16, numberOfLines: 6144},
// The second chunk's size is a multiple of 16 bytes (aes.BlockSize)
{numberOfBytesInEachRow: 16, numberOfLines: 4097},
// The second chunk's size is not a multiple of 16 bytes (aes.BlockSize)
{numberOfBytesInEachRow: 12, numberOfLines: 5462},
{numberOfBytesInEachRow: 10, numberOfLines: 6556},
}
for _, test := range testcases {
t.Run(fmt.Sprintf("%v_%v", test.numberOfBytesInEachRow, test.numberOfLines), func(t *testing.T) {
tmpDir, err := generateKLinesOfNByteRows(test.numberOfLines, test.numberOfBytesInEachRow, t.TempDir())
if err != nil {
t.Error(err)
}
encryptDecryptFile(t, encMat, test.numberOfLines, tmpDir)
})
}
}
func TestEncryptDecryptLargeFile(t *testing.T) {
encMat := snowflakeFileEncryption{
"ztke8tIdVt1zmlQIZm0BMA==",
"123873c7-3a66-40c4-ab89-e3722fbccce1",
3112,
}
numberOfFiles := 1
numberOfLines := 10000
tmpDir, err := generateKLinesOfNFiles(numberOfLines, numberOfFiles, false, t.TempDir())
if err != nil {
t.Error(err)
}
encryptDecryptFile(t, encMat, numberOfLines, tmpDir)
}
func encryptDecryptFile(t *testing.T, encMat snowflakeFileEncryption, expected int, tmpDir string) {
files, err := filepath.Glob(filepath.Join(tmpDir, "file*"))
if err != nil {
t.Error(err)
}
inputFile := files[0]
metadata, encryptedFile, err := encryptFile(&encMat, inputFile, 0, tmpDir)
if err != nil {
t.Error(err)
}
defer os.Remove(encryptedFile)
decryptedFile, err := decryptFile(metadata, &encMat, encryptedFile, 0, tmpDir)
if err != nil {
t.Error(err)
}
defer os.Remove(decryptedFile)
cnt := 0
fd, err := os.Open(decryptedFile)
if err != nil {
t.Error(err)
}
defer fd.Close()
scanner := bufio.NewScanner(fd)
for scanner.Scan() {
cnt++
}
if err = scanner.Err(); err != nil {
t.Error(err)
}
if cnt != expected {
t.Fatalf("incorrect number of lines. expected: %v, got: %v", expected, cnt)
}
}
func generateKLinesOfNByteRows(numLines int, numBytes int, tmpDir string) (string, error) {
fname := path.Join(tmpDir, "file"+strconv.FormatInt(int64(numLines*numBytes), 10))
f, err := os.Create(fname)
if err != nil {
return "", err
}
for j := 0; j < numLines; j++ {
str := randomString(numBytes - 1) // \n is the last character
rec := fmt.Sprintf("%v\n", str)
f.Write([]byte(rec))
}
f.Close()
return tmpDir, nil
}
func generateKLinesOfNFiles(k int, n int, compress bool, tmpDir string) (string, error) {
for i := 0; i < n; i++ {
fname := path.Join(tmpDir, "file"+strconv.FormatInt(int64(i), 10))
f, err := os.Create(fname)
if err != nil {
return "", err
}
for j := 0; j < k; j++ {
num := rand.Float64() * 10000
min := time.Date(1970, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
max := time.Date(2070, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
delta := max - min
sec := rand.Int63n(delta) + min
tm := time.Unix(sec, 0)
dt := tm.Format("2021-03-01")
sec = rand.Int63n(delta) + min
ts := time.Unix(sec, 0).Format(timeFormat)
sec = rand.Int63n(delta) + min
tsltz := time.Unix(sec, 0).Format(timeFormat)
sec = rand.Int63n(delta) + min
tsntz := time.Unix(sec, 0).Format(timeFormat)
sec = rand.Int63n(delta) + min
tstz := time.Unix(sec, 0).Format(timeFormat)
pct := rand.Float64() * 1000
ratio := fmt.Sprintf("%.2f", rand.Float64()*1000)
rec := fmt.Sprintf("%v,%v,%v,%v,%v,%v,%v,%v\n", num, dt, ts, tsltz, tsntz, tstz, pct, ratio)
f.Write([]byte(rec))
}
f.Close()
if compress {
if !isWindows {
gzipCmd := exec.Command("gzip", filepath.Join(tmpDir, "file"+strconv.FormatInt(int64(i), 10)))
gzipOut, err := gzipCmd.StdoutPipe()
if err != nil {
return "", err
}
gzipErr, err := gzipCmd.StderrPipe()
if err != nil {
return "", err
}
gzipCmd.Start()
io.ReadAll(gzipOut)
io.ReadAll(gzipErr)
gzipCmd.Wait()
} else {
fOut, err := os.Create(fname + ".gz")
if err != nil {
return "", err
}
w := gzip.NewWriter(fOut)
fIn, err := os.Open(fname)
if err != nil {
return "", err
}
if _, err = io.Copy(w, fIn); err != nil {
return "", err
}
w.Close()
fOut.Close()
fIn.Close()
}
}
}
return tmpDir, nil
}