-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcopy_paste_test.go
184 lines (148 loc) · 5.1 KB
/
copy_paste_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
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"heckel.io/pcopy/clipboard/clipboardtest"
"heckel.io/pcopy/config/configtest"
"heckel.io/pcopy/test"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
)
func TestCLI_Copy(t *testing.T) {
filename, config := configtest.NewTestConfig(t)
serverRouter := startTestServerRouter(t, config)
defer serverRouter.Stop()
test.WaitForPortUp(t, "12345")
app, stdin, _, stderr := newTestApp()
stdin.WriteString("test stdin")
if err := Run(app, "pcp", "-c", filename); err != nil {
t.Fatal(err)
}
clipboardtest.Content(t, config, "default", "test stdin")
test.StrContains(t, stderr.String(), "Direct link (valid for 7d")
test.StrContains(t, stderr.String(), "curl -sSLk --pinnedpubkey")
test.StrContains(t, stderr.String(), "https://localhost:12345/default")
}
func TestCLI_CopyPaste(t *testing.T) {
filename, config := configtest.NewTestConfig(t)
serverRouter := startTestServerRouter(t, config)
defer serverRouter.Stop()
test.WaitForPortUp(t, "12345")
copyApp, copyStdin, _, copyStderr := newTestApp()
copyStdin.WriteString("this is a test string")
if err := Run(copyApp, "pcp", "-c", filename, "somefile"); err != nil {
t.Fatal(err)
}
pasteApp, _, pasteStdout, _ := newTestApp()
if err := Run(pasteApp, "ppaste", "-c", filename, "somefile"); err != nil {
t.Fatal(err)
}
test.StrContains(t, copyStderr.String(), "https://localhost:12345/somefile")
test.StrContains(t, pasteStdout.String(), "this is a test string")
}
func TestCLI_CopyPasteStream(t *testing.T) {
filename, config := configtest.NewTestConfig(t)
serverRouter := startTestServerRouter(t, config)
defer serverRouter.Stop()
test.WaitForPortUp(t, "12345")
// Copy
copyApp, copyStdin, _, copyStderr := newTestApp()
copyErrChan := make(chan error)
go func() {
copyStdin.WriteString("this is a test string\n")
if err := Run(copyApp, "pcp", "--stream", "-c", filename, "mystream"); err != nil {
copyErrChan <- err
return
}
test.StrContains(t, copyStderr.String(), "https://localhost:12345/mystream")
}()
// Wait for pipe to be created
success := false
for i := 0; i < 20; i++ {
stat, _ := os.Stat(filepath.Join(config.ClipboardDir, "mystream"))
if stat != nil && stat.Mode()&os.ModeNamedPipe == os.ModeNamedPipe {
success = true
break
}
time.Sleep(20 * time.Millisecond)
}
if !success {
t.Fatalf("waiting for pipe timed out")
}
// Awkwardly check for copy error, since we cannot call t.Fatal in a goroutine
select {
case err := <-copyErrChan:
t.Fatal(err)
default:
}
// Paste
pasteApp, _, pasteStdout, _ := newTestApp()
if err := Run(pasteApp, "ppaste", "-c", filename, "mystream"); err != nil {
t.Fatal(err)
}
test.StrContains(t, pasteStdout.String(), "this is a test string")
}
func TestCurl_CopyPOSTSuccess(t *testing.T) {
_, config := configtest.NewTestConfig(t)
serverRouter := startTestServerRouter(t, config)
defer serverRouter.Stop()
test.WaitForPortUp(t, "12345")
var stdout bytes.Buffer
cmd := exec.Command("curl", "-sSLk", "-dabc", fmt.Sprintf("%s/howdy?f=json", config.ServerAddr))
cmd.Stdout = &stdout
cmd.Run()
clipboardtest.Content(t, config, "howdy", "abc")
test.StrContains(t, stdout.String(), `"url":"https://localhost:12345/howdy"`) // json
}
func TestCurl_POSTGETRandomWithJsonFormat(t *testing.T) {
_, config := configtest.NewTestConfig(t)
serverRouter := startTestServerRouter(t, config)
defer serverRouter.Stop()
test.WaitForPortUp(t, "12345")
var stdout bytes.Buffer
cmdCurlPOST := exec.Command("curl", "-sSLk", "-dabc", fmt.Sprintf("%s?f=json", config.ServerAddr))
cmdCurlPOST.Stdout = &stdout
cmdCurlPOST.Run()
var info map[string]interface{}
json.Unmarshal(stdout.Bytes(), &info)
stdout.Reset()
cmdCurlGET := exec.Command("sh", "-c", info["curl"].(string))
cmdCurlGET.Stdout = &stdout
cmdCurlGET.Run()
test.StrEquals(t, stdout.String(), "abc")
}
func TestCurl_POSTGETRandomStreamWithJsonFormat(t *testing.T) {
// This tests #46: curl POST with streaming and short payloads does not work (curl -dabc http://...?s=1)
_, config := configtest.NewTestConfig(t)
serverRouter := startTestServerRouter(t, config)
defer serverRouter.Stop()
test.WaitForPortUp(t, "12345")
// Streaming enabled (s=1), note that "stdbuf -oL" is required to flush buffers after every line
cmdCurlPOST := exec.Command("stdbuf", "-oL", "curl", "-sSLk", "-dabc", fmt.Sprintf("%s?s=1&f=json", config.ServerAddr))
stdoutPipe, _ := cmdCurlPOST.StdoutPipe()
cmdCurlPOST.Start()
out := test.WaitForOutput(t, stdoutPipe, 1*time.Second, 100*time.Millisecond)
var info map[string]interface{}
json.Unmarshal([]byte(out), &info)
fileID := info["file"].(string)
curlGET := info["curl"].(string)
file := filepath.Join(config.ClipboardDir, fileID)
stat, _ := os.Stat(file)
if stat.Mode()&os.ModeNamedPipe == 0 {
t.Fatalf("expected %s to be a pipe, but it's not", file)
}
// Now GET it
var stdout bytes.Buffer
cmdCurlGET := exec.Command("sh", "-c", curlGET)
cmdCurlGET.Stdout = &stdout
cmdCurlGET.Run()
test.StrEquals(t, stdout.String(), "abc")
stat, _ = os.Stat(file)
if stat != nil {
t.Fatalf("expected %s to not exist anymore, but it does", file)
}
}