Download test/binary_parser_test.exs from Snapkitty/forge-code: direct link, hf CLI and curl.
- Browser
- Download file 10.9 kB
-
https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/test/binary_parser_test.exs
- Command line
-
hf download hf://datasets/Snapkitty/forge-code/test/binary_parser_test.exs
-
curl -L -o binary_parser_test.exs https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/test/binary_parser_test.exs
10.9 kB
| defmodule Forge.BinaryParserTest do | |
| use ExUnit.Case | |
| alias Forge.Binary.Parser | |
| alias Forge.Binary.Field | |
| @moduletag :binary_parser | |
| describe "Forge.Binary.Parser" do | |
| @tag :basic | |
| test "parse valid BEAM file" do | |
| code = """ | |
| defmodule BinaryParserModule do | |
| def test, do: :ok | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| assert parsed.path == beam_path | |
| assert is_binary(parsed.raw) | |
| assert byte_size(parsed.raw) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :magic_number | |
| test "validate BEAM magic number" do | |
| code = "defmodule MagicModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| assert parsed.magic == :beam || parsed.magic == 0x6265616D | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :version | |
| test "detect BEAM version" do | |
| code = "defmodule VersionModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| assert parsed.version in [:v1_0, :v0_9, :v0_8, :unknown] | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :sections | |
| test "parse sections from BEAM file" do | |
| code = """ | |
| defmodule SectionModule do | |
| def func, do: :ok | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| assert is_list(parsed.sections) | |
| assert length(parsed.sections) > 0 | |
| # Check for common sections | |
| section_names = Enum.map(parsed.sections, & &1.name) | |
| assert :code in section_names || :Code in section_names | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :fields | |
| test "parse fields with offset preservation" do | |
| code = """ | |
| defmodule FieldModule do | |
| def test_func, do: 42 | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| # Check that sections have fields | |
| with_fields = Enum.reject(parsed.sections, &(&1.fields == [])) | |
| assert length(with_fields) > 0 | |
| # Check offset preservation | |
| Enum.each(with_fields, fn section -> | |
| Enum.each(section.fields, fn field -> | |
| assert is_integer(field.offset) | |
| assert field.offset >= 0 | |
| assert is_integer(field.length) | |
| assert field.length > 0 | |
| end) | |
| end) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :chunk_parsing | |
| test "parse chunks using beam_lib" do | |
| code = """ | |
| defmodule ChunkModule do | |
| def func, do: :ok | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| # Should have parsed chunks | |
| assert is_list(parsed.chunks) | |
| assert length(parsed.chunks) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :error_handling | |
| test "handle file not found" do | |
| assert {:error, :enoent, _} = Parser.parse_file("nonexistent.beam") | |
| end | |
| @tag :error_handling | |
| test "handle invalid BEAM file" do | |
| {:ok, path} = create_temp_file("not a beam file", ".beam") | |
| assert {:error, _, _} = Parser.parse_file(path) | |
| cleanup_temp([path]) | |
| end | |
| @tag :error_handling | |
| test "handle unsupported BEAM version" do | |
| # Create a file with invalid magic | |
| {:ok, path} = create_temp_file("INVALIDBEAMFILE", ".beam") | |
| # This should either fail or be parsed as unknown | |
| result = Parser.parse_file(path) | |
| assert result == {:error, _, _} || result[:ok].version == :unknown | |
| cleanup_temp([path]) | |
| end | |
| @tag :metadata | |
| test "extract metadata from BEAM" do | |
| code = """ | |
| defmodule MetadataParserModule do | |
| @vsn "1.0.0" | |
| def start, do: :ok | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| assert parsed.module | |
| assert is_atom(parsed.module) || is_binary(parsed.module) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :checksum | |
| test "validate BEAM checksum if present" do | |
| code = "defmodule ChecksumModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| # Checksum validation should be attempted | |
| # (Note: modern BEAM files may not have checksums) | |
| assert parsed.checksum_valid in [true, false, nil] | |
| cleanup_temp([path, beam_path]) | |
| end | |
| end | |
| describe "Forge.Binary.Field" do | |
| @tag :field_creation | |
| test "create binary field" do | |
| field = %Field{ | |
| section: :code, | |
| offset: 1024, | |
| length: 4, | |
| bytes: <<1, 2, 3, 4>>, | |
| semantic: :instruction_word, | |
| state: :raw, | |
| confidence: :high | |
| } | |
| assert field.section == :code | |
| assert field.offset == 1024 | |
| assert field.length == 4 | |
| assert field.bytes == <<1, 2, 3, 4>> | |
| assert field.semantic == :instruction_word | |
| assert field.state == :raw | |
| assert field.confidence == :high | |
| end | |
| @tag :field_states | |
| test "field state transitions" do | |
| field = %Field{section: :code, offset: 0, length: 1} | |
| # Transition from raw to decoded | |
| decoded_field = Field.put_state(field, :decoded) | |
| assert decoded_field.state == :decoded | |
| # Transition from decoded to normalized | |
| normalized_field = Field.put_state(decoded_field, :normalized) | |
| assert normalized_field.state == :normalized | |
| # Transition from normalized to semantic | |
| semantic_field = Field.put_state(normalized_field, :semantic) | |
| assert semantic_field.state == :semantic | |
| end | |
| @tag :field_confidence | |
| test "field confidence levels" do | |
| field = %Field{} | |
| high_confidence = Field.put_confidence(field, :high) | |
| assert high_confidence.confidence == :high | |
| medium_confidence = Field.put_confidence(field, :medium) | |
| assert medium_confidence.confidence == :medium | |
| low_confidence = Field.put_confidence(field, :low) | |
| assert low_confidence.confidence == :low | |
| end | |
| @tag :field_validation | |
| test "validate field struct" do | |
| field = %Field{ | |
| section: :code, | |
| offset: 0, | |
| length: 4, | |
| bytes: <<1, 2, 3, 4>> | |
| } | |
| assert Field.valid?(field) | |
| # Invalid field (negative offset) | |
| invalid_field = %Field{field | offset: -1} | |
| refute Field.valid?(invalid_field) | |
| # Invalid field (negative length) | |
| invalid_field2 = %Field{field | length: -1} | |
| refute Field.valid?(invalid_field2) | |
| # Invalid field (length > bytes) | |
| invalid_field3 = %Field{field | length: 10, bytes: <<1>>} | |
| refute Field.valid?(invalid_field3) | |
| end | |
| @tag :field_equality | |
| test "field equality" do | |
| field1 = %Field{section: :code, offset: 0, length: 4, bytes: <<1, 2, 3, 4>>} | |
| field2 = %Field{section: :code, offset: 0, length: 4, bytes: <<1, 2, 3, 4>>} | |
| field3 = %Field{section: :code, offset: 1, length: 4, bytes: <<1, 2, 3, 4>>} | |
| assert field1 == field2 | |
| refute field1 == field3 | |
| end | |
| end | |
| describe "Binary Parsing Features" do | |
| @tag :offset_preservation | |
| test "preserve offsets through parsing" do | |
| code = """ | |
| defmodule OffsetModule do | |
| def func1, do: 1 | |
| def func2, do: 2 | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| # Check that all fields have valid offsets | |
| Enum.each(parsed.sections, fn section -> | |
| Enum.each(section.fields, fn field -> | |
| assert field.offset >= 0 | |
| assert field.offset + field.length <= byte_size(parsed.raw) | |
| end) | |
| end) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :section_parsing | |
| test "parse all section types" do | |
| code = """ | |
| defmodule AllSectionsModule do | |
| @const 42 | |
| def func(x), do: x + @const | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| # Should have multiple section types | |
| section_types = Enum.map(parsed.sections, & &1.name) | |
| assert length(Enum.uniq(section_types)) > 1 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :field_decoding | |
| test "decode fields from binary" do | |
| code = """ | |
| defmodule DecodeModule do | |
| def test, do: 123 | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| # Check that some fields are decoded | |
| decoded_fields = Enum.filter(parsed.sections, &(&1.fields != [])) | |
| assert length(decoded_fields) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :endianness | |
| test "handle endianness correctly" do | |
| code = "defmodule EndianModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| # BEAM files should be in native endianness | |
| assert parsed.endianness in [:native, :big, :little] | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :provenance | |
| test "preserve provenance in parsing" do | |
| code = "defmodule ProvenanceModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, parsed} = Parser.parse_file(beam_path) | |
| # Should have file path | |
| assert parsed.path == beam_path | |
| cleanup_temp([path, beam_path]) | |
| end | |
| end | |
| # Helper functions | |
| defp create_temp_file(content, extension) do | |
| temp_dir = Path.join(System.tmp_dir(), "forge_test") | |
| File.mkdir_p!(temp_dir) | |
| name = "test_" <> Integer.to_string(:erlang.unique_integer([:positive])) <> extension | |
| path = Path.join(temp_dir, name) | |
| File.write!(path, content) | |
| {:ok, path} | |
| end | |
| defp compile_to_beam(path) do | |
| output_dir = Path.join(System.tmp_dir(), "forge_test", "ebin") | |
| File.mkdir_p!(output_dir) | |
| # Compile the file | |
| {:ok, module, _binary} = Code.eval_file(path) | |
| # Get the beam path | |
| beam_name = Atom.to_string(module) <> ".beam" | |
| beam_path = Path.join(output_dir, beam_name) | |
| # Force compilation to beam file | |
| Code.compile_file(path, beam_path, [:return_errors]) | |
| {:ok, beam_path} | |
| rescue | |
| _ -> {:error, :compilation_failed} | |
| end | |
| defp cleanup_temp(files) do | |
| Enum.each(files, &File.rm!/1) | |
| end | |
| end | |