-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffkey_test.go
180 lines (123 loc) · 3.19 KB
/
offkey_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
package main_test
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"regexp"
"strings"
"testing"
"time"
"github.com/playwright-community/playwright-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestE2E(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
os.MkdirAll(".test", os.ModePerm)
// encrypt with offkey
secret := fmt.Sprintf("this is my very secret message from %d", time.Now().Unix())
log.Printf("encrypting secret %q", secret)
cmd := exec.Command(
"sh",
"-c",
fmt.Sprintf("echo %s | go run offkey.go -o=false", secret),
)
url := make(chan string)
stdout, err := cmd.StdoutPipe()
require.NoError(err)
go func(r io.ReadCloser, url chan string) {
var (
buf = make([]byte, 1024)
matcher = regexp.MustCompile(`(?m)Open "(.+)"`)
)
n, err := r.Read(buf)
require.NoError((err))
matches := matcher.FindStringSubmatch(string(buf[:n]))
assert.Len(matches, 2)
url <- matches[1]
}(stdout, url)
go func() {
require.NoError(cmd.Run())
}()
// navigate to browser
target := <-url
pw, err := playwright.Run()
require.NoError(err)
defer pw.Stop()
browser, err := pw.Chromium.Launch()
require.NoError(err)
defer browser.Close()
page, err := browser.NewPage()
require.NoError(err)
_, err = page.Goto(target)
require.NoError(err)
// extract passphrase and search for the hint
var passphrase string
pp, err := page.QuerySelector("#passphrase")
assert.NoError(err)
assert.True(pp.IsVisible())
text, err := pp.InnerText()
assert.NoError(err)
assert.Regexp("(?:[a-z]+-){9}[a-z]+", text)
passphrase = text
log.Printf("using passphrase %q", passphrase)
pp, err = page.QuerySelector("#passphrase-hint")
assert.NoError(err)
assert.True(pp.IsHidden())
code, err := page.Screenshot()
require.NoError(err)
err = ioutil.WriteFile(".test/secret-with-passphrase.png", code, os.ModePerm)
require.NoError(err)
// print the document, search for the passphrase and extract the hint
page.EmulateMedia(playwright.PageEmulateMediaOptions{
Media: playwright.MediaPrint,
})
pp, err = page.QuerySelector("#passphrase")
assert.NoError(err)
assert.True(pp.IsHidden())
pp, err = page.QuerySelector("#passphrase-hint")
assert.NoError(err)
assert.True(pp.IsVisible())
// extract qr code
code, err = page.Screenshot()
require.NoError(err)
err = ioutil.WriteFile(".test/secret-for-printing.png", code, os.ModePerm)
require.NoError(err)
err = exec.Command(
"sh",
"-c",
"zbarimg --raw -q .test/secret-for-printing.png > .test/secret.age",
).Run()
require.NoError(err)
// decrypt with age
out, err := exec.Command(
"sh",
"-c",
fmt.Sprintf(`expect -c 'spawn age --decrypt .test/secret.age; expect -- "Enter passphrase:*"; send -- "%s\n"; expect eof'`, passphrase),
).CombinedOutput()
assert.NoError(err)
var (
buf = bytes.NewBuffer(out)
last string
)
for {
line, err := buf.ReadString('\n')
if err != nil {
if err == io.EOF {
break
} else {
require.NoError(err)
}
}
last = line
last = strings.TrimSuffix(last, "\n")
last = strings.TrimSuffix(last, "\r")
}
assert.Equal(secret, last)
log.Printf("decrypted secret %q", last)
}