1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-03-28 14:56:28 +01:00

Bug 562164 - Add JUnit tests for IMemoryExporter implementations

Added "number of units to retrieve" parameter to ReadMemory#from
Fixed result compare for text-based formats
Returning back 64KiB DATA_PER_TRANSFER optimization
Reworked FileExport from "O extends AutoCloseable"
Renamed ReadMemory to IReadMemory

Change-Id: Id7eb51015884d5dbffa5e91e9601f5e6ddb52d90
Signed-off-by: Alexander Fedorov <alexander.fedorov@arsysop.ru>
This commit is contained in:
Alexander Fedorov 2020-05-05 12:52:49 +03:00
parent e1f6300001
commit d374b71c94
14 changed files with 111 additions and 85 deletions

View file

@ -16,32 +16,56 @@ package org.eclipse.cdt.debug.core.memory.tests;
import java.math.BigInteger;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import org.eclipse.cdt.debug.core.memory.transport.ReadMemory;
import org.eclipse.cdt.debug.core.memory.transport.IReadMemory;
import org.eclipse.cdt.debug.core.memory.transport.WriteMemory;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.MemoryByte;
import org.eclipse.osgi.util.NLS;
/**
* Simple emulator of memory block to control the memory transport without real device
*
*/
final class EmulateMemory implements WriteMemory, ReadMemory {
final class EmulateMemory implements WriteMemory, IReadMemory {
private final BigInteger addressable;
private final BigInteger base;
//FIXME: needs improvements, assumes identical pattern during write and read
private final Map<BigInteger, byte[]> storage;
EmulateMemory(BigInteger base) {
storage = new LinkedHashMap<>();
EmulateMemory(BigInteger addressable, BigInteger base) {
this.addressable = addressable;
this.base = base;
this.storage = new LinkedHashMap<>();
}
@Override
public byte[] from(BigInteger offset) throws DebugException {
return Optional.ofNullable(storage.get(offset)).orElseThrow(() -> failed(offset));
public MemoryByte[] from(BigInteger offset, long units) throws DebugException {
int length = (int) (units * addressable.longValue());
MemoryByte[] result = new MemoryByte[length];
int i = 0;
while (i < length) {
byte[] raw = storage.getOrDefault(offset.add(BigInteger.valueOf(i)), new byte[0]);
int obtained = raw.length;
if (obtained > 0) {
for (int j = 0; j < obtained; j++) {
result[i + j] = new MemoryByte(raw[j]);
}
i = i + obtained;
} else {
//unreachable with current test data
MemoryByte unavailable = new MemoryByte();
unavailable.setReadable(false);
for (int j = i; j < length; j++) {
result[i + j] = unavailable;
}
i = length;
}
}
return result;
}
@Override

View file

@ -18,6 +18,7 @@ import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.function.Consumer;
import org.eclipse.cdt.debug.core.memory.transport.ExportRequest;
@ -58,7 +59,7 @@ public final class PlainTextTransportTest {
}
private void transport(String name, BigInteger end) throws CoreException, IOException {
EmulateMemory memory = new EmulateMemory(base);
EmulateMemory memory = new EmulateMemory(BigInteger.valueOf(1), base);
Consumer<BigInteger> scroll = new CollectScrolls();
File input = new InputFile(name).get();
new PlainTextImport(input, new ImportRequest(base, start, memory), scroll)//
@ -66,11 +67,11 @@ public final class PlainTextTransportTest {
File output = new OutputFile(name).get();
new PlainTextExport(output, new ExportRequest(start, end, BigInteger.ONE, memory))//
.run(new NullProgressMonitor());
Assert.assertArrayEquals(read(input), read(output));
Assert.assertEquals(read(input), read(output));
}
private byte[] read(File file) throws IOException {
return Files.readAllBytes(Paths.get(file.toString()));
private List<String> read(File file) throws IOException {
return Files.readAllLines(Paths.get(file.toString()));
}
}

View file

@ -58,7 +58,7 @@ public final class RAWBinaryTransportTest {
}
private void transport(String name, BigInteger end) throws CoreException, IOException {
EmulateMemory memory = new EmulateMemory(base);
EmulateMemory memory = new EmulateMemory(BigInteger.valueOf(1), base);
Consumer<BigInteger> scroll = new CollectScrolls();
File input = new InputFile(name).get();
new RAWBinaryImport(input, new ImportRequest(base, start, memory), scroll)//

View file

@ -18,6 +18,7 @@ import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.function.Consumer;
import org.eclipse.cdt.debug.core.memory.transport.ExportRequest;
@ -58,7 +59,7 @@ public final class SRecordTransportTest {
}
private void transport(String name, BigInteger end) throws CoreException, IOException {
EmulateMemory memory = new EmulateMemory(base);
EmulateMemory memory = new EmulateMemory(BigInteger.valueOf(1), base);
Consumer<BigInteger> scroll = new CollectScrolls();
File input = new InputFile(name).get();
File output = new OutputFile(name).get();
@ -74,8 +75,9 @@ public final class SRecordTransportTest {
Assert.assertArrayEquals(read(input), read(output));
}
private byte[] read(File file) throws IOException {
return Files.readAllBytes(Paths.get(file.toString()));
private String[] read(File file) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(file.toString()));
return lines.toArray(new String[lines.size()]);
}
}

View file

@ -27,9 +27,9 @@ public final class ExportRequest {
private final BigInteger start;
private final BigInteger end;
private final BigInteger addressable;
private final ReadMemory read;
private final IReadMemory read;
public ExportRequest(BigInteger start, BigInteger end, BigInteger addressable, ReadMemory read) {
public ExportRequest(BigInteger start, BigInteger end, BigInteger addressable, IReadMemory read) {
this.start = start;
this.end = end;
this.addressable = addressable;
@ -64,7 +64,7 @@ public final class ExportRequest {
*
* @return reader
*/
public ReadMemory read() {
public IReadMemory read() {
return read;
}
}

View file

@ -13,8 +13,11 @@
*******************************************************************************/
package org.eclipse.cdt.debug.core.memory.transport;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import org.eclipse.cdt.debug.internal.core.memory.transport.Messages;
@ -32,12 +35,12 @@ import org.osgi.framework.FrameworkUtil;
*
* @since 0.1
*/
public abstract class FileExport<O extends AutoCloseable> implements ICoreRunnable {
public abstract class FileExport implements ICoreRunnable {
protected final BigInteger start;
protected final BigInteger end;
protected final BigInteger addressable;
protected final ReadMemory read;
protected final IReadMemory read;
private final File file;
@ -51,7 +54,7 @@ public abstract class FileExport<O extends AutoCloseable> implements ICoreRunnab
@Override
public void run(IProgressMonitor monitor) throws CoreException {
try (O writer = output(file)) {
try (OutputStream output = output(file)) {
BigInteger jobs = end.subtract(start).divide(chunkSize());
BigInteger factor = BigInteger.ONE;
if (jobs.compareTo(BigInteger.valueOf(0x7FFFFFFF)) > 0) {
@ -59,7 +62,8 @@ public abstract class FileExport<O extends AutoCloseable> implements ICoreRunnab
jobs = jobs.divide(factor);
}
monitor.beginTask(Messages.FileExport_task_transferring, jobs.intValue());
transfer(writer, factor, monitor);
transfer(output, factor, monitor);
output.flush();
} catch (IOException ex) {
requestFailed(Messages.FileExport_e_write_file, ex);
} catch (DebugException ex) {
@ -72,13 +76,16 @@ public abstract class FileExport<O extends AutoCloseable> implements ICoreRunnab
}
/**
* Creates the writer for the given file
* Creates the output stream for the given file
*
* @param file to export to
* @return writer instance
* @throws IOException
*/
protected abstract O output(File file) throws IOException;
protected OutputStream output(File file) throws IOException {
file.getParentFile().mkdirs();
return new BufferedOutputStream(new FileOutputStream(file));
}
/**
* Determines the data chunk to use for export
@ -87,7 +94,7 @@ public abstract class FileExport<O extends AutoCloseable> implements ICoreRunnab
*/
protected abstract BigInteger chunkSize();
protected abstract void transfer(O output, BigInteger factor, IProgressMonitor monitor)
protected abstract void transfer(OutputStream output, BigInteger factor, IProgressMonitor monitor)
throws IOException, DebugException;
protected String transferring(BigInteger length, BigInteger address) {

View file

@ -16,21 +16,27 @@ package org.eclipse.cdt.debug.core.memory.transport;
import java.math.BigInteger;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.MemoryByte;
/**
* Reads an array of bytes using the given offset
*
* @since 0.1
*/
public interface ReadMemory {
public interface IReadMemory {
/**
* Reads an array of bytes from a memory starting from the given offset.
* Reads an array of bytes from a memory starting from the given offset. If requested to retrieve data beyond the memory
* boundaries, implementations should return memory bytes with the <code>READABLE</code> bit turned off for each byte outside the
* of the accessible range. An exception should not be thrown in this case.
*
* @param offset
* @return the obtained data
* @throws DebugException
* @param offset zero based offset at which to start retrieving bytes in terms of addressable units
* @param units the number of addressable units to retrieve
* @return the obtained data, {@link MemoryByte#isReadable()} needs to be checked
* @throws DebugException if unable to retrieve the specified bytes due to a failure communicating with the target
*
* @see {@link MemoryByte}
*/
byte[] from(BigInteger offset) throws DebugException;
MemoryByte[] from(BigInteger offset, long units) throws DebugException;
}

View file

@ -15,27 +15,22 @@
package org.eclipse.cdt.debug.internal.core.memory.transport;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import org.eclipse.cdt.debug.core.memory.transport.ExportRequest;
import org.eclipse.cdt.debug.core.memory.transport.FileExport;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.MemoryByte;
public final class PlainTextExport extends FileExport<FileWriter> {
public final class PlainTextExport extends FileExport {
public PlainTextExport(File output, ExportRequest request) {
super(output, request);
}
@Override
protected FileWriter output(File file) throws IOException {
file.getParentFile().mkdirs();
return new FileWriter(file);
}
@Override
protected BigInteger chunkSize() {
// These variables control how the output will be formatted
@ -49,7 +44,7 @@ public final class PlainTextExport extends FileExport<FileWriter> {
}
@Override
protected void transfer(FileWriter output, BigInteger factor, IProgressMonitor monitor)
protected void transfer(OutputStream output, BigInteger factor, IProgressMonitor monitor)
throws IOException, DebugException {
// These variables control how the output will be formatted
// The output data is split by chunks of 1 addressable unit size.
@ -69,17 +64,18 @@ public final class PlainTextExport extends FileExport<FileWriter> {
buf.append(" "); //$NON-NLS-1$
}
BigInteger from = transferAddress.add(dataCellSize.multiply(BigInteger.valueOf(i)));
byte[] bytes = read.from(from);
MemoryByte[] bytes = read.from(from, dataCellSize.longValue());
for (int byteIndex = 0; byteIndex < bytes.length; byteIndex++) {
String bString = BigInteger.valueOf(0xFF & bytes[byteIndex]).toString(16);
//FIXME: check MemoryByte#isReadable
String bString = BigInteger.valueOf(0xFF & bytes[byteIndex].getValue()).toString(16);
if (bString.length() == 1) {
buf.append("0"); //$NON-NLS-1$
}
buf.append(bString);
}
}
output.write(buf.toString().toUpperCase());
output.write("\n"); //$NON-NLS-1$
output.write(buf.toString().toUpperCase().getBytes());
output.write("\n".getBytes()); //$NON-NLS-1$
transferAddress = transferAddress.add(length);
jobCount = jobCount.add(BigInteger.ONE);
if (jobCount.compareTo(factor) == 0) {

View file

@ -15,33 +15,29 @@
package org.eclipse.cdt.debug.internal.core.memory.transport;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import org.eclipse.cdt.debug.core.memory.transport.ExportRequest;
import org.eclipse.cdt.debug.core.memory.transport.FileExport;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.MemoryByte;
public final class RAWBinaryExport extends FileExport<FileOutputStream> {
public final class RAWBinaryExport extends FileExport {
public RAWBinaryExport(File input, ExportRequest request) {
super(input, request);
}
@Override
protected FileOutputStream output(File file) throws IOException {
return new FileOutputStream(file);
}
@Override
protected BigInteger chunkSize() {
return BigInteger.valueOf(1024);
}
@Override
protected void transfer(FileOutputStream output, BigInteger factor, IProgressMonitor monitor)
protected void transfer(OutputStream output, BigInteger factor, IProgressMonitor monitor)
throws IOException, DebugException {
BigInteger transferAddress = start;
BigInteger jobCount = BigInteger.ZERO;
@ -52,8 +48,11 @@ public final class RAWBinaryExport extends FileExport<FileOutputStream> {
length = end.subtract(transferAddress);
}
monitor.subTask(transferring(length, transferAddress));
byte[] byteValues = read.from(transferAddress);
output.write(byteValues);
MemoryByte[] byteValues = read.from(transferAddress, length.longValue() / addressable.longValue());
for (MemoryByte memoryByte : byteValues) {
//FIXME: check MemoryByte#isReadable
output.write(memoryByte.getValue());
}
transferAddress = transferAddress.add(length);
jobCount = jobCount.add(BigInteger.ONE);
if (jobCount.compareTo(factor) == 0) {

View file

@ -15,7 +15,7 @@ package org.eclipse.cdt.debug.internal.core.memory.transport;
import java.math.BigInteger;
import org.eclipse.cdt.debug.core.memory.transport.ReadMemory;
import org.eclipse.cdt.debug.core.memory.transport.IReadMemory;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IMemoryBlockExtension;
import org.eclipse.debug.core.model.MemoryByte;
@ -24,24 +24,17 @@ import org.eclipse.debug.core.model.MemoryByte;
* Reads memory from the given {@link IMemoryBlockExtension}
*
*/
public final class ReadMemoryBlock implements ReadMemory {
public final class ReadMemoryBlock implements IReadMemory {
private final IMemoryBlockExtension memory;
private final long unit;
public ReadMemoryBlock(IMemoryBlockExtension memory) {
this.memory = memory;
this.unit = BigInteger.valueOf(1).longValue();
}
@Override
public byte[] from(BigInteger offset) throws DebugException {
MemoryByte[] received = memory.getBytesFromOffset(offset, unit);
byte[] bytes = new byte[received.length];
for (int i = 0; i < received.length; i++) {
bytes[i] = received[i].getValue();
}
return bytes;
public MemoryByte[] from(BigInteger offset, long units) throws DebugException {
return memory.getBytesFromOffset(offset, units);
}
}

View file

@ -15,26 +15,22 @@
package org.eclipse.cdt.debug.internal.core.memory.transport;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import org.eclipse.cdt.debug.core.memory.transport.ExportRequest;
import org.eclipse.cdt.debug.core.memory.transport.FileExport;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.MemoryByte;
public final class SRecordExport extends FileExport<FileWriter> {
public final class SRecordExport extends FileExport {
public SRecordExport(File input, ExportRequest request) {
super(input, request);
}
@Override
protected FileWriter output(File file) throws IOException {
return new FileWriter(file);
}
@Override
protected BigInteger chunkSize() {
// FIXME 4 byte default
@ -42,18 +38,19 @@ public final class SRecordExport extends FileExport<FileWriter> {
}
@Override
protected void transfer(FileWriter output, BigInteger factor, IProgressMonitor monitor)
protected void transfer(OutputStream output, BigInteger factor, IProgressMonitor monitor)
throws IOException, DebugException {
final BigInteger DATA_PER_RECORD = chunkSize();
final BigInteger DATA_PER_TRANSFER = BigInteger.valueOf(4096).multiply(DATA_PER_RECORD);
BigInteger jobCount = BigInteger.ZERO;
BigInteger transferAddress = start;
while (transferAddress.compareTo(end) < 0 && !monitor.isCanceled()) {
BigInteger length = DATA_PER_RECORD;
BigInteger length = DATA_PER_TRANSFER;
if (end.subtract(transferAddress).compareTo(length) < 0) {
length = end.subtract(transferAddress);
}
monitor.subTask(transferring(length, transferAddress));
byte[] bytes = read.from(transferAddress);
MemoryByte[] bytes = read.from(transferAddress, length.longValue() / addressable.longValue());
BigInteger sRecordAddress = transferAddress;
BigInteger sRecordEndAddress = transferAddress.add(length);
while (sRecordAddress.compareTo(sRecordEndAddress) < 0 && !monitor.isCanceled()) {
@ -61,7 +58,7 @@ public final class SRecordExport extends FileExport<FileWriter> {
if (sRecordEndAddress.subtract(sRecordAddress).compareTo(sRecordDataLength) < 0) {
sRecordDataLength = end.subtract(sRecordAddress);
}
output.write("S3"); // FIXME 4 byte address //$NON-NLS-1$
output.write("S3".getBytes()); // FIXME 4 byte address //$NON-NLS-1$
StringBuilder buf = new StringBuilder();
BigInteger sRecordLength = BigInteger.valueOf(4); // address size
@ -80,7 +77,8 @@ public final class SRecordExport extends FileExport<FileWriter> {
final int byteOffset = sRecordAddress.subtract(transferAddress).intValue();
final int byteLength = byteOffset + sRecordDataLength.intValue();
for (int byteIndex = byteOffset; byteIndex < byteLength; byteIndex++) {
String bString = BigInteger.valueOf(0xFF & bytes[byteIndex]).toString(16);
//FIXME: check MemoryByte#isReadable
String bString = BigInteger.valueOf(0xFF & bytes[byteIndex].getValue()).toString(16);
if (bString.length() == 1) {
buf.append("0"); //$NON-NLS-1$
}
@ -101,8 +99,8 @@ public final class SRecordExport extends FileExport<FileWriter> {
buf.append("0"); //$NON-NLS-1$
}
buf.append(bString);
output.write(buf.toString().toUpperCase());
output.write("\n"); //$NON-NLS-1$
output.write(buf.toString().toUpperCase().getBytes());
output.write("\n".getBytes()); //$NON-NLS-1$
sRecordAddress = sRecordAddress.add(sRecordDataLength);
jobCount = jobCount.add(BigInteger.ONE);
if (jobCount.compareTo(factor) == 0) {

View file

@ -18,7 +18,7 @@ import java.io.File;
import java.math.BigInteger;
import org.eclipse.cdt.debug.core.memory.transport.ExportRequest;
import org.eclipse.cdt.debug.core.memory.transport.ReadMemory;
import org.eclipse.cdt.debug.core.memory.transport.IReadMemory;
import org.eclipse.cdt.debug.internal.core.memory.transport.PlainTextExport;
import org.eclipse.cdt.debug.internal.core.memory.transport.ReadMemoryBlock;
import org.eclipse.cdt.debug.internal.core.memory.transport.TransportJob;
@ -451,7 +451,7 @@ public class PlainTextExporter implements IMemoryExporter {
@Override
public void exportMemory() {
ReadMemory read = new ReadMemoryBlock((IMemoryBlockExtension) fMemoryBlock);
IReadMemory read = new ReadMemoryBlock((IMemoryBlockExtension) fMemoryBlock);
BigInteger addressable = new AddressableSize((IMemoryBlockExtension) fMemoryBlock).get();
ExportRequest request = new ExportRequest(fStartAddress, fEndAddress, addressable, read);
PlainTextExport memoryExport = new PlainTextExport(fOutputFile, request);

View file

@ -18,7 +18,7 @@ import java.io.File;
import java.math.BigInteger;
import org.eclipse.cdt.debug.core.memory.transport.ExportRequest;
import org.eclipse.cdt.debug.core.memory.transport.ReadMemory;
import org.eclipse.cdt.debug.core.memory.transport.IReadMemory;
import org.eclipse.cdt.debug.internal.core.memory.transport.RAWBinaryExport;
import org.eclipse.cdt.debug.internal.core.memory.transport.ReadMemoryBlock;
import org.eclipse.cdt.debug.internal.core.memory.transport.TransportJob;
@ -454,7 +454,7 @@ public class RAWBinaryExporter implements IMemoryExporter {
@Override
public void exportMemory() {
ReadMemory read = new ReadMemoryBlock((IMemoryBlockExtension) fMemoryBlock);
IReadMemory read = new ReadMemoryBlock((IMemoryBlockExtension) fMemoryBlock);
BigInteger addressable = new AddressableSize((IMemoryBlockExtension) fMemoryBlock).get();
ExportRequest request = new ExportRequest(fStartAddress, fEndAddress, addressable, read);
RAWBinaryExport memoryExport = new RAWBinaryExport(fOutputFile, request);

View file

@ -19,7 +19,7 @@ import java.io.File;
import java.math.BigInteger;
import org.eclipse.cdt.debug.core.memory.transport.ExportRequest;
import org.eclipse.cdt.debug.core.memory.transport.ReadMemory;
import org.eclipse.cdt.debug.core.memory.transport.IReadMemory;
import org.eclipse.cdt.debug.internal.core.memory.transport.ReadMemoryBlock;
import org.eclipse.cdt.debug.internal.core.memory.transport.SRecordExport;
import org.eclipse.cdt.debug.internal.core.memory.transport.TransportJob;
@ -489,7 +489,7 @@ public class SRecordExporter implements IMemoryExporter {
@Override
public void exportMemory() {
ReadMemory read = new ReadMemoryBlock((IMemoryBlockExtension) fMemoryBlock);
IReadMemory read = new ReadMemoryBlock((IMemoryBlockExtension) fMemoryBlock);
BigInteger addressable = new AddressableSize((IMemoryBlockExtension) fMemoryBlock).get();
ExportRequest request = new ExportRequest(fStartAddress, fEndAddress, addressable, read);
SRecordExport memoryExport = new SRecordExport(fOutputFile, request);