Testing Files Generator
English

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 caseFileExpected
One byte under the limit10mb_under_1b.jpgaccept
Exactly at the limit10mb_at_limit.jpgaccept
One byte over the limit10mb_over_1b.jpgreject size_limit
Twice the limit, to see that the server stops reading early instead of taking it all into memory10mb_far_over_2x.jpgreject size_limit
An empty file of 0 bytesempty.jpgunspecified 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 caseFileExpected
Each type the form takes, as a real file of that typeallowed_jpg.jpg, allowed_png.png, allowed_pdf.pdfaccept
A type the form does not takedenied.svg, denied.html, denied.exe, denied.shreject extension_rule
An extension that says one type over content of another, such as a PDF named .jpgpdf_as_jpg.jpg, png_as_pdf.pdf, jpg_as_png.pngreject mime_mismatch
Two extensions, the last one not allowedinvoice.jpg.exereject extension_rule
No extension at allinvoiceunspecified extension_rule
The extension in capitalsPHOTO.JPGunspecified 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 caseFileExpected
A file cut short, the way an interrupted upload leaves itcorrupt-truncate.pdfreject content_malformed
A file whose first bytes are zeros, so nothing says what it iscorrupt-zero-head.pngreject content_malformed
The smallest file a format allows, valid and tinytfg generate --preset empty-and-minimalaccept

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 caseFileExpected
Spaces and bracketsmy report (final) v2.jpgaccept
Letters outside ASCII and an emojia Polish word, a Japanese word and an emoji in one nameunspecified filename_invalid
A name over 200 characters longaaa...aaa.jpgunspecified filename_too_long
A name that climbs out of its folder, ../ or ..\climbing_name.jpg, uploaded as ../tfg_climbed_out.jpgunspecified filename_traversal
An absolute path, on Linux or Windowsabsolute_name.jpg, uploaded as /tmp/tfg_climbed_out.jpgunspecified 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 caseFileExpected
Fifty files in one uploadbulk_0001.jpg to bulk_0050.jpgaccept
A ZIP that really holds 200 documentstfg generate --format zip --size 5mb --set entries=200 --set entry_format=pdfaccept
An archive your system unpacks, checked for zip slip and zip bombs firsttfg tool archive-scan upload.zipsafe, 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.