191 lines
8.2 KiB
Plaintext
191 lines
8.2 KiB
Plaintext
# Tests of an executable's resources: icons of PNG files and icon files, the
|
|
# tree `.rsrc` holds them in, found again by their types and numbers, what the
|
|
# file's properties say of the program, and its text as Windows holds it.
|
|
import std::io;
|
|
import binary::pe;
|
|
import std::testing;
|
|
|
|
|
|
# The header of a PNG of a size, which is all an icon's group reads of it.
|
|
def png_of(width: u32, height: u32): byte_writer {
|
|
var made := byte_writer::new();
|
|
val signature: u8[8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
|
|
made.put(signature[:]);
|
|
made.byte(0); made.byte(0); made.byte(0); made.byte(13);
|
|
made.put("IHDR");
|
|
for each in [width, height] {
|
|
made.byte((each >> 24) as u8);
|
|
made.byte((each >> 16) as u8);
|
|
made.byte((each >> 8) as u8);
|
|
made.byte(each as u8);
|
|
}
|
|
made.put("pixels");
|
|
return made;
|
|
}
|
|
|
|
# An icon file of two images, 16 and 256 pixels wide, each its bytes as text.
|
|
def ico_file(): byte_writer {
|
|
var made := byte_writer::new();
|
|
made.u16(0);
|
|
made.u16(1);
|
|
made.u16(2);
|
|
# 16x16, 32 bits, "small" at 38; 256x256, as 0, "large!" at 43.
|
|
made.byte(16); made.byte(16); made.byte(0); made.byte(0); made.u16(1); made.u16(32); made.u32(5); made.u32(38);
|
|
made.byte(0); made.byte(0); made.byte(0); made.byte(0); made.u16(1); made.u16(32); made.u32(6); made.u32(43);
|
|
made.put("small");
|
|
made.put("large!");
|
|
return made;
|
|
}
|
|
|
|
# Whether bytes of a span are text.
|
|
def holds_text(bytes: span<u8>, at: u64, text: strview): bool {
|
|
return false if at + text.length > bytes.length;
|
|
for k in 0..text.length {
|
|
return false if bytes[at + k] != text[k];
|
|
}
|
|
return true;
|
|
}
|
|
|
|
# Whether bytes hold text as UTF-16, the least significant byte first.
|
|
def holds_utf16(bytes: span<u8>, text: strview): bool {
|
|
var wide := byte_writer::new();
|
|
pe::put_utf16(&mut wide, text);
|
|
val want := wide.span;
|
|
val length := want.length - 2;
|
|
return false if bytes.length < length;
|
|
for at in 0..=(bytes.length - length) {
|
|
var same := true;
|
|
for k in 0..length {
|
|
same = same && bytes[at + k] == want[k];
|
|
}
|
|
return true if same;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@test def an_icons_images_are_pngs_and_icon_files_but_not_larger_than_256(t: &mut tester): void {
|
|
var icon := pe::icon_group::new();
|
|
val small := png_of(32, 32);
|
|
t.expects(icon.add(small.span) == pe::icon_read::added);
|
|
val large := png_of(256, 256);
|
|
t.expects(icon.add(large.span) == pe::icon_read::added);
|
|
val huge := png_of(512, 512);
|
|
t.expects(icon.add(huge.span) == pe::icon_read::too_large);
|
|
val file := ico_file();
|
|
t.expects(icon.add(file.span) == pe::icon_read::added);
|
|
t.expects(icon.length == 4);
|
|
t.expects(icon.add("GIF89a and the rest") == pe::icon_read::unknown);
|
|
# An icon file whose image reaches past its end.
|
|
var cut := ico_file();
|
|
cut.rewind(cut.length - 3);
|
|
t.expects(icon.add(cut.span) == pe::icon_read::broken);
|
|
t.expects(icon.length == 4);
|
|
|
|
val size := pe::png_size(huge.span);
|
|
t.expects(size[0] == 512 && size[1] == 512);
|
|
t.expects(pe::png_size("not one")[0] == 0);
|
|
}
|
|
|
|
@test def resources_are_found_by_their_types_and_numbers(t: &mut tester): void {
|
|
var icon := pe::icon_group::new();
|
|
val small := png_of(16, 16);
|
|
val large := png_of(256, 256);
|
|
_ = icon.add(small.span);
|
|
_ = icon.add(large.span);
|
|
var made := pe::resources::new();
|
|
made.add_icon(&icon, 1, pe::language_neutral);
|
|
val version := pe::version_resource("Hearth", "1.2.3", "hearth.exe");
|
|
made.add(pe::resource_version, 1, pe::language_english, version.span);
|
|
t.expects(made.length == 4 && !made.empty);
|
|
t.expects(made.next_id(pe::resource_icon) == 3 && made.next_id(pe::resource_manifest) == 1);
|
|
|
|
# The section, in an executable meant for windows of its own.
|
|
val code: u8[1] = [0xC3];
|
|
val section := made.bytes(0x2000);
|
|
var image := pe::image::new(0x400000, 0x401000);
|
|
image.subsystem = pe::subsystem_windows;
|
|
image.section(".text", 0x401000, code[:], 1, pe::readable | pe::executable);
|
|
image.section(".rsrc", 0x402000, section.span, section.length, pe::readable);
|
|
image.resources = pe::directory { address = 0x402000, size = section.length };
|
|
val bytes := image.file();
|
|
val all := bytes.span;
|
|
val pe_at := word(all, 0x3C, 4);
|
|
t.expects(word(all, pe_at + 24 + 68, 2) == 2);
|
|
t.expects(word(all, pe_at + 24 + 112 + 16, 4) == 0x2000 && word(all, pe_at + 24 + 112 + 20, 4) == section.length);
|
|
t.expects(word(all, header_of(all, 1) + 36, 4) == 0x40000040);
|
|
|
|
# The group: two images, the second 256 pixels each way, written as 0, by
|
|
# the numbers of the icons holding them.
|
|
val group := pe::find_resource(all, pe::resource_icon_group, 1) else {
|
|
t.expects(false, "no icon group");
|
|
return;
|
|
};
|
|
t.expects(group.length == 6 + 14 * 2 && word(group, 2, 2) == 1 && word(group, 4, 2) == 2);
|
|
t.expects(group[6] == 16 && group[7] == 16 && word(group, 6 + 6, 2) == 32 && word(group, 6 + 8, 4) == small.length);
|
|
t.expects(group[20] == 0 && group[21] == 0 && word(group, 20 + 12, 2) == 2);
|
|
val second := pe::find_resource(all, pe::resource_icon, word(group, 20 + 12, 2) as u32) else {
|
|
t.expects(false, "no icon of the group's");
|
|
return;
|
|
};
|
|
t.expects(second.length == large.length && holds_text(second, 12, "IHDR") && second[18] == 1);
|
|
|
|
val found := pe::find_resource(all, pe::resource_version, 1) else {
|
|
t.expects(false, "no version");
|
|
return;
|
|
};
|
|
t.expects(found.length == version.length && holds_utf16(found, "Hearth"));
|
|
t.expects(pe::find_resource(all, pe::resource_icon_group, 2) is none);
|
|
t.expects(pe::find_resource(all, pe::resource_manifest, 1) is none);
|
|
|
|
# An executable without them has none to find.
|
|
var plain := pe::image::new(0x400000, 0x401000);
|
|
plain.section(".text", 0x401000, code[:], 1, pe::readable | pe::executable);
|
|
val none := plain.file();
|
|
t.expects(pe::find_resource(none.span, pe::resource_icon_group, 1) is none);
|
|
t.expects(word(none.span, word(none.span, 0x3C, 4) + 24 + 68, 2) == 3);
|
|
}
|
|
|
|
@test def a_resource_added_again_takes_the_place_of_the_first(t: &mut tester): void {
|
|
var made := pe::resources::new();
|
|
made.add(pe::resource_manifest, 1, 0, "first");
|
|
made.add(pe::resource_manifest, 1, 0, "second");
|
|
made.add(pe::resource_manifest, 1, pe::language_english, "english");
|
|
t.expects(made.length == 2);
|
|
val section := made.bytes(0x1000);
|
|
# One type, one number, two languages, in order, the neutral one first.
|
|
t.expects(word(section.span, 14, 2) == 1 && word(section.span, 16, 4) == 24);
|
|
t.expects((word(section.span, 20, 4) & 0x7FFFFFFF) == 24);
|
|
val languages := word(section.span, 24 + 20, 4) & 0x7FFFFFFF;
|
|
t.expects(word(section.span, languages + 14, 2) == 2);
|
|
t.expects(word(section.span, languages + 16, 4) == 0 && word(section.span, languages + 24, 4) == 0x409);
|
|
val entry := word(section.span, languages + 20, 4);
|
|
t.expects(word(section.span, entry + 4, 4) == 6);
|
|
val data := word(section.span, entry, 4) - 0x1000;
|
|
t.expects(holds_text(section.span, data, "second"));
|
|
}
|
|
|
|
@test def the_version_says_the_program_its_numbers_and_its_file(t: &mut tester): void {
|
|
val made := pe::version_resource("Hearth", "1.2.3-alpha", "hearth.edit.exe");
|
|
val bytes := made.span;
|
|
t.expects(word(bytes, 0, 2) == made.length && word(bytes, 2, 2) == 52 && word(bytes, 4, 2) == 0);
|
|
t.expects(holds_utf16(bytes, "VS_VERSION_INFO"));
|
|
# The fixed part, after the key and its zero, at four bytes.
|
|
t.expects(word(bytes, 40, 4) == 0xFEEF04BD);
|
|
t.expects(word(bytes, 48, 4) == 0x00010002 && word(bytes, 52, 4) == 0x00030000);
|
|
t.expects(word(bytes, 68, 4) == 2 && word(bytes, 76, 4) == 1); # A prerelease, and a program.
|
|
for text in ["StringFileInfo", "040904B0", "FileDescription", "Hearth", "1.2.3-alpha", "hearth.edit", "hearth.edit.exe", "VarFileInfo", "Translation"] {
|
|
t.expects(holds_utf16(bytes, text), text);
|
|
}
|
|
|
|
# With no title, it's named by its file; with no version, it says none.
|
|
val plain := pe::version_resource("", "", "tool.exe");
|
|
t.expects(word(plain.span, 48, 4) == 0 && !holds_utf16(plain.span, "FileVersion") && holds_utf16(plain.span, "ProductName"));
|
|
t.expects(word(plain.span, 68, 4) == 0);
|
|
}
|
|
|
|
@test def text_is_written_as_utf16(t: &mut tester): void {
|
|
var made := byte_writer::new();
|
|
pe::put_utf16(&mut made, "A\xC3\xA9\xE2\x82\xAC\xF0\x9F\x98\x80");
|
|
t.expects(hex_of(made.span).span == "4100e900ac203dd800de0000", hex_of(made.span).span);
|
|
}
|