#!/usr/bin/env elixir # Test script to verify Field 52 (PIN Data) binary handling fix IO.puts("=== Testing Field 52 Binary Data Fix ===\n") # Add the project to the code path Mix.start() Mix.env(:dev) Code.require_file("lib/da_product_app/mercury_iso8583/packagers/iso_component.ex") Code.require_file("lib/da_product_app/mercury_iso8583/packagers/field_packagers/ifb_binary.ex") Code.require_file("lib/da_product_app/mercury_iso8583/packagers/field_packagers/base_field_packager.ex") Code.require_file("lib/da_product_app/mercury_iso8583/packagers/interpreters/binary_interpreter.ex") Code.require_file("lib/da_product_app/mercury_iso8583/packagers/padders/null_padder.ex") Code.require_file("lib/da_product_app/mercury_iso8583/packagers/prefixers/null_prefixer.ex") alias DaProductApp.MercuryISO8583.Packagers.ISOComponent alias DaProductApp.MercuryISO8583.Packagers.FieldPackagers.IFB_BINARY alias DaProductApp.MercuryISO8583.Packagers.FieldPackagers.BaseFieldPackager # Test data - Field 52 PIN data (8 bytes binary) original_pin_data = <<0xA1, 0x02, 0xEB, 0xF1, 0xAF, 0x3B, 0xA7, 0x15>> expected_hex = "A102EBF1AF3BA715" IO.puts("Test 1: Original PIN Data") IO.puts(" Binary: #{inspect(original_pin_data)}") IO.puts(" Hex: #{Base.encode16(original_pin_data)}") IO.puts(" Expected: #{expected_hex}") IO.puts("") # Create IFB_BINARY packager for Field 52 (8 bytes) packager = IFB_BINARY.new(8, "PIN Data") IO.puts("Test 2: Pack Binary Field") IO.puts(" Packager: #{inspect(packager)}") # Create ISOComponent with binary value component = %ISOComponent{ field_number: 52, value: original_pin_data, packager: packager } IO.puts(" Component value: #{inspect(component.value)}") IO.puts(" Component value hex: #{Base.encode16(component.value)}") # Pack the component case BaseFieldPackager.pack(component) do {:ok, packed_data} -> packed_hex = Base.encode16(packed_data) IO.puts("\n✅ Pack successful!") IO.puts(" Packed data: #{inspect(packed_data)}") IO.puts(" Packed hex: #{packed_hex}") IO.puts(" Packed size: #{byte_size(packed_data)} bytes") if byte_size(packed_data) == 8 do IO.puts(" ✅ Size correct (8 bytes)") else IO.puts(" ❌ Size incorrect (expected 8 bytes, got #{byte_size(packed_data)})") end if packed_hex == expected_hex do IO.puts(" ✅ Hex matches expected value") else IO.puts(" ❌ Hex doesn't match!") IO.puts(" Expected: #{expected_hex}") IO.puts(" Got: #{packed_hex}") end # Test unpacking IO.puts("\nTest 3: Unpack Binary Field") empty_component = %ISOComponent{field_number: 52, packager: packager} case BaseFieldPackager.unpack_with_packager(empty_component, packed_data, 0, packager) do {:ok, {unpacked_component, bytes_consumed}} -> unpacked_hex = Base.encode16(unpacked_component.value) IO.puts(" ✅ Unpack successful!") IO.puts(" Unpacked value: #{inspect(unpacked_component.value)}") IO.puts(" Unpacked hex: #{unpacked_hex}") IO.puts(" Bytes consumed: #{bytes_consumed}") if unpacked_hex == expected_hex do IO.puts(" ✅ Round-trip successful - data preserved!") else IO.puts(" ❌ Round-trip failed - data corrupted!") end {:error, reason} -> IO.puts(" ❌ Unpack failed: #{inspect(reason)}") end {:error, reason} -> IO.puts("\n❌ Pack failed: #{inspect(reason)}") end IO.puts("\n=== Test Complete ===")