def run_test(ctx, reader, writer, address, data, stratagem): # Just to avoid triggering our warning to users wr_data = data if stratagem is None else None if not isinstance(reader, DataAbortMemoryReader): ctx.write_memory(address, wr_data, impl=writer, stratagem=stratagem) else: _da_read_pre_info['writer'] = writer _da_read_pre_info['data'] = data _da_read_pre_info['address'] = address data_read = ctx.read_memory(address, len(data), impl=reader) if data != data_read: now = round(time.time()) wfile = '{}.{}.bin'.format(writer.name, now) rfile = '{}.{}.bin'.format(reader.name, now) msg = 'Test case failed. Saving written and readback data to:' + linesep msg += ' {} and {}'.format(wfile, rfile) log.error(msg) fail_dir = create_resource_dir('memory_test', 'failure') filename = os.path.join(fail_dir, wfile) with open(filename, 'wb') as outfile: outfile.write(data) filename = os.path.join(fail_dir, rfile) with open(filename, 'wb') as outfile: outfile.write(data_read) return 'Fail' return 'Pass'
def test_find_fdt(state: dict, script: str): """ Exercises depthcharge-find-fdt """ output_dir = create_resource_dir(os.path.join(state['test_dir'], 'fdt')) dts_file = os.path.join(output_dir, 'test.dts.input') with open(dts_file, mode='w') as outfile: outfile.write(_DTS) args = [_DTC, '-q', '-I', 'dts', '-O', 'dtb', dts_file] sub = run_script(args, arch=None, capture_output=True) dtb = sub.stdout image_file = os.path.join(output_dir, 'image.bin') with open(image_file, 'wb') as outfile: outfile.write(random_pattern(412, seed=0)) outfile.write(dtb) outfile.write(random_pattern(123, seed=1)) outfile.write(dtb) outfile.write(random_pattern(4500, seed=2)) outfile.write(dtb) outfile.write(random_pattern(150, seed=3)) # Print only args = [script, '-f', image_file] run_script(args, stdout=DEVNULL) outpfx = os.path.join(output_dir, 'test') # Save DTB and DTS files args = [script, '-f', image_file, '-o', outpfx, '-a', '0x8000_0000'] run_script(args, stdout=DEVNULL) # Save DTB only args = [ script, '-f', image_file, '-o', outpfx, '-a', '0x9000_0000', '--no-dts' ] run_script(args, stdout=DEVNULL) # Save DTS only args = [ script, '-f', image_file, '-o', outpfx, '-a', '0xa000_0000', '--no-dtb' ] run_script(args, stdout=DEVNULL) # Count results results = {'dtb': 0, 'dts': 0} for _, _, filenames in os.walk(output_dir): for f in filenames: if f.endswith('.dts'): results['dts'] += 1 elif f.endswith('.dtb'): results['dtb'] += 1 assert results['dtb'] == 6 assert results['dts'] == 6
def load_state(args): if args.state is None: state = {} state['test_dir'] = create_resource_dir('launch_scripts-' + now_str()) state['config_file'] = os.path.join(state['test_dir'], 'test.cfg') # We'll populate this during test_inspect state['config'] = None return state with open(args.state, 'rb') as infile: return pickle.load(infile)
def test_mkenv(state: dict, script: str): """ Convert a textual environment to binary form. """ output_dir = create_resource_dir(os.path.join(state['test_dir'], 'env')) text_env = os.path.join(output_dir, 'env.txt') with open(text_env, 'w') as outfile: outfile.write(_ENV_TEXT) # Do this twice just to exercise all the arg forms args = [ script, '-H', '-S', '0x1000', '-f', text_env, '-o', os.path.join(output_dir, 'env_no_hdr.1.bin') ] run(args, stdout=DEVNULL, check=True) args = [ script, '--no-hdr', '--size', '8K', '--flags', '0xa', # Gets ignored due to --no-hdr '-f', text_env, '-o', os.path.join(output_dir, 'env_no_hdr.2.bin') ] run(args, stdout=DEVNULL, check=True) args = [ script, '--size', '1K', '-f', text_env, '-o', os.path.join(output_dir, 'env.bin') ] run(args, stdout=DEVNULL, check=True) args = [ script, '-S', '0x1000', '-F', '0xa', '-f', text_env, '-o', os.path.join(output_dir, 'env_flags_0xa.bin') ] run(args, stdout=DEVNULL, check=True)
def test_print(state: dict, script: str): """ Exercise depthcharge-print. Requires state['config_file']. """ items = ('all', 'arch', 'commands', 'commands=detail', 'env', 'env=expand', 'gd', 'version') output_dir = create_resource_dir(os.path.join(state['test_dir'], 'print')) for item in items: args = [script, '-c', state['config_file'], '-i', item] filename = os.path.join(output_dir, item.replace(':', '_')) log.note(' Printing ' + item + ' > ' + filename) with open(filename, 'w') as outfile: run_script(args, stdout=outfile)
def test_find_env(state: dict, script: str): """ Locate environments created by test_mkenv in a binar blob. """ output_dir = create_resource_dir(os.path.join(state['test_dir'], 'env')) blobfile = os.path.join(output_dir, 'blob.bin') with open(blobfile, 'wb') as outfile: outfile.write(random_pattern(31, seed=0) + b'\x00') with open(os.path.join(output_dir, 'env_no_hdr.1.bin'), 'rb') as infile: data = infile.read() outfile.write(data) outfile.write(random_pattern(63, seed=1) + b'\x00') with open(os.path.join(output_dir, 'env_no_hdr.2.bin'), 'rb') as infile: data = infile.read() outfile.write(data) outfile.write(random_pattern(1023, seed=2) + b'\x00') with open(os.path.join(output_dir, 'env_flags_0xa.bin'), 'rb') as infile: data = infile.read() outfile.write(data) outfile.write(random_pattern(3, seed=3) + b'\x00') with open(os.path.join(output_dir, 'env.bin'), 'rb') as infile: data = infile.read() outfile.write(data) outfile.write(random_pattern(64, seed=4) + b'\x00') test_dir = create_resource_dir(os.path.join(state['test_dir'], 'find_env')) filename_pfx = os.path.join(test_dir, 'env') # Expanded text args = [ script, '-A', 'arm', '-a', '0x8004_0000', '-E', '-f', blobfile, '-o', filename_pfx, ] run(args, stdout=DEVNULL, check=True) # Expanded text dup, default arch and addr args = [ script, '--expand', '-f', blobfile, '-o', filename_pfx, ] run(args, stdout=DEVNULL, check=True) # Text args = [ script, '-f', blobfile, '-o', filename_pfx, ] run(args, stdout=DEVNULL, check=True) args = [ script, '-B', '-f', blobfile, '-o', filename_pfx, ] run(args, stdout=DEVNULL, check=True) # Dup of the above, just long-form args args = [ script, '--binary', '--file', blobfile, '--outfile', filename_pfx, ] run(args, stdout=DEVNULL, check=True) results = { 'high_exp_text': 0, 'low_exp_text': 0, 'text': 0, 'bin': 0 } for root, _, filenames in os.walk(test_dir): for filename in filenames: if filename.endswith('.bin'): results['bin'] += 1 elif filename.endswith('exp.txt'): if '0x8' in filename: results['high_exp_text'] += 1 else: results['low_exp_text'] += 1 elif filename.endswith('.txt'): results['text'] += 1 else: raise RuntimeError('Unexpected condition') for key, value in results.items(): if value != 4: msg = '{:s}: Expected 4, got {:d}'.format(key, value) raise ValueError(msg)