File: form_test.go

package info (click to toggle)
docker.io 28.5.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 69,048 kB
  • sloc: sh: 5,867; makefile: 863; ansic: 184; python: 162; asm: 159
file content (235 lines) | stat: -rw-r--r-- 5,586 bytes parent folder | download | duplicates (2)
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
package httputils

import (
	"math"
	"net/http"
	"net/url"
	"strconv"
	"testing"

	cerrdefs "github.com/containerd/errdefs"
	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func TestBoolValue(t *testing.T) {
	cases := map[string]bool{
		"":      false,
		"0":     false,
		"no":    false,
		"false": false,
		"none":  false,
		"1":     true,
		"yes":   true,
		"true":  true,
		"one":   true,
		"100":   true,
	}

	for c, e := range cases {
		v := url.Values{}
		v.Set("test", c)
		r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
		r.Form = v

		a := BoolValue(r, "test")
		if a != e {
			t.Fatalf("Value: %s, expected: %v, actual: %v", c, e, a)
		}
	}
}

func TestBoolValueOrDefault(t *testing.T) {
	r, _ := http.NewRequest(http.MethodGet, "", http.NoBody)
	if !BoolValueOrDefault(r, "queryparam", true) {
		t.Fatal("Expected to get true default value, got false")
	}

	v := url.Values{}
	v.Set("param", "")
	r, _ = http.NewRequest(http.MethodGet, "", http.NoBody)
	r.Form = v
	if BoolValueOrDefault(r, "param", true) {
		t.Fatal("Expected not to get true")
	}
}

func TestInt64ValueOrZero(t *testing.T) {
	cases := map[string]int64{
		"":     0,
		"asdf": 0,
		"0":    0,
		"1":    1,
	}

	for c, e := range cases {
		v := url.Values{}
		v.Set("test", c)
		r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
		r.Form = v

		a := Int64ValueOrZero(r, "test")
		if a != e {
			t.Fatalf("Value: %s, expected: %v, actual: %v", c, e, a)
		}
	}
}

func TestInt64ValueOrDefault(t *testing.T) {
	cases := map[string]int64{
		"":   -1,
		"-1": -1,
		"42": 42,
	}

	for c, e := range cases {
		v := url.Values{}
		v.Set("test", c)
		r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
		r.Form = v

		a, err := Int64ValueOrDefault(r, "test", -1)
		if a != e {
			t.Fatalf("Value: %s, expected: %v, actual: %v", c, e, a)
		}
		if err != nil {
			t.Fatalf("Error should be nil, but received: %s", err)
		}
	}
}

func TestInt64ValueOrDefaultWithError(t *testing.T) {
	v := url.Values{}
	v.Set("test", "invalid")
	r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
	r.Form = v

	_, err := Int64ValueOrDefault(r, "test", -1)
	if err == nil {
		t.Fatal("Expected an error.")
	}
}

func TestUint32Value(t *testing.T) {
	const valueNotSet = "unset"

	tests := []struct {
		value       string
		expected    uint32
		expectedErr error
	}{
		{
			value:    "0",
			expected: 0,
		},
		{
			value:    strconv.FormatUint(math.MaxUint32, 10),
			expected: math.MaxUint32,
		},
		{
			value:       valueNotSet,
			expectedErr: strconv.ErrSyntax,
		},
		{
			value:       "",
			expectedErr: strconv.ErrSyntax,
		},
		{
			value:       "-1",
			expectedErr: strconv.ErrRange,
		},
		{
			value:       "4294967296", // MaxUint32+1
			expectedErr: strconv.ErrRange,
		},
		{
			value:       "not-a-number",
			expectedErr: strconv.ErrSyntax,
		},
	}
	for _, tc := range tests {
		t.Run(tc.value, func(t *testing.T) {
			r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
			r.Form = url.Values{}
			if tc.value != valueNotSet {
				r.Form.Set("field", tc.value)
			}
			out, err := Uint32Value(r, "field")
			assert.Check(t, is.Equal(tc.expected, out))
			assert.Check(t, is.ErrorIs(err, tc.expectedErr))
		})
	}
}

func TestDecodePlatform(t *testing.T) {
	tests := []struct {
		doc          string
		platformJSON string
		expected     *ocispec.Platform
		expectedErr  string
	}{
		{
			doc:         "empty platform",
			expectedErr: `failed to parse platform: unexpected end of JSON input`,
		},
		{
			doc:          "not JSON",
			platformJSON: `linux/ams64`,
			expectedErr:  `failed to parse platform: invalid character 'l' looking for beginning of value`,
		},
		{
			doc:          "malformed JSON",
			platformJSON: `{"architecture"`,
			expectedErr:  `failed to parse platform: unexpected end of JSON input`,
		},
		{
			doc:          "missing os",
			platformJSON: `{"architecture":"amd64","os":""}`,
			expectedErr:  `both OS and Architecture must be provided`,
		},
		{
			doc:          "variant without architecture",
			platformJSON: `{"architecture":"","os":"","variant":"v7"}`,
			expectedErr:  `optional platform fields provided, but OS and Architecture are missing`,
		},
		{
			doc:          "missing architecture",
			platformJSON: `{"architecture":"","os":"linux"}`,
			expectedErr:  `both OS and Architecture must be provided`,
		},
		{
			doc:          "os.version without os and architecture",
			platformJSON: `{"architecture":"","os":"","os.version":"12.0"}`,
			expectedErr:  `optional platform fields provided, but OS and Architecture are missing`,
		},
		{
			doc:          "os.features without os and architecture",
			platformJSON: `{"architecture":"","os":"","os.features":["a","b"]}`,
			expectedErr:  `optional platform fields provided, but OS and Architecture are missing`,
		},
		{
			doc:          "valid platform",
			platformJSON: `{"architecture":"arm64","os":"linux","os.version":"12.0", "os.features":["a","b"], "variant": "v7"}`,
			expected: &ocispec.Platform{
				Architecture: "arm64",
				OS:           "linux",
				OSVersion:    "12.0",
				OSFeatures:   []string{"a", "b"},
				Variant:      "v7",
			},
		},
	}
	for _, tc := range tests {
		t.Run(tc.doc, func(t *testing.T) {
			p, err := DecodePlatform(tc.platformJSON)
			assert.Check(t, is.DeepEqual(p, tc.expected))
			if tc.expectedErr != "" {
				assert.Check(t, cerrdefs.IsInvalidArgument(err))
				assert.Check(t, is.Error(err, tc.expectedErr))
			} else {
				assert.Check(t, err)
			}
		})
	}
}