|
| 1 | +// Copyright 2024 The Sigstore Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package payload |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | +) |
| 20 | + |
| 21 | +func TestCheckSize(t *testing.T) { |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + input uint64 |
| 25 | + setting string |
| 26 | + wantErr bool |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "size is within default limit", |
| 30 | + input: 1000, |
| 31 | + wantErr: false, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "size exceeds default limit", |
| 35 | + input: 200000000, |
| 36 | + wantErr: true, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "size is within overridden limit (bytes)", |
| 40 | + input: 1000, |
| 41 | + setting: "1024", |
| 42 | + wantErr: false, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "size is exceeds overridden limit (bytes)", |
| 46 | + input: 2000, |
| 47 | + setting: "1024", |
| 48 | + wantErr: true, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "size is within overridden limit (megabytes, short form)", |
| 52 | + input: 1999999, |
| 53 | + setting: "2M", |
| 54 | + wantErr: false, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "size exceeds overridden limit (megabytes, short form)", |
| 58 | + input: 2000001, |
| 59 | + setting: "2M", |
| 60 | + wantErr: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "size is within overridden limit (megabytes, long form)", |
| 64 | + input: 1999999, |
| 65 | + setting: "2MB", |
| 66 | + wantErr: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "size exceeds overridden limit (megabytes, long form)", |
| 70 | + input: 2000001, |
| 71 | + setting: "2MB", |
| 72 | + wantErr: true, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "size is within overridden limit (mebibytes)", |
| 76 | + input: 2097151, |
| 77 | + setting: "2MiB", |
| 78 | + wantErr: false, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "size exceeds overridden limit (mebibytes)", |
| 82 | + input: 2097153, |
| 83 | + setting: "2MiB", |
| 84 | + wantErr: true, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "size is negative results in default", |
| 88 | + input: 5121, |
| 89 | + setting: "-5KiB", |
| 90 | + wantErr: false, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "invalid setting results in default", |
| 94 | + input: 5121, |
| 95 | + setting: "five kilobytes", |
| 96 | + wantErr: false, |
| 97 | + }, |
| 98 | + } |
| 99 | + for _, test := range tests { |
| 100 | + t.Run(test.name, func(t *testing.T) { |
| 101 | + if test.setting != "" { |
| 102 | + t.Setenv("COSIGN_MAX_ATTACHMENT_SIZE", test.setting) |
| 103 | + } |
| 104 | + got := CheckSize(test.input) |
| 105 | + if (got != nil) != (test.wantErr) { |
| 106 | + t.Errorf("CheckSize() = %v, expected %v", got, test.wantErr) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments