Tactility/Modules/app-module/tests/source/elf_check_test.cpp
Ken Van Hoeylandt f8172446fd
App loading improvements and more (#675)
- External apps can opt into automatic cleanup when they exit, including tracked tasks, threads, files, directories, and memory.
- App-relative file paths resolve against the app’s working directory, with `.` and `..` normalized.
- The shell identifies ELF binaries that cannot be executed instead of treating them as scripts.
- Shared icon fonts now offer distinct default and large sizes, with updated icons across the interface.
- ESP32 apps can load additional ELF formats when address mirroring is enabled.
- Terminal colors render clearly on monochrome displays, and toolbar buttons have cleaner styling.
- GPS settings safely refresh their device list when their window is hidden or its widgets are destroyed.
2026-10-06 07:45:58 +02:00

75 lines
2.4 KiB
C++

// SPDX-License-Identifier: Apache-2.0
#include "doctest.h"
#include <app/elf_check.h>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <unistd.h>
namespace {
constexpr ElfRequirements REQUIREMENTS = {
.elf_class = ELF_CLASS_32,
.data = ELF_DATA_2LSB,
.types = ELF_TYPE_MASK(ELF_TYPE_DYN) | ELF_TYPE_MASK(ELF_TYPE_REL),
.machine = ELF_MACHINE_XTENSA,
};
class TempFile {
std::string path;
public:
explicit TempFile(const uint8_t* data, size_t size) {
char name[] = "/tmp/elf_check_test_XXXXXX";
const int fd = mkstemp(name);
REQUIRE_NE(fd, -1);
REQUIRE_EQ(write(fd, data, size), static_cast<ssize_t>(size));
close(fd);
path = name;
}
~TempFile() { unlink(path.c_str()); }
const char* get() const { return path.c_str(); }
};
TempFile makeElf(uint8_t elf_class, uint16_t type, uint16_t machine) {
uint8_t header[20] = { 0x7f, 'E', 'L', 'F', elf_class, ELF_DATA_2LSB };
header[16] = type & 0xFF;
header[17] = type >> 8;
header[18] = machine & 0xFF;
header[19] = machine >> 8;
return TempFile(header, sizeof(header));
}
} // namespace
TEST_CASE("elf_check_file accepts every type in the mask") {
CHECK(elf_check_file(makeElf(ELF_CLASS_32, ELF_TYPE_DYN, ELF_MACHINE_XTENSA).get(), &REQUIREMENTS));
CHECK(elf_check_file(makeElf(ELF_CLASS_32, ELF_TYPE_REL, ELF_MACHINE_XTENSA).get(), &REQUIREMENTS));
}
TEST_CASE("elf_check_file rejects a type outside the mask") {
CHECK_FALSE(elf_check_file(makeElf(ELF_CLASS_32, 2, ELF_MACHINE_XTENSA).get(), &REQUIREMENTS));
CHECK_FALSE(elf_check_file(makeElf(ELF_CLASS_32, 0xFE00, ELF_MACHINE_XTENSA).get(), &REQUIREMENTS));
}
TEST_CASE("elf_check_file rejects a mismatched class or machine") {
CHECK_FALSE(elf_check_file(makeElf(ELF_CLASS_64, ELF_TYPE_DYN, ELF_MACHINE_XTENSA).get(), &REQUIREMENTS));
CHECK_FALSE(elf_check_file(makeElf(ELF_CLASS_32, ELF_TYPE_DYN, ELF_MACHINE_RISCV).get(), &REQUIREMENTS));
}
TEST_CASE("elf_has_magic is true for any ELF and false otherwise") {
CHECK(elf_has_magic(makeElf(ELF_CLASS_32, 2, ELF_MACHINE_RISCV).get()));
const uint8_t script[] = "echo hello\n";
CHECK_FALSE(elf_has_magic(TempFile(script, sizeof(script) - 1).get()));
const uint8_t truncated[] = { 0x7f, 'E', 'L' };
CHECK_FALSE(elf_has_magic(TempFile(truncated, sizeof(truncated)).get()));
CHECK_FALSE(elf_has_magic("/nonexistent/elf_check_test"));
}