Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ jobs:
run: |
opam exec -- dune runtest

- name: Integration tests
run: |
opam exec -- dune build @integration

- name: Format
run: |
opam exec -- dune build @fmt
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ $ opam install object

And add `object` to your project's `dune-project` or `*.opam` files.

## Tests

The tests for `object` are split between:
* Unit tests in `test/unit` that build in-memory representations of binaries
and test they can be parsed correctly. They can be run using
`dune build @runtest`.
* Integration tests in `test/integration` that use the system toolchain to
build binaries and check they can be parsed correctly. These tests rely more
on specific versions of the toolchain that match GitHub CI runners. They can
be run using `dune build @integration`.

Both suites can be run together with `dune build @run-all-tests`.

## Documentation

* Documentation on [ocaml.org](https://ocaml.org/p/object)
Expand Down
8 changes: 8 additions & 0 deletions dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
; dune build @run-all-tests: alias that runs the unit tests (@runtest)
; and the integration tests (@integration) together.

(alias
(name run-all-tests)
(deps
(alias_rec runtest)
(alias_rec integration)))
8 changes: 4 additions & 4 deletions dune-project
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(lang dune 3.9)
(lang dune 3.23)
(name object)
(generate_opam_files true)
(cram enable)
Expand All @@ -14,8 +14,8 @@
(synopsis "A unified interface for reading and writing object file formats")
(description "A unified interface for reading and writing object file formats")
(depends
(ocaml (>= 4.08))
(integers (>= 0.8.0))
(cmdliner :with-test)
(ocaml (>= 4.14))
(integers (>= 0.8))
(cmdliner (and :with-test (>= 2.1)))
(alcotest :with-test)
(ocamlformat (and :with-dev-setup (= 0.29.0)))))
1 change: 0 additions & 1 deletion example/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
(executables
(public_names oc_objdump generic_objdump ocnm octool oclipo ocfile)
(names oc_objdump generic_objdump ocnm octool oclipo ocfile)
(libraries integers object unix cmdliner))
10 changes: 6 additions & 4 deletions object.opam
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ tags: ["elf" "object" "mach-o" "pe" "coff"]
homepage: "https://github.com/tmcgilchrist/object"
bug-reports: "https://github.com/tmcgilchrist/object/issues"
depends: [
"dune" {>= "3.9"}
"ocaml" {>= "4.08"}
"integers" {>= "0.8.0"}
"cmdliner" {with-test}
"dune" {>= "3.23"}
"ocaml" {>= "4.14"}
"integers" {>= "0.8"}
"cmdliner" {with-test & >= "2.1"}
"alcotest" {with-test}
"ocamlformat" {with-dev-setup & = "0.29.0"}
"odoc" {with-doc}
Expand All @@ -33,3 +33,5 @@ build: [
]
]
dev-repo: "git+https://github.com/tmcgilchrist/object.git"
x-maintenance-intent: ["(latest)"]
available: arch != "ppc64"
1 change: 1 addition & 0 deletions object.opam.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
available: arch != "ppc64"
157 changes: 71 additions & 86 deletions test/dune → test/integration/dune
Original file line number Diff line number Diff line change
@@ -1,109 +1,94 @@
(test
(name test_object)
(libraries object integers alcotest))
; Integration tests: build real object files and executables with the system
; toolchain, then verify the object library can read the details back. They
; also compare the example tools against the platform's native tools.
;

(executables
(names
test_pe_real
test_fat
test_macho_arm64
test_macho_amd64
test_elf_arm64)
(libraries object integers alcotest cmdliner))

; Fixtures produced by the local toolchain.

(test
(name test_leb128)
(libraries object integers alcotest))
(rule
(deps hello_world.c)
(target hello_world)
(enabled_if
(or
(= %{system} "linux")
(= %{system} "macosx")))
(action
(run cc %{deps} -o %{target})))

(test
(name test_pe)
(libraries object integers alcotest))
; A Windows PE/COFF object built by cross-compiling a header-free C source.

; Exercise the reader against a real toolchain-produced COFF object, when a
; cross-compiler that targets Windows PE/COFF is available.
(rule
(deps hello_pe.c)
(target hello_pe.obj)
(enabled_if
(or
(= %{bin-available:clang} true)
(= %{bin-available:x86_64-w64-mingw32-gcc} true)))
(action
(system
"clang --target=x86_64-pc-windows-msvc -c %{deps} -o %{target} || x86_64-w64-mingw32-gcc -c %{deps} -o %{target}")))

; Reader acceptance tests against the real binaries above.

(test
(name test_pe_real)
(build_if
(rule
(alias integration)
(enabled_if
(or
(= %{bin-available:clang} true)
(= %{bin-available:x86_64-w64-mingw32-gcc} true)))
(libraries object integers alcotest cmdliner)
(deps hello_pe.obj)
(action
(run %{exe:test_pe_real.exe} --binary %{dep:hello_pe.obj})))

(test
(name test_fat)
(build_if
(rule
(alias integration)
(enabled_if
(and
(= %{system} "macosx")
(= %{architecture} "arm64")))
(libraries object integers alcotest)
(action
(run %{exe:test_fat.exe})))

(test
(name test_macho_arm64)
(rule
(alias integration)
(deps hello_world)
(build_if
(enabled_if
(and
(= %{system} "macosx")
(= %{architecture} "arm64")))
(libraries object integers alcotest cmdliner)
(action
(run %{exe:test_macho_arm64.exe} --binary %{dep:hello_world})))

(test
(name test_macho_amd64)
(rule
(alias integration)
(deps hello_world)
(build_if
(enabled_if
(and
(= %{system} "macosx")
(= %{architecture} "amd64")))
(libraries object integers alcotest cmdliner)
(action
(run %{exe:test_macho_amd64.exe} --binary %{dep:hello_world})))

(test
(name test_elf_arm64)
(libraries object integers alcotest cmdliner)
(build_if
(rule
(alias integration)
(deps hello_world)
(enabled_if
(and
(= %{system} "linux")
(= %{architecture} "arm64")))
(action
(run %{exe:test_elf_arm64.exe} --binary %{dep:hello_world})))

; Provide a plain C program to inspect

(rule
(deps hello_world.c)
(target hello_world)
(enabled_if
(or
(= %{system} "linux")
(= %{system} "macosx")))
(action
(run cc %{deps} -o %{target})))

; Provide a C program with DWARF 5 debug information to inspect.

(rule
(deps hello_world.c)
(target hello_world_dwarf_5)
(enabled_if
(or
(= %{system} "linux")
(= %{system} "macosx")))
(action
(run cc -gdwarf-5 %{deps} -o %{target})))

; A Windows PE/COFF object built by cross-compiling a header-free C source.

(rule
(deps hello_pe.c)
(target hello_pe.obj)
(enabled_if
(or
(= %{bin-available:clang} true)
(= %{bin-available:x86_64-w64-mingw32-gcc} true)))
(action
(system
"clang --target=x86_64-pc-windows-msvc -c %{deps} -o %{target} || x86_64-w64-mingw32-gcc -c %{deps} -o %{target}")))

; FAT binary testing - compare our tools with system tools
; FAT binary testing - compare our example tools with the system tools on a
; known multi-arch system binary.

(rule
(target xcrun.otool-f.expected)
Expand All @@ -118,18 +103,18 @@

(rule
(target xcrun.otool-f.output)
(deps %{bin:octool})
(deps %{exe:../../example/octool.exe})
(enabled_if
(and
(= %{system} "macosx")
(= %{architecture} "arm64")))
(action
(with-stdout-to
%{target}
(run %{bin:octool} -f /usr/bin/xcrun))))
(run %{exe:../../example/octool.exe} -f /usr/bin/xcrun))))

(rule
(alias runtest)
(alias integration)
(enabled_if
(and
(= %{system} "macosx")
Expand All @@ -150,18 +135,18 @@

(rule
(target xcrun.lipo-info.output)
(deps %{bin:oclipo})
(deps %{exe:../../example/oclipo.exe})
(enabled_if
(and
(= %{system} "macosx")
(= %{architecture} "arm64")))
(action
(with-stdout-to
%{target}
(run %{bin:oclipo} --info /usr/bin/xcrun))))
(run %{exe:../../example/oclipo.exe} --info /usr/bin/xcrun))))

(rule
(alias runtest)
(alias integration)
(enabled_if
(and
(= %{system} "macosx")
Expand All @@ -182,18 +167,18 @@

(rule
(target xcrun.lipo-archs.output)
(deps %{bin:oclipo})
(deps %{exe:../../example/oclipo.exe})
(enabled_if
(and
(= %{system} "macosx")
(= %{architecture} "arm64")))
(action
(with-stdout-to
%{target}
(run %{bin:oclipo} --archs /usr/bin/xcrun))))
(run %{exe:../../example/oclipo.exe} --archs /usr/bin/xcrun))))

(rule
(alias runtest)
(alias integration)
(enabled_if
(and
(= %{system} "macosx")
Expand All @@ -214,7 +199,7 @@

(rule
(target xcrun.lipo-verify_arch.output)
(deps %{bin:oclipo})
(deps %{exe:../../example/oclipo.exe})
(enabled_if
(and
(= %{system} "macosx")
Expand All @@ -223,15 +208,15 @@
(with-stdout-to
%{target}
(run
%{bin:oclipo}
%{exe:../../example/oclipo.exe}
--verify_arch
x86_64
--verify_arch
arm64e
/usr/bin/xcrun))))

(rule
(alias runtest)
(alias integration)
(enabled_if
(and
(= %{system} "macosx")
Expand Down Expand Up @@ -263,7 +248,7 @@
(run %{bin:file} /usr/bin/xcrun))))

(rule
(alias runtest)
(alias integration)
(enabled_if
(and
(= %{system} "macosx")
Expand All @@ -284,18 +269,18 @@

(rule
(target xcrun.otool-l.output)
(deps %{bin:octool})
(deps %{exe:../../example/octool.exe})
(enabled_if
(and
(= %{system} "macosx")
(= %{architecture} "arm64")))
(action
(with-stdout-to
%{target}
(run %{bin:octool} -l /usr/bin/xcrun))))
(run %{exe:../../example/octool.exe} -l /usr/bin/xcrun))))

(rule
(alias runtest)
(alias integration)
(enabled_if
(and
(= %{system} "macosx")
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions test/unit/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
; Unit tests: exercise the library against in-memory and embedded data.

(tests
(names test_object test_leb128 test_pe)
(libraries object integers alcotest))
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading