Use cases
File upload test cases, with the files to run them
A checklist of test cases for a file upload, from the size limit to a file name that tries to
leave its folder. Every case names the file that tests it and what your system should do with it,
so the list can be run and not only read.
The whole list in one command
tfg generate --preset upload-validation --limit 10mb --allow jpg,png,pdf --out ./upload
writes a file for nearly every case below, with a manifest that says what should happen to each.
Put in the limit and the types your form declares.
accept means your system should take the file and reject that it should
turn it away with a clear message. unspecified means either answer can be right, so
write down which one your system means, and check that it does that every time. The word after the
outcome is the reason the manifest gives.
Size
Is the size limit enforced where it is declared?
| Test case | File | Expected |
| One byte under the limit | 10mb_under_1b.jpg | accept |
| Exactly at the limit | 10mb_at_limit.jpg | accept |
| One byte over the limit | 10mb_over_1b.jpg | reject size_limit |
| Twice the limit, to see that the server stops reading early instead of taking it all into memory | 10mb_far_over_2x.jpg | reject size_limit |
| An empty file of 0 bytes | empty.jpg | unspecified size_zero |
The two cases a limit most often gets wrong are a limit checked only in the browser, and a limit
counted in MB where the code counts MiB. The
size-boundaries preset puts a file at every distance from
the limit you choose, a byte, a kilobyte and a megabyte either side.
Type
Does the form take the types it should and refuse the rest?
| Test case | File | Expected |
| Each type the form takes, as a real file of that type | allowed_jpg.jpg, allowed_png.png, allowed_pdf.pdf | accept |
| A type the form does not take | denied.svg, denied.html, denied.exe, denied.sh | reject extension_rule |
| An extension that says one type over content of another, such as a PDF named .jpg | pdf_as_jpg.jpg, png_as_pdf.pdf, jpg_as_png.png | reject mime_mismatch |
| Two extensions, the last one not allowed | invoice.jpg.exe | reject extension_rule |
| No extension at all | invoice | unspecified extension_rule |
| The extension in capitals | PHOTO.JPG | unspecified extension_rule |
An SVG or an HTML file taken for a picture or for plain text is a way to get a script past a
form, which is why they are on the list of types to refuse unless your form means to take them.
Content
Does the system open the file, or only look at its name?
| Test case | File | Expected |
| A file cut short, the way an interrupted upload leaves it | corrupt-truncate.pdf | reject content_malformed |
| A file whose first bytes are zeros, so nothing says what it is | corrupt-zero-head.png | reject content_malformed |
| The smallest file a format allows, valid and tiny | tfg generate --preset empty-and-minimal | accept |
A file that only has the right name and the right size passes every check that never opens it.
Corrupt sample files come in PDF, Word, Excel, PowerPoint,
JPG, PNG, MP4 and ZIP, and corrupt test files says how to make
them in any format at any size.
Name
Does the file come back under the name it was given?
| Test case | File | Expected |
| Spaces and brackets | my report (final) v2.jpg | accept |
| Letters outside ASCII and an emoji | a Polish word, a Japanese word and an emoji in one name | unspecified filename_invalid |
| A name over 200 characters long | aaa...aaa.jpg | unspecified filename_too_long |
A name that climbs out of its folder, ../ or ..\ | climbing_name.jpg, uploaded as ../tfg_climbed_out.jpg | unspecified filename_traversal |
| An absolute path, on Linux or Windows | absolute_name.jpg, uploaded as /tmp/tfg_climbed_out.jpg | unspecified filename_traversal |
A path in a name cannot be saved to a disk, so the file keeps a plain name and the manifest says
which name to upload it under. Taking the file and keeping only the last part of the name is a
good answer. Writing it outside the upload folder is the defect this case exists to find. The
filename-handling preset has many more names: look-alike
letters, invisible characters, names reserved by Windows and lengths counted in bytes.
Volume and archives
What happens with many files, or a file holding many?
| Test case | File | Expected |
| Fifty files in one upload | bulk_0001.jpg to bulk_0050.jpg | accept |
| A ZIP that really holds 200 documents | tfg generate --format zip --size 5mb --set entries=200 --set entry_format=pdf | accept |
| An archive your system unpacks, checked for zip slip and zip bombs first | tfg tool archive-scan upload.zip | safe, or what it found |
--bulk sets how many files the mass upload holds, up to ten thousand.
Tools says what the archive check looks for.
In a test
Running the list as a test
The manifest holds the expected outcome of every file and the name to upload it under, so one
loop runs the whole list:
import json, os
directory = "upload"
manifest = json.load(open(os.path.join(directory, "manifest.json")))
for entry in manifest["files"]:
outcome = entry["expected"]["outcome"]
if outcome == "unspecified":
continue
name = entry.get("send_as", entry["name"])
response = upload(os.path.join(directory, entry["path"]), name)
assert response.ok == (outcome == "accept"), name
The files marked unspecified are left out here, because their answer is your own
policy. Decide it, write it into the recipe with expected, and the loop checks them
too. Test files in CI runs this in a pipeline.