Debian Codegraph @

clipboard_darwin_test.go

raw symbols SCIP
1 // Copyright 2013 @atotto. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build darwin
6 // +build darwin
7
8 package clipboard
9
10 import (
11 "testing"
12 )
13
14 // TestCopyViaOsascriptSuccess verifies that copyViaOsascript writes the
15 // expected value to the system clipboard and returns no error.
16 func TestCopyViaOsascriptSuccess(t *testing.T) {
17 if testing.Short() {
18 t.Skip("skipping osascript integration test in short mode")
19 }
20
21 ctx := t.Context()
22 const want = "s3cr3t-pass"
23
24 if err := copyViaOsascript(ctx, want); err != nil {
25 t.Fatalf("copyViaOsascript returned error: %v", err)
26 }
27
28 got, err := readAll(ctx)
29 if err != nil {
30 t.Fatalf("readAll failed: %v", err)
31 }
32 if string(got) != want {
33 t.Errorf("clipboard = %q, want %q", got, want)
34 }
35 }
36
37 // TestCopyViaOsascriptSpecialChars verifies that passwords containing
38 // characters that are special in AppleScript string literals (quotes,
39 // ampersands, angle brackets) survive the base64 round-trip correctly.
40 func TestCopyViaOsascriptSpecialChars(t *testing.T) {
41 if testing.Short() {
42 t.Skip("skipping osascript integration test in short mode")
43 }
44
45 ctx := t.Context()
46 const want = `p@$$w0rd "quoted" & <tags>`
47
48 if err := copyViaOsascript(ctx, want); err != nil {
49 t.Fatalf("copyViaOsascript returned error: %v", err)
50 }
51
52 got, err := readAll(ctx)
53 if err != nil {
54 t.Fatalf("readAll failed: %v", err)
55 }
56 if string(got) != want {
57 t.Errorf("clipboard = %q, want %q", got, want)
58 }
59 }
60
61 // TestWriteAllSecretFallback verifies that when osascript is unavailable,
62 // writeAll quietly falls back to pbcopy and still places the text on the
63 // clipboard without returning an error.
64 func TestWriteAllSecretFallback(t *testing.T) {
65 if testing.Short() {
66 t.Skip("skipping osascript integration test in short mode")
67 }
68
69 // Point osascriptPath at a nonexistent binary so that copyViaOsascript
70 // always fails and the pbcopy fallback path inside writeAll is exercised.
71 orig := osascriptPath
72 osascriptPath = "/nonexistent-osascript-binary"
73 t.Cleanup(func() { osascriptPath = orig })
74
75 ctx := t.Context()
76 const want = "fallback-test-pass"
77
78 if err := writeAll(ctx, []byte(want), true); err != nil {
79 t.Fatalf("writeAll (secret, fallback) returned error: %v", err)
80 }
81
82 got, err := readAll(ctx)
83 if err != nil {
84 t.Fatalf("readAll failed: %v", err)
85 }
86 if string(got) != want {
87 t.Errorf("clipboard = %q, want %q", got, want)
88 }
89 }