-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathcliphist_test.go
More file actions
191 lines (167 loc) · 4.66 KB
/
cliphist_test.go
File metadata and controls
191 lines (167 loc) · 4.66 KB
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
package main
import (
"bytes"
"crypto/rand"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
mathrand "math/rand"
"os"
"path/filepath"
"strconv"
"testing"
"github.com/rogpeppe/go-internal/testscript"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
)
func TestMain(m *testing.M) {
testImage := image.NewRGBA(image.Rectangle{Max: image.Point{20, 20}})
os.Exit(testscript.RunMain(m, map[string]func() int{
"cliphist": func() int { main(); return 0 },
"rand": func() int {
size, _ := strconv.Atoi(os.Args[1])
_, _ = io.CopyN(os.Stdout, rand.Reader, int64(size))
return 0
},
"randstr": func() int {
size, _ := strconv.Atoi(os.Args[1])
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, size)
for i := range b {
b[i] = letterBytes[mathrand.Intn(len(letterBytes))]
}
os.Stdout.Write(b)
return 0
},
"gif": func() int { _ = gif.Encode(os.Stdout, testImage, nil); return 0 },
"jpg": func() int { _ = jpeg.Encode(os.Stdout, testImage, nil); return 0 },
"png": func() int { _ = png.Encode(os.Stdout, testImage); return 0 },
"bmp": func() int { _ = bmp.Encode(os.Stdout, testImage); return 0 },
"tiff": func() int { _ = tiff.Encode(os.Stdout, testImage, nil); return 0 },
}))
}
func TestStoreMaxSize(t *testing.T) {
tests := []struct {
name string
inputSize int
maxStoreSize uint64
shouldStore bool
}{
// Under limit: should store
{"under limit", 100, 1000, true},
{"exactly at limit", 1000, 1000, true},
// Over limit: should not store
{"over limit by 1", 1001, 1000, false},
{"way over limit", 5000, 1000, false},
// maxStoreSize 0 = no limit
{"no limit small", 100, 0, true},
{"no limit large", 100000, 0, true},
// Test with realistic sizes
{"5MB limit under", 4 * 1024 * 1024, 5 * 1000 * 1000, true},
{"5MB limit over", 6 * 1024 * 1024, 5 * 1000 * 1000, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create temporary db
tmpDir, err := os.MkdirTemp("", "cliphist-test-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer func() { _ = os.RemoveAll(tmpDir) }()
dbPath := filepath.Join(tmpDir, "db")
// Generate input of specified size
input := make([]byte, tt.inputSize)
for i := range input {
input[i] = 'a'
}
// Execute store
err = store(dbPath, bytes.NewReader(input), 100, 750, 0, tt.maxStoreSize)
if err != nil {
t.Fatalf("store failed: %v", err)
}
// Verify if it was stored
var output bytes.Buffer
err = list(dbPath, &output, 100)
if tt.shouldStore {
if err != nil {
t.Fatalf("list failed: %v", err)
}
if output.Len() == 0 {
t.Errorf("expected item to be stored, but list is empty")
}
} else {
// If it shouldn't store, list might fail (db doesn't exist) or return empty
if err == nil && output.Len() > 0 {
t.Errorf("expected item NOT to be stored, but list returned: %s", output.String())
}
}
})
}
}
func TestParseSize(t *testing.T) {
tests := []struct {
input string
expected uint64
wantErr bool
}{
// Bytes without unit
{"0", 0, false},
{"1024", 1024, false},
{"5000000", 5000000, false},
// Decimal units (base 1000)
{"1KB", 1000, false},
{"5MB", 5 * 1000 * 1000, false},
{"1GB", 1000 * 1000 * 1000, false},
{"2.5MB", 2500000, false},
// Binary units (base 1024)
{"1KiB", 1024, false},
{"5MiB", 5 * 1024 * 1024, false},
{"1GiB", 1024 * 1024 * 1024, false},
{"2.5MiB", uint64(2.5 * 1024 * 1024), false},
// Case insensitive
{"5mb", 5 * 1000 * 1000, false},
{"5Mb", 5 * 1000 * 1000, false},
{"5mib", 5 * 1024 * 1024, false},
{"5MIB", 5 * 1024 * 1024, false},
// With spaces
{" 5MB ", 5 * 1000 * 1000, false},
{"5 MB", 5 * 1000 * 1000, false},
// Errors
{"", 0, true},
{"abc", 0, true},
{"MB", 0, true},
{"-5MB", 0, true},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result, err := parseSize(tt.input)
if tt.wantErr {
if err == nil {
t.Errorf("parseSize(%q) expected error, got %d", tt.input, result)
}
return
}
if err != nil {
t.Errorf("parseSize(%q) unexpected error: %v", tt.input, err)
return
}
if result != tt.expected {
t.Errorf("parseSize(%q) = %d, want %d", tt.input, result, tt.expected)
}
})
}
}
func TestScripts(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata",
RequireExplicitExec: true,
Setup: func(env *testscript.Env) error {
env.Vars = append(env.Vars, fmt.Sprintf("HOME=%s", env.WorkDir))
env.Vars = append(env.Vars, fmt.Sprintf("XDG_CACHE_HOME=%s", env.WorkDir))
return nil
},
})
}