-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_binary.py
More file actions
324 lines (285 loc) · 11.2 KB
/
Copy pathtest_binary.py
File metadata and controls
324 lines (285 loc) · 11.2 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
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
"""Unit coverage for the per-version opaque-binary schema predicates.
These predicates are pure functions on the schema mapping, so they are asserted
directly without invoking validation. That is important for the OAS 3.0 array
``type`` case, which would otherwise crash the 3.0 ``type`` keyword.
"""
import pytest
from openapi_schema_validator._binary import is_oas30_binary_schema
from openapi_schema_validator._binary import is_oas31_binary_schema
from openapi_schema_validator._binary import is_oas31_strict_binary_schema
from openapi_schema_validator._binary import is_oas32_binary_schema
from openapi_schema_validator._binary import is_oas32_strict_binary_schema
OCTET = "application/octet-stream"
ALL_PREDICATES = [
is_oas30_binary_schema,
is_oas31_binary_schema,
is_oas32_binary_schema,
is_oas31_strict_binary_schema,
is_oas32_strict_binary_schema,
]
@pytest.mark.parametrize("predicate", ALL_PREDICATES)
@pytest.mark.parametrize("schema", [True, False, None, "x", 1, ["string"]])
def test_non_mapping_schema_is_false(predicate, schema):
# Boolean / non-mapping schemas are never classified as binary.
assert predicate(schema) is False
class TestIsOAS30BinarySchema:
@pytest.mark.parametrize(
"schema",
[
{"type": "string", "format": "binary"},
{"format": "binary"},
],
)
def test_binary_marker_is_true(self, schema):
assert is_oas30_binary_schema(schema) is True
@pytest.mark.parametrize(
"schema",
[
{"type": "string"},
{"type": "string", "format": "byte"},
{"type": "string", "format": "base64"},
{"type": "integer", "format": "binary"},
{"type": "string", "format": "date"},
{},
],
)
def test_non_binary_is_false(self, schema):
assert is_oas30_binary_schema(schema) is False
def test_array_type_is_false(self):
# Array-valued type is out of scope for OAS 3.0. The predicate must not
# mask the (separately latent) crashing schema -- assert directly,
# never via validation.
schema = {"type": ["string", "null"], "format": "binary"}
assert is_oas30_binary_schema(schema) is False
def test_encoding_co_presence_excludes(self):
# format: binary alongside a real contentEncoding is encoded text -- the
# encoding wins, so this is not classified as binary.
schema = {
"type": "string",
"format": "binary",
"contentEncoding": "base64",
}
assert is_oas30_binary_schema(schema) is False
def test_non_mapping_is_false(self):
assert is_oas30_binary_schema(True) is False
class TestIsOAS31BinarySchema:
@pytest.mark.parametrize(
"schema",
[
{},
{"contentMediaType": OCTET},
{"type": "string", "contentMediaType": OCTET},
{"type": ["string", "null"], "contentMediaType": OCTET},
{"contentMediaType": "application/pdf"},
{"contentMediaType": "image/png"},
{"contentMediaType": "audio/mpeg"},
{"contentMediaType": "video/mp4"},
# no-op identity encodings do not count as encoded text
{"contentMediaType": OCTET, "contentEncoding": "identity"},
{"contentMediaType": OCTET, "contentEncoding": "binary"},
{"contentMediaType": OCTET, "contentEncoding": "7bit"},
{"contentMediaType": OCTET, "contentEncoding": "8bit"},
# case-insensitive media-type matching, parameter stripping
{"type": "string", "contentMediaType": "Image/PNG"},
{
"type": "string",
"contentMediaType": "application/pdf; version=1",
},
],
)
def test_binary_is_true(self, schema):
assert is_oas31_binary_schema(schema) is True
@pytest.mark.parametrize(
"schema",
[
# typeless text is NOT binary (tightened phrasing)
{"contentMediaType": "application/json"},
{"contentMediaType": "application/ld+json"},
{"contentMediaType": "image/svg+xml"},
{"contentMediaType": "text/plain"},
# plain string assertion, no binary marker
{"type": "string"},
{"type": "string", "contentMediaType": "application/json"},
{
"type": "string",
"contentMediaType": "application/problem+json; charset=utf-8",
},
# format: binary is not a 3.1 binary marker
{"type": "string", "format": "binary"},
# encoded text is never opaque binary
{"type": "string", "format": "byte"},
{"type": "string", "format": "base64"},
{
"type": "string",
"contentMediaType": OCTET,
"contentEncoding": "base64",
},
{
"type": "string",
"contentMediaType": OCTET,
"contentEncoding": "base64url",
},
{
"type": "string",
"contentMediaType": OCTET,
"contentEncoding": "base16",
},
{
"type": "string",
"contentMediaType": OCTET,
"contentEncoding": "base32",
},
{
"type": "string",
"contentMediaType": OCTET,
"contentEncoding": "quoted-printable",
},
# typeless encoded schema: excluded by the encoded-text gate
{"contentMediaType": OCTET, "contentEncoding": "base16"},
# type without "string" is not a candidate
{"type": "integer", "contentMediaType": OCTET},
],
)
def test_non_binary_is_false(self, schema):
assert is_oas31_binary_schema(schema) is False
def test_non_mapping_is_false(self):
assert is_oas31_binary_schema(True) is False
class TestIsOAS31StrictBinarySchema:
@pytest.mark.parametrize(
"schema",
[
{},
{"contentMediaType": OCTET},
{"contentMediaType": "application/pdf"},
{"contentMediaType": "application/pdf; version=1"},
],
)
def test_typeless_raw_binary_is_true(self, schema):
assert is_oas31_strict_binary_schema(schema) is True
@pytest.mark.parametrize(
"schema",
[
# any type assertion stays on the string path under strict mode
{"type": "string", "contentMediaType": OCTET},
{"type": ["string", "null"], "contentMediaType": OCTET},
{"type": "string", "format": "binary"},
{"type": "string"},
# encoded / textual typeless schemas are not raw binary
{"contentMediaType": OCTET, "contentEncoding": "base16"},
{"contentMediaType": "application/json"},
{"contentMediaType": "image/svg+xml"},
],
)
def test_non_raw_binary_is_false(self, schema):
assert is_oas31_strict_binary_schema(schema) is False
class TestMediaTypeClassification:
@pytest.mark.parametrize(
"media_type",
[
"text/plain",
"text/csv",
"application/json",
"application/xml",
"application/x-www-form-urlencoded",
"application/javascript",
"application/ecmascript",
"application/yaml",
"application/x-yaml",
"application/graphql",
"application/x-ndjson",
"application/csv",
"application/ld+json",
"application/problem+json",
"image/svg+xml",
"application/vnd.api+yaml",
# case-insensitive + parameters
"Application/JSON",
"application/json; charset=utf-8",
"application/problem+json; charset=utf-8",
],
)
def test_textual_media_types_are_not_binary(self, media_type):
# Under a type: string assertion, textual content stays on the string
# path, so the default predicate is False.
schema = {"type": "string", "contentMediaType": media_type}
assert is_oas31_binary_schema(schema) is False
@pytest.mark.parametrize(
"media_type",
[
"application/octet-stream",
"application/pdf",
"application/zip",
"application/vnd.ms-excel",
"image/png",
"image/jpeg",
"audio/mpeg",
"video/mp4",
# case-insensitive + parameters
"Application/PDF",
"application/pdf; version=1.7",
],
)
def test_opaque_media_types_are_binary(self, media_type):
schema = {"type": "string", "contentMediaType": media_type}
assert is_oas31_binary_schema(schema) is True
class TestEncodingExclusion:
@pytest.mark.parametrize(
"encoding",
["base64", "base64url", "base16", "base32", "quoted-printable"],
)
def test_real_encoding_excludes_binary(self, encoding):
schema = {
"type": "string",
"contentMediaType": OCTET,
"contentEncoding": encoding,
}
assert is_oas31_binary_schema(schema) is False
@pytest.mark.parametrize(
"encoding", ["identity", "binary", "7bit", "8bit"]
)
def test_noop_encoding_keeps_binary(self, encoding):
schema = {
"type": "string",
"contentMediaType": OCTET,
"contentEncoding": encoding,
}
assert is_oas31_binary_schema(schema) is True
class TestCrossVersionDivergence:
def test_format_binary_is_binary_in_oas30_only(self):
schema = {"type": "string", "format": "binary"}
assert is_oas30_binary_schema(schema) is True
assert is_oas31_binary_schema(schema) is False
assert is_oas32_binary_schema(schema) is False
def test_typeless_content_media_type_is_binary_in_oas31_plus_only(self):
schema = {"contentMediaType": OCTET}
assert is_oas30_binary_schema(schema) is False
assert is_oas31_binary_schema(schema) is True
assert is_oas32_binary_schema(schema) is True
@pytest.mark.parametrize(
"schema",
[
{},
{"contentMediaType": OCTET},
{"type": "string", "contentMediaType": OCTET},
{"type": ["string", "null"], "contentMediaType": OCTET},
{"type": "string", "contentMediaType": "application/json"},
{"type": "string", "format": "binary"},
{"contentMediaType": OCTET, "contentEncoding": "base16"},
],
)
def test_oas32_default_matches_oas31_default(self, schema):
assert is_oas32_binary_schema(schema) == is_oas31_binary_schema(schema)
@pytest.mark.parametrize(
"schema",
[
{},
{"contentMediaType": OCTET},
{"type": "string", "contentMediaType": OCTET},
{"contentMediaType": "application/json"},
{"contentMediaType": OCTET, "contentEncoding": "base16"},
],
)
def test_oas32_strict_matches_oas31_strict(self, schema):
assert is_oas32_strict_binary_schema(
schema
) == is_oas31_strict_binary_schema(schema)