Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
riverar committed Sep 18, 2023
1 parent d846498 commit a1cc775
Showing 1 changed file with 66 additions and 63 deletions.
129 changes: 66 additions & 63 deletions crates/tools/msvc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,75 +340,78 @@ fn make_reproducible(lib: &std::path::Path) {

#[test]
fn test_make_reproducible() {
let mut def = std::fs::File::create("test.def").unwrap();
def.write_all(
format!(
r#"
for (machine, offset) in [("x86", 0), ("x64", 12), ("arm64", 12)] {
let mut def = std::fs::File::create("test.def").unwrap();
def.write_all(
format!(
r#"
LIBRARY long-library-name-for-placement-in-long-names-table.dll
EXPORTS
A=A.#1
B=B.#2
C=C.#3
"#
)
.as_bytes(),
)
.as_bytes(),
)
.unwrap();
drop(def);

let mut cmd = std::process::Command::new("lib");
cmd.arg("/nologo");
cmd.arg("/Brepro");
cmd.arg(format!("/out:test.lib"));
cmd.arg(format!("/def:test.def"));
cmd.output().unwrap();

let mut archive = std::fs::File::options()
.read(true)
.write(true)
.open("test.lib")
.unwrap();

let mut buf = [0; 24];

make_reproducible(&std::path::Path::new("test.lib"));

// Archive member header timestamp
archive.seek(SeekFrom::Start(0x18)).unwrap();
archive.read_exact(&mut buf[..12]).unwrap();
assert_eq!(&buf[..12], b"-1 ");

// COFF file header time/date stamp
archive.seek(SeekFrom::Start(0x322)).unwrap();
archive.read_exact(&mut buf[..4]).unwrap();
assert_eq!(&buf[..4], [0x00, 0x00, 0x00, 0x00]);

// Compiler version symbol
archive.seek(SeekFrom::Start(0x481)).unwrap();
archive.read_exact(&mut buf[..12]).unwrap();
assert_eq!(&buf[..12], b"@comp.id\0\0\0\0");

// Import header time/date stamp
archive.seek(SeekFrom::Start(0x493)).unwrap();
archive.read_exact(&mut buf[..4]).unwrap();
assert_eq!(&buf[..4], [0x00, 0x00, 0x00, 0x00]);

// Sampling to ensure lib is consistent
archive.seek(SeekFrom::Start(0xB4)).unwrap();
archive.read_exact(&mut buf[..24]).unwrap();
assert_eq!(&buf[..24], b"__NULL_IMPORT_DESCRIPTOR");

archive.seek(SeekFrom::Start(0x26E)).unwrap();
archive.read_exact(&mut buf[..4]).unwrap();
assert_eq!(&buf[..4], b"// ");

archive.seek(SeekFrom::Start(0x405)).unwrap();
archive.read_exact(&mut buf[..18]).unwrap();
assert_eq!(&buf[..18], b"Microsoft (R) LINK");

drop(archive);

std::fs::remove_file("test.lib").unwrap();
std::fs::remove_file("test.exp").unwrap();
std::fs::remove_file("test.def").unwrap();
drop(def);

let mut cmd = std::process::Command::new("lib");
cmd.arg("/nologo");
cmd.arg("/Brepro");
cmd.arg(format!("/machine:{machine}"));
cmd.arg(format!("/out:test.lib"));
cmd.arg(format!("/def:test.def"));
cmd.output().unwrap();

let mut archive = std::fs::File::options()
.read(true)
.write(true)
.open("test.lib")
.unwrap();

let mut buf = [0; 24];

make_reproducible(&std::path::Path::new("test.lib"));

// Archive member header timestamp
archive.seek(SeekFrom::Start(0x18)).unwrap();
archive.read_exact(&mut buf[..12]).unwrap();
assert_eq!(&buf[..12], b"-1 ");

// COFF file header time/date stamp
archive.seek(SeekFrom::Start(0x322 - offset)).unwrap();
archive.read_exact(&mut buf[..4]).unwrap();
assert_eq!(&buf[..4], [0x00, 0x00, 0x00, 0x00]);

// Compiler version symbol
archive.seek(SeekFrom::Start(0x481 - offset)).unwrap();
archive.read_exact(&mut buf[..12]).unwrap();
assert_eq!(&buf[..12], b"@comp.id\0\0\0\0");

// Import header time/date stamp
archive.seek(SeekFrom::Start(0x493 - offset)).unwrap();
archive.read_exact(&mut buf[..4]).unwrap();
assert_eq!(&buf[..4], [0x00, 0x00, 0x00, 0x00]);

// Sampling to ensure lib is consistent
archive.seek(SeekFrom::Start(0xB4)).unwrap();
archive.read_exact(&mut buf[..24]).unwrap();
assert_eq!(&buf[..24], b"__NULL_IMPORT_DESCRIPTOR");

archive.seek(SeekFrom::Start(0x26E - offset)).unwrap();
archive.read_exact(&mut buf[..4]).unwrap();
assert_eq!(&buf[..4], b"// ");

archive.seek(SeekFrom::Start(0x405 - offset)).unwrap();
archive.read_exact(&mut buf[..18]).unwrap();
assert_eq!(&buf[..18], b"Microsoft (R) LINK");

drop(archive);

std::fs::remove_file("test.lib").unwrap();
std::fs::remove_file("test.exp").unwrap();
std::fs::remove_file("test.def").unwrap();
}
}

0 comments on commit a1cc775

Please sign in to comment.