/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include #include #include #include #include #include "fcitx-utils/log.h" #include "../src/resourceguard.h" using namespace fcitx::m3ui; int main() { FCITX_ASSERT(safeImageSize(1, 1)); FCITX_ASSERT(safeImageSize(4096, 4096)); FCITX_ASSERT(safeImageSize(8192, 128)); FCITX_ASSERT(!safeImageSize(0, 16)); FCITX_ASSERT(!safeImageSize(-1, 16)); FCITX_ASSERT(!safeImageSize(8193, 16)); FCITX_ASSERT(!safeImageSize(8192, 8192)); FCITX_ASSERT(!safeImageSize(4096.1, 4096)); FCITX_ASSERT(!safeImageSize(std::numeric_limits::infinity(), 16)); FCITX_ASSERT(!safeImageSize(std::numeric_limits::quiet_NaN(), 16)); FCITX_ASSERT(safeImageMargins(16, 16, 0, 0, 0, 0)); FCITX_ASSERT(safeImageMargins(16, 16, 8, 8, 8, 8)); FCITX_ASSERT(!safeImageMargins(16, 16, 9, 8, 8, 8)); FCITX_ASSERT(!safeImageMargins(16, 16, -1, 0, 0, 0)); FCITX_ASSERT(!safeImageMargins(0, 16, 0, 0, 0, 0)); FCITX_ASSERT(!safeImageMargins(16, 16, std::numeric_limits::max(), 0, 1, 0)); std::vector png = {137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 'I', 'H', 'D', 'R', 0, 0, 0, 16, 0, 0, 0, 16}; FCITX_ASSERT(safePngSize(png)); png[16] = 255; FCITX_ASSERT(!safePngSize(png)); png[16] = 0; png[19] = 0; FCITX_ASSERT(!safePngSize(png)); png.resize(23); FCITX_ASSERT(!safePngSize(png)); FCITX_ASSERT(!readImageSnapshot(fcitx::UnixFD{})); auto name = (std::filesystem::temp_directory_path() / "fcitx-image-test-XXXXXX").string(); fcitx::UnixFD file = fcitx::UnixFD::own(mkstemp(name.data())); FCITX_ASSERT(file.isValid()); // Unlink immediately; the descriptor owns this isolated fixture. FCITX_ASSERT(unlink(name.c_str()) == 0); FCITX_ASSERT(!readImageSnapshot(file)); FCITX_ASSERT(write(file.fd(), "image", 5) == 5); FCITX_ASSERT(lseek(file.fd(), 0, SEEK_SET) == 0); auto bytes = readImageSnapshot(file); FCITX_ASSERT(bytes && bytes->size() == 5); // A short read (e.g. concurrent truncation) is rejected. FCITX_ASSERT(lseek(file.fd(), 3, SEEK_SET) == 3); FCITX_ASSERT(!readImageSnapshot(file)); FCITX_ASSERT(ftruncate(file.fd(), maxImageFileSize + 1) == 0); FCITX_ASSERT(!readImageSnapshot(file)); int fds[2]; FCITX_ASSERT(pipe(fds) == 0); fcitx::UnixFD readEnd = fcitx::UnixFD::own(fds[0]); fcitx::UnixFD writeEnd = fcitx::UnixFD::own(fds[1]); FCITX_ASSERT(!readImageSnapshot(readEnd)); }