forked from mtusi2026/mtusi2026lab5
lab5
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 502 B |
Binary file not shown.
|
After Width: | Height: | Size: 644 B |
Binary file not shown.
|
After Width: | Height: | Size: 633 B |
Binary file not shown.
|
After Width: | Height: | Size: 69 B |
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// Small smoke tool for step 4: put → get → delete against MinIO.
|
||||
#include "s3_client.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
int main() {
|
||||
try {
|
||||
auto cfg = S3Client::from_env();
|
||||
S3Client s3(cfg);
|
||||
|
||||
const std::string key = "smoke/step4.txt";
|
||||
const std::string payload = "lab4-s3-smoke-ok";
|
||||
std::istringstream in(payload);
|
||||
s3.put_object(key, in, static_cast<std::int64_t>(payload.size()),
|
||||
"text/plain");
|
||||
|
||||
std::ostringstream out;
|
||||
s3.get_object(key, out);
|
||||
if (out.str() != payload) {
|
||||
std::cerr << "mismatch: got [" << out.str() << "]\n";
|
||||
return 2;
|
||||
}
|
||||
|
||||
s3.delete_object(key);
|
||||
std::cout << "s3_smoke: OK bucket=" << cfg.bucket << " key=" << key << "\n";
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "s3_smoke: FAIL: " << e.what() << "\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "image_pipeline.hpp"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc != 3) {
|
||||
std::cerr << "usage: thumbnail_smoke INPUT OUTPUT.jpg\n";
|
||||
return 2;
|
||||
}
|
||||
try {
|
||||
std::ifstream input(argv[1], std::ios::binary);
|
||||
if (!input) return 1;
|
||||
const std::vector<std::uint8_t> bytes((std::istreambuf_iterator<char>(input)), {});
|
||||
const auto image = make_thumbnail(bytes);
|
||||
std::ofstream output(argv[2], std::ios::binary);
|
||||
output.write(image.bytes.data(), image.bytes.size());
|
||||
if (!output) return 1;
|
||||
std::cout << image.width << 'x' << image.height << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << e.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "image_pipeline.hpp"
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc != 3) return 2;
|
||||
try {
|
||||
std::ifstream input(argv[1], std::ios::binary);
|
||||
if (!input) return 1;
|
||||
const std::vector<std::uint8_t> bytes((std::istreambuf_iterator<char>(input)), {});
|
||||
const auto variants = make_variants(bytes);
|
||||
std::filesystem::create_directories(argv[2]);
|
||||
nlohmann::json manifest;
|
||||
for (const auto& v : variants) {
|
||||
const auto path = std::filesystem::path(argv[2]) /
|
||||
(v.name + (v.content_type == "image/webp" ? ".webp" : ".jpg"));
|
||||
std::ofstream out(path, std::ios::binary);
|
||||
out.write(v.image.bytes.data(), v.image.bytes.size());
|
||||
if (!out) return 1;
|
||||
manifest[v.name] = {{"width", v.image.width}, {"height", v.image.height},
|
||||
{"size_bytes", v.image.bytes.size()}};
|
||||
}
|
||||
std::cout << manifest.dump() << '\n';
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << e.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user