forked from mtusi2026/mtusi2026lab6
lab6
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
// Step 5 smoke: PNG / JPEG / truncated PNG / non-image / size limit.
|
||||
#include "image_util.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef IMAGE_SMOKE_FIXTURES
|
||||
#define IMAGE_SMOKE_FIXTURES "tools/fixtures"
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
std::vector<std::uint8_t> read_all(const std::string& path) {
|
||||
std::ifstream in(path, std::ios::binary);
|
||||
if (!in) {
|
||||
throw std::runtime_error("cannot open " + path);
|
||||
}
|
||||
return std::vector<std::uint8_t>((std::istreambuf_iterator<char>(in)),
|
||||
std::istreambuf_iterator<char>());
|
||||
}
|
||||
|
||||
bool expect_ok(const char* name, const std::vector<std::uint8_t>& bytes,
|
||||
const char* want_ct) {
|
||||
ImageLimits lim;
|
||||
ImageInfo info;
|
||||
auto rej = validate_image(bytes.data(), bytes.size(), lim, info);
|
||||
if (rej) {
|
||||
std::cerr << name << ": expected OK, got " << image_reject_code(*rej)
|
||||
<< "\n";
|
||||
return false;
|
||||
}
|
||||
if (info.content_type != want_ct) {
|
||||
std::cerr << name << ": content_type " << info.content_type << "\n";
|
||||
return false;
|
||||
}
|
||||
std::cout << name << ": OK " << info.content_type << " " << info.width
|
||||
<< "x" << info.height << "\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool expect_reject(const char* name, const std::vector<std::uint8_t>& bytes,
|
||||
ImageReject want) {
|
||||
ImageLimits lim;
|
||||
ImageInfo info;
|
||||
auto rej = validate_image(bytes.data(), bytes.size(), lim, info);
|
||||
if (!rej || *rej != want) {
|
||||
std::cerr << name << ": expected " << image_reject_code(want) << ", got "
|
||||
<< (rej ? image_reject_code(*rej) : "OK") << "\n";
|
||||
return false;
|
||||
}
|
||||
std::cout << name << ": OK reject=" << image_reject_code(want) << "\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
try {
|
||||
const std::string dir =
|
||||
(argc > 1) ? argv[1] : std::string(IMAGE_SMOKE_FIXTURES);
|
||||
const auto png = read_all(dir + "/tiny.png");
|
||||
const auto jpg = read_all(dir + "/tiny.jpg");
|
||||
|
||||
bool ok = true;
|
||||
ok &= expect_ok("png", png, "image/png");
|
||||
ok &= expect_ok("jpeg", jpg, "image/jpeg");
|
||||
|
||||
std::vector<std::uint8_t> trunc(png.begin(),
|
||||
png.begin() + std::min<std::size_t>(16, png.size()));
|
||||
ok &= expect_reject("truncated_png", trunc, ImageReject::invalid_image);
|
||||
|
||||
std::vector<std::uint8_t> spoof = jpg;
|
||||
spoof[0] = 'G';
|
||||
spoof[1] = 'I';
|
||||
spoof[2] = 'F';
|
||||
ok &= expect_reject("not_image", spoof, ImageReject::unsupported_media_type);
|
||||
|
||||
ImageLimits tiny_lim;
|
||||
tiny_lim.max_bytes = 10;
|
||||
ImageInfo info;
|
||||
auto rej = validate_image(png.data(), png.size(), tiny_lim, info);
|
||||
if (!rej || *rej != ImageReject::payload_too_large) {
|
||||
std::cerr << "max_bytes: FAIL\n";
|
||||
ok = false;
|
||||
} else {
|
||||
std::cout << "max_bytes: OK\n";
|
||||
}
|
||||
|
||||
// JPEG bytes sniffed as jpeg regardless of any HTTP Content-Type.
|
||||
auto sniffed = sniff_content_type(jpg.data(), jpg.size());
|
||||
if (!sniffed || *sniffed != "image/jpeg") {
|
||||
std::cerr << "sniff_jpeg: FAIL\n";
|
||||
ok = false;
|
||||
} else {
|
||||
std::cout << "sniff_jpeg: OK (MIME spoof would be ignored)\n";
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
return 1;
|
||||
}
|
||||
std::cout << "image_smoke: OK\n";
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "image_smoke: FAIL: " << e.what() << "\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user