[213] | 1 | #!/usr/bin/env python3 |
---|
[197] | 2 | |
---|
| 3 | import sys |
---|
[213] | 4 | import gc |
---|
[215] | 5 | import time |
---|
[197] | 6 | import pyregfi |
---|
| 7 | |
---|
[213] | 8 | def usage(): |
---|
| 9 | sys.stderr.write("USAGE: pyregfi-smoketest.py hive1 [hive2 ...]\n") |
---|
[197] | 10 | |
---|
| 11 | |
---|
[215] | 12 | # helper function |
---|
| 13 | def getCurrentPath(key): |
---|
| 14 | if key == None: |
---|
| 15 | return '' |
---|
| 16 | path = [] |
---|
| 17 | p = key |
---|
| 18 | while p != None: |
---|
| 19 | path.append(p.name) |
---|
| 20 | if p.is_root(): |
---|
| 21 | break |
---|
| 22 | else: |
---|
| 23 | p = p.get_parent() |
---|
| 24 | path.reverse() |
---|
| 25 | del path[0] |
---|
| 26 | |
---|
| 27 | return path |
---|
| 28 | |
---|
| 29 | |
---|
[213] | 30 | # Uses the HiveIterator to walk all keys |
---|
| 31 | # Gathers various (meaningless) statistics to exercise simple attribute access |
---|
| 32 | # and to hopefully smoke out any bugs that can be identified by changing stats |
---|
[216] | 33 | def iterTallyNames(hive): |
---|
[213] | 34 | key_count = 0 |
---|
| 35 | key_lens = 0 |
---|
| 36 | key_rawlens = 0 |
---|
| 37 | value_count = 0 |
---|
| 38 | value_lens = 0 |
---|
| 39 | value_rawlens = 0 |
---|
[197] | 40 | |
---|
[213] | 41 | for k in hive: |
---|
| 42 | key_count += 1 |
---|
| 43 | if k.name != None: |
---|
| 44 | key_lens += len(k.name) |
---|
| 45 | if k.name_raw != None: |
---|
| 46 | key_rawlens += len(k.name_raw) |
---|
[199] | 47 | |
---|
[213] | 48 | for v in k.values: |
---|
| 49 | value_count += 1 |
---|
| 50 | if v.name != None: |
---|
| 51 | value_lens += len(v.name) |
---|
| 52 | if v.name_raw != None: |
---|
| 53 | value_rawlens += len(v.name_raw) |
---|
[200] | 54 | |
---|
[214] | 55 | print(" Counts: keys=%d, values=%d" % (key_count, value_count)) |
---|
| 56 | print(" Total name length: keys=%d, values=%d" % (key_lens, value_lens)) |
---|
| 57 | print(" Total raw name lengths: keys=%d, values=%d" % (key_rawlens, value_rawlens)) |
---|
[200] | 58 | |
---|
[209] | 59 | |
---|
[215] | 60 | # For each key in the hive, this traverses the parent links up to the root, |
---|
| 61 | # recording the path as it goes, and then uses the subtree/descend method |
---|
| 62 | # to find the same key again, verifying it is the same. This test is currently |
---|
| 63 | # very slow because no key caching is used. |
---|
| 64 | def iterParentWalk(hive): |
---|
| 65 | i = 1 |
---|
| 66 | for k in hive: |
---|
| 67 | path = getCurrentPath(k) |
---|
| 68 | try: |
---|
| 69 | hive_iter = hive.subtree(path) |
---|
| 70 | if hive_iter.current_key() != k: |
---|
| 71 | print("WARNING: k != current_key for path '%s'." % path) |
---|
| 72 | else: |
---|
| 73 | i += 1 |
---|
| 74 | except Exception as e: |
---|
| 75 | print("WARNING: Could not decend to path '%s'.\nError:\n %s\n%s" % (path,e.args,e)) |
---|
| 76 | print(" Successfully tested paths on %d keys." % i) |
---|
[209] | 77 | |
---|
[215] | 78 | |
---|
[216] | 79 | # Uses the HiveIterator to walk all keys |
---|
| 80 | # Gathers various (meaningless) statistics about data/data_raw attributes |
---|
| 81 | def iterTallyData(hive): |
---|
| 82 | data_stat = 0.0 |
---|
| 83 | dataraw_stat = 0.0 |
---|
| 84 | |
---|
| 85 | for k in hive: |
---|
| 86 | for v in k.values: |
---|
| 87 | d = v.data |
---|
| 88 | if d == None: |
---|
| 89 | data_stat += 0.1 |
---|
| 90 | elif hasattr(d, "__len__"): |
---|
| 91 | data_stat += len(d) |
---|
| 92 | else: |
---|
| 93 | data_stat += d/2.0**64 |
---|
| 94 | |
---|
| 95 | d = v.data_raw |
---|
| 96 | if d == None: |
---|
| 97 | dataraw_stat += 0.1 |
---|
| 98 | else: |
---|
| 99 | dataraw_stat += len(d) |
---|
| 100 | |
---|
| 101 | print(" Data stat: %f" % data_stat) |
---|
| 102 | print(" Raw data stat: %f" % dataraw_stat) |
---|
| 103 | |
---|
| 104 | |
---|
[217] | 105 | recurseKey_stat = 0.0 |
---|
| 106 | recurseValue_stat = 0.0 |
---|
| 107 | def checkValues(key): |
---|
| 108 | global recurseKey_stat |
---|
| 109 | global recurseValue_stat |
---|
| 110 | recurseKey_stat += (key.mtime.low^key.mtime.high - key.max_bytes_subkeyname) * key.flags |
---|
| 111 | for v in key.values: |
---|
| 112 | recurseValue_stat += (v.data_off - v.data_size) / (1.0 + v.flags) + v.data_in_offset |
---|
| 113 | value = key.values[v.name] |
---|
| 114 | if v != value: |
---|
| 115 | print("WARNING: iterator value '%s' does not match dictionary value '%s'." |
---|
| 116 | % (v.name, value.name)) |
---|
[216] | 117 | |
---|
[217] | 118 | def recurseTree(cur, operation): |
---|
| 119 | for k in cur.subkeys: |
---|
| 120 | key = cur.subkeys[k.name] |
---|
| 121 | if k != key: |
---|
| 122 | print("WARNING: iterator subkey '%s' does not match dictionary subkey '%s'." |
---|
| 123 | % (k.name, key.name)) |
---|
| 124 | del key |
---|
| 125 | operation(k) |
---|
| 126 | recurseTree(k, operation) |
---|
| 127 | |
---|
| 128 | # Retrieves all keys by recursion, rather than the iterator, and validates |
---|
| 129 | # list dictionary access. Also builds nonsensical statistics as an excuse |
---|
| 130 | # to access various base structure attributes. |
---|
| 131 | def recurseKeyTally(hive): |
---|
| 132 | root = hive.get_root() |
---|
| 133 | checkValues(root) |
---|
| 134 | recurseTree(root, checkValues) |
---|
| 135 | print(" Key stat: %f" % recurseKey_stat) |
---|
| 136 | print(" Value stat: %f" % recurseValue_stat) |
---|
| 137 | |
---|
| 138 | |
---|
[213] | 139 | if len(sys.argv) < 2: |
---|
| 140 | usage() |
---|
| 141 | sys.exit(1) |
---|
[209] | 142 | |
---|
[215] | 143 | |
---|
[217] | 144 | #tests = [("iterTallyNames",iterTallyNames),("iterParentWalk",iterParentWalk),("iterTallyData",iterTallyData),] |
---|
| 145 | tests = [("recurseKeyTally",recurseKeyTally),] |
---|
[215] | 146 | |
---|
[213] | 147 | files = [] |
---|
| 148 | for f in sys.argv[1:]: |
---|
| 149 | files.append((f, open(f,"r+b"))) |
---|
| 150 | |
---|
| 151 | |
---|
[215] | 152 | start_time = time.time() |
---|
[213] | 153 | for hname,fh in files: |
---|
| 154 | hive = pyregfi.Hive(fh) |
---|
| 155 | for tname,t in tests: |
---|
[215] | 156 | teststart = time.time() |
---|
[213] | 157 | tstr = "'%s' on '%s'" % (tname,hname) |
---|
| 158 | print("##BEGIN %s:" % tstr) |
---|
| 159 | t(hive) |
---|
[215] | 160 | print("##END %s; runtime=%f; messages:" % (tstr, time.time() - teststart)) |
---|
[213] | 161 | print(pyregfi.GetLogMessages()) |
---|
| 162 | print |
---|
[215] | 163 | sys.stdout.flush() |
---|
[213] | 164 | |
---|
[215] | 165 | hive = None |
---|
[213] | 166 | files = None |
---|
| 167 | tests = None |
---|
| 168 | gc.collect() |
---|
[215] | 169 | print("### Tests Completed, runtime: %f ###" % (time.time() - start_time)) |
---|
[214] | 170 | #print(gc.garbage) |
---|