File: post-policy_test.go

package info (click to toggle)
golang-github-minio-minio-go-v7 7.0.84-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 2,456 kB
  • sloc: makefile: 38; xml: 2
file content (459 lines) | stat: -rw-r--r-- 10,834 bytes parent folder | download
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * MinIO Go Library for Amazon S3 Compatible Cloud Storage
 * Copyright 2015-2023 MinIO, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package minio

import (
	"strings"
	"testing"
	"time"

	"github.com/minio/minio-go/v7/pkg/encrypt"
)

func TestPostPolicySetExpires(t *testing.T) {
	tests := []struct {
		name       string
		input      time.Time
		wantErr    bool
		wantResult string
	}{
		{
			name:       "valid time",
			input:      time.Date(2023, time.March, 2, 15, 4, 5, 0, time.UTC),
			wantErr:    false,
			wantResult: "2023-03-02T15:04:05",
		},
		{
			name:    "time before 1970",
			input:   time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC),
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pp := NewPostPolicy()

			err := pp.SetExpires(tt.input)
			if (err != nil) != tt.wantErr {
				t.Errorf("%s: want error: %v, got: %v", tt.name, tt.wantErr, err)
			}

			if tt.wantResult != "" {
				result := pp.String()
				if !strings.Contains(result, tt.wantResult) {
					t.Errorf("%s: want result to contain: '%s', got: '%s'", tt.name, tt.wantResult, result)
				}
			}
		})
	}
}

func TestPostPolicySetKey(t *testing.T) {
	tests := []struct {
		name       string
		input      string
		wantErr    bool
		wantResult string
	}{
		{
			name:       "valid key",
			input:      "my-object",
			wantResult: `"eq","$key","my-object"`,
		},
		{
			name:    "empty key",
			input:   "",
			wantErr: true,
		},
		{
			name:    "key with spaces",
			input:   "  ",
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pp := NewPostPolicy()

			err := pp.SetKey(tt.input)
			if (err != nil) != tt.wantErr {
				t.Errorf("%s: want error: %v, got: %v", tt.name, tt.wantErr, err)
			}

			if tt.wantResult != "" {
				result := pp.String()
				if !strings.Contains(result, tt.wantResult) {
					t.Errorf("%s: want result to contain: '%s', got: '%s'", tt.name, tt.wantResult, result)
				}
			}
		})
	}
}

func TestPostPolicySetKeyStartsWith(t *testing.T) {
	tests := []struct {
		name  string
		input string
		want  string
	}{
		{
			name:  "valid key prefix",
			input: "my-prefix/",
			want:  `["starts-with","$key","my-prefix/"]`,
		},
		{
			name:  "empty prefix (allow any key)",
			input: "",
			want:  `["starts-with","$key",""]`,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pp := NewPostPolicy()

			err := pp.SetKeyStartsWith(tt.input)
			if err != nil {
				t.Errorf("%s: want no error, got: %v", tt.name, err)
			}

			if tt.want != "" {
				result := pp.String()
				if !strings.Contains(result, tt.want) {
					t.Errorf("%s: want result to contain: '%s', got: '%s'", tt.name, tt.want, result)
				}
			}
		})
	}
}

func TestPostPolicySetBucket(t *testing.T) {
	tests := []struct {
		name       string
		input      string
		wantErr    bool
		wantResult string
	}{
		{
			name:       "valid bucket",
			input:      "my-bucket",
			wantResult: `"eq","$bucket","my-bucket"`,
		},
		{
			name:    "empty bucket",
			input:   "",
			wantErr: true,
		},
		{
			name:    "bucket with spaces",
			input:   "   ",
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pp := NewPostPolicy()

			err := pp.SetBucket(tt.input)
			if (err != nil) != tt.wantErr {
				t.Errorf("%s: want error: %v, got: %v", tt.name, tt.wantErr, err)
			}

			if tt.wantResult != "" {
				result := pp.String()
				if !strings.Contains(result, tt.wantResult) {
					t.Errorf("%s: want result to contain: '%s', got: '%s'", tt.name, tt.wantResult, result)
				}
			}
		})
	}
}

func TestPostPolicySetCondition(t *testing.T) {
	tests := []struct {
		name       string
		matchType  string
		condition  string
		value      string
		wantErr    bool
		wantResult string
	}{
		{
			name:       "valid eq condition",
			matchType:  "eq",
			condition:  "X-Amz-Date",
			value:      "20210324T000000Z",
			wantResult: `"eq","$X-Amz-Date","20210324T000000Z"`,
		},
		{
			name:      "empty value",
			matchType: "eq",
			condition: "X-Amz-Date",
			value:     "",
			wantErr:   true,
		},
		{
			name:      "invalid condition",
			matchType: "eq",
			condition: "Invalid-Condition",
			value:     "somevalue",
			wantErr:   true,
		},
		{
			name:       "valid starts-with condition",
			matchType:  "starts-with",
			condition:  "X-Amz-Credential",
			value:      "my-access-key",
			wantResult: `"starts-with","$X-Amz-Credential","my-access-key"`,
		},
		{
			name:      "empty condition",
			matchType: "eq",
			condition: "",
			value:     "somevalue",
			wantErr:   true,
		},
		{
			name:      "empty matchType",
			matchType: "",
			condition: "X-Amz-Date",
			value:     "somevalue",
			wantErr:   true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pp := NewPostPolicy()

			err := pp.SetCondition(tt.matchType, tt.condition, tt.value)
			if (err != nil) != tt.wantErr {
				t.Errorf("%s: want error: %v, got: %v", tt.name, tt.wantErr, err)
			}

			if tt.wantResult != "" {
				result := pp.String()
				if !strings.Contains(result, tt.wantResult) {
					t.Errorf("%s: want result to contain: '%s', got: '%s'", tt.name, tt.wantResult, result)
				}
			}
		})
	}
}

func TestPostPolicySetTagging(t *testing.T) {
	tests := []struct {
		name       string
		tagging    string
		wantErr    bool
		wantResult string
	}{
		{
			name:       "valid tagging",
			tagging:    `<Tagging><TagSet><Tag><Key>key1</Key><Value>value1</Value></Tag></TagSet></Tagging>`,
			wantResult: `"eq","$tagging","<Tagging><TagSet><Tag><Key>key1</Key><Value>value1</Value></Tag></TagSet></Tagging>"`,
		},
		{
			name:    "empty tagging",
			tagging: "",
			wantErr: true,
		},
		{
			name:    "whitespace tagging",
			tagging: "   ",
			wantErr: true,
		},
		{
			name:    "invalid XML",
			tagging: `<Tagging><TagSet><Tag><Key>key1</Key><Value>value1</Value></Tag></TagSet>`,
			wantErr: true,
		},
		{
			name:    "invalid schema",
			tagging: `<InvalidTagging></InvalidTagging>`,
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pp := NewPostPolicy()

			err := pp.SetTagging(tt.tagging)
			if (err != nil) != tt.wantErr {
				t.Errorf("%s: want error: %v, got: %v", tt.name, tt.wantErr, err)
			}

			if tt.wantResult != "" {
				result := pp.String()
				if !strings.Contains(result, tt.wantResult) {
					t.Errorf("%s: want result to contain: '%s', got: '%s'", tt.name, tt.wantResult, result)
				}
			}
		})
	}
}

func TestPostPolicySetUserMetadata(t *testing.T) {
	tests := []struct {
		name       string
		key        string
		value      string
		wantErr    bool
		wantResult string
	}{
		{
			name:       "valid metadata",
			key:        "user-key",
			value:      "user-value",
			wantResult: `"eq","$x-amz-meta-user-key","user-value"`,
		},
		{
			name:    "empty key",
			key:     "",
			value:   "somevalue",
			wantErr: true,
		},
		{
			name:    "empty value",
			key:     "user-key",
			value:   "",
			wantErr: true,
		},
		{
			name:    "key with spaces",
			key:     "   ",
			value:   "somevalue",
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pp := NewPostPolicy()

			err := pp.SetUserMetadata(tt.key, tt.value)
			if (err != nil) != tt.wantErr {
				t.Errorf("%s: want error: %v, got: %v", tt.name, tt.wantErr, err)
			}

			if tt.wantResult != "" {
				result := pp.String()
				if !strings.Contains(result, tt.wantResult) {
					t.Errorf("%s: want result to contain: '%s', got: '%s'", tt.name, tt.wantResult, result)
				}
			}
		})
	}
}

func TestPostPolicySetChecksum(t *testing.T) {
	tests := []struct {
		name       string
		checksum   Checksum
		wantErr    bool
		wantResult string
	}{
		{
			name:       "valid checksum SHA256",
			checksum:   ChecksumSHA256.ChecksumBytes([]byte("somerandomdata")),
			wantResult: `[["eq","$x-amz-checksum-algorithm","SHA256"],["eq","$x-amz-checksum-sha256","29/7Qm/iMzZ1O3zMbO0luv6mYWyS6JIqPYV9lc8w1PA="]]`,
		},
		{
			name:       "valid checksum CRC32",
			checksum:   ChecksumCRC32.ChecksumBytes([]byte("somerandomdata")),
			wantResult: `[["eq","$x-amz-checksum-algorithm","CRC32"],["eq","$x-amz-checksum-crc32","7sOPnw=="]]`,
		},
		{
			name:       "empty checksum",
			checksum:   Checksum{},
			wantResult: "",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pp := NewPostPolicy()

			err := pp.SetChecksum(tt.checksum)
			if (err != nil) != tt.wantErr {
				t.Errorf("%s: want error: %v, got: %v", tt.name, tt.wantErr, err)
			}

			if tt.wantResult != "" {
				result := pp.String()
				if !strings.Contains(result, tt.wantResult) {
					t.Errorf("%s: want result to contain: '%s', got: '%s'", tt.name, tt.wantResult, result)
				}
			}
		})
	}
}

func TestPostPolicySetEncryption(t *testing.T) {
	tests := []struct {
		name    string
		sseType string
		keyID   string
		want    map[string]string
	}{
		{
			name:    "SSE-S3 encryption",
			sseType: "SSE-S3",
			keyID:   "my-key-id",
			want: map[string]string{
				"X-Amz-Server-Side-Encryption":                "aws:kms",
				"X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id": "my-key-id",
			},
		},
		{
			name:    "SSE-C encryption with Key ID",
			sseType: "SSE-C",
			keyID:   "my-key-id",
			want: map[string]string{
				"X-Amz-Server-Side-Encryption-Customer-Key":       "bXktc2VjcmV0LWtleTEyMzQ1Njc4OTBhYmNkZWZnaGk=",
				"X-Amz-Server-Side-Encryption-Customer-Key-Md5":   "T1mefJwyXBH43sRtfEgRZQ==",
				"X-Amz-Server-Side-Encryption-Customer-Algorithm": "AES256",
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pp := NewPostPolicy()

			var sse encrypt.ServerSide
			var err error
			if tt.sseType == "SSE-S3" {
				sse, err = encrypt.NewSSEKMS(tt.keyID, nil)
				if err != nil {
					t.Fatalf("Failed to create SSE-KMS: %v", err)
				}
			} else if tt.sseType == "SSE-C" {
				sse, err = encrypt.NewSSEC([]byte("my-secret-key1234567890abcdefghi"))
				if err != nil {
					t.Fatalf("Failed to create SSE-C: %v", err)
				}
			} else {
				t.Fatalf("Unknown SSE type: %s", tt.sseType)
			}

			pp.SetEncryption(sse)

			for k, v := range tt.want {
				if pp.formData[k] != v {
					t.Errorf("%s: want %s: %s, got: %s", tt.name, k, v, pp.formData[k])
				}
			}
		})
	}
}