diff options
| author | Ajax <commial@gmail.com> | 2018-07-02 14:49:04 +0200 |
|---|---|---|
| committer | Ajax <commial@gmail.com> | 2018-07-03 14:28:18 +0200 |
| commit | 6ef8dbb2223d0847e3822b545b249511e96a1f9b (patch) | |
| tree | 1c4686d1a43e01d097a6229b4d3f1ba1bda2008c /test/core/locationdb.py | |
| parent | 88e2c5e6ca4f9c06db0ee72e263284dd465d2573 (diff) | |
| download | miasm-6ef8dbb2223d0847e3822b545b249511e96a1f9b.tar.gz miasm-6ef8dbb2223d0847e3822b545b249511e96a1f9b.zip | |
LocationDB: introduced to replace AsmSymbolPool
Diffstat (limited to 'test/core/locationdb.py')
| -rw-r--r-- | test/core/locationdb.py | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/test/core/locationdb.py b/test/core/locationdb.py new file mode 100644 index 00000000..b9a5f707 --- /dev/null +++ b/test/core/locationdb.py @@ -0,0 +1,108 @@ +from miasm2.core.locationdb import LocationDB + + +# Basic tests (LocationDB description) +loc_db = LocationDB() +loc_key1 = loc_db.add_location() +loc_key2 = loc_db.add_location(offset=0x1234) +loc_key3 = loc_db.add_location(name="first_name") +loc_db.add_location_name(loc_key3, "second_name") +loc_db.set_location_offset(loc_key3, 0x5678) +loc_db.remove_location_name(loc_key3, "second_name") + +assert loc_db.get_location_offset(loc_key1) is None +assert loc_db.get_location_offset(loc_key2) == 0x1234 + +assert loc_db.pretty_str(loc_key1) == str(loc_key1) +assert loc_db.pretty_str(loc_key2) == "loc_1234" +assert loc_db.pretty_str(loc_key3) == "first_name" +loc_db.consistency_check() + +# Offset manipulation +loc_key4 = loc_db.add_location() +assert loc_db.get_location_offset(loc_key4) is None +loc_db.set_location_offset(loc_key4, 0x1122) +assert loc_db.get_location_offset(loc_key4) == 0x1122 +loc_db.unset_location_offset(loc_key4) +assert loc_db.get_location_offset(loc_key4) is None +try: + loc_db.set_location_offset(loc_key4, 0x1234) + has_raised = False +except KeyError: + has_raised = True +assert has_raised +assert loc_db.get_location_offset(loc_key4) is None +loc_db.set_location_offset(loc_key4, 0x1122) +try: + loc_db.set_location_offset(loc_key4, 0x1123) + has_raised = False +except ValueError: + has_raised = True +assert has_raised +assert loc_db.get_location_offset(loc_key4) == 0x1122 +loc_db.set_location_offset(loc_key4, 0x1123, force=True) +assert loc_db.get_location_offset(loc_key4) == 0x1123 +assert 0x1123 in loc_db.offsets +try: + loc_db.add_location(offset=0x1123) + has_raised = False +except ValueError: + has_raised = True +assert loc_db.add_location(offset=0x1123, strict=False) == loc_key4 +assert loc_db.get_offset_location(0x1123) == loc_key4 +assert loc_db.get_or_create_offset_location(0x1123) == loc_key4 +loc_key4_bis = loc_db.get_or_create_offset_location(0x1144) +assert loc_db.get_offset_location(0x1144) == loc_key4_bis +loc_db.consistency_check() + +# Names manipulation +loc_key5 = loc_db.add_location() +name1 = "name1" +name2 = "name2" +name3 = "name3" +assert len(loc_db.get_location_names(loc_key5)) == 0 +loc_db.add_location_name(loc_key5, name1) +loc_db.add_location_name(loc_key5, name2) +assert name1 in loc_db.names +assert name2 in loc_db.names +assert name1 in loc_db.get_location_names(loc_key5) +assert name2 in loc_db.get_location_names(loc_key5) +assert loc_db.get_name_location(name1) == loc_key5 +loc_db.remove_location_name(loc_key5, name1) +assert name1 not in loc_db.names +assert name1 not in loc_db.get_location_names(loc_key5) +try: + loc_db.remove_location_name(loc_key5, name1) + has_raised = False +except KeyError: + has_raised = True +try: + loc_db.add_location_name(loc_key1, name2) + has_raised = False +except KeyError: + has_raised = True +try: + loc_db.add_location(name=name2) + has_raised = False +except ValueError: + has_raised = True +assert loc_db.add_location(name=name2, strict=False) == loc_key5 +assert loc_db.get_or_create_name_location(name2) == loc_key5 +loc_key5_bis = loc_db.get_or_create_name_location(name3) +assert loc_db.get_name_location(name3) == loc_key5_bis +loc_db.consistency_check() + +# Merge +loc_db2 = LocationDB() +loc_db2.add_location(offset=0x3344) +loc_db2.add_location(name=name2) +loc_db.merge(loc_db2) +assert 0x3344 in loc_db.offsets +assert name2 in loc_db.names +loc_db.consistency_check() +assert loc_db.get_name_location(name2) == loc_key5 + +# Delete +loc_db.remove_location(loc_key5) +assert loc_db.get_name_location(name2) is None +loc_db.consistency_check() |