draugr.git


draugr

~ single ELF file analysis tool
git clone https://github.com/djnnvx/draugr.git
log | files | refs | archive | README

json_test.odin (1694B)

      1 package elf
      2 
      3 import "core:encoding/json"
      4 import "core:testing"
      5 
      6 // build_json_dump and checksec both use the temp allocator; a free_all inside
      7 // either one frees the other's slices, which crashed the whole --json path.
      8 @(test)
      9 test_json_dump_survives_checksec_allocations :: proc(t: ^testing.T) {
     10 	data := build_dynamic_elf(true, true, DEFAULT_DYN_ENTRIES[:])
     11 	defer delete(data)
     12 
     13 	info, _ := load(data)
     14 	defer destroy(&info)
     15 
     16 	append(&info.dyn_symbols, Symbol{name = "__memcpy_chk"})
     17 	append(&info.dyn_symbols, Symbol{name = "memcpy"})
     18 
     19 	dump := build_json_dump(&info, context.temp_allocator)
     20 	defer free_all(context.temp_allocator)
     21 
     22 	testing.expect_value(t, len(dump.needed), 2)
     23 	testing.expect_value(t, dump.needed[0], "libtest.so.1")
     24 	testing.expect_value(t, dump.runpath, "/opt/runpath")
     25 	testing.expect_value(t, dump.checksec.fortified, 1)
     26 
     27 	out, err := json.marshal(dump, {pretty = true}, context.temp_allocator)
     28 	testing.expect(t, err == nil, "marshal must succeed")
     29 	testing.expect(t, len(out) > 0, "marshal must produce output")
     30 }
     31 
     32 @(test)
     33 test_json_dump_round_trips_through_parser :: proc(t: ^testing.T) {
     34 	data := build_dynamic_elf(true, true, DEFAULT_DYN_ENTRIES[:])
     35 	defer delete(data)
     36 
     37 	info, _ := load(data)
     38 	defer destroy(&info)
     39 
     40 	dump := build_json_dump(&info, context.temp_allocator)
     41 	out, _ := json.marshal(dump, {}, context.temp_allocator)
     42 	defer free_all(context.temp_allocator)
     43 
     44 	parsed, perr := json.parse(out, allocator = context.temp_allocator)
     45 	testing.expect(t, perr == nil, "emitted JSON must parse back")
     46 
     47 	obj := parsed.(json.Object)
     48 	testing.expect_value(t, obj["soname"].(json.String), "libtest.so.1")
     49 	testing.expect_value(t, obj["class"].(json.Float), 64)
     50 }