use of com.facebook.buck.charset.NulTerminatedCharsetDecoder in project buck by facebook.
the class SegmentCommandTest method testCreatingFromBytes32BitsLittleEndian.
@Test
public void testCreatingFromBytes32BitsLittleEndian() throws Exception {
ByteBuffer byteBuffer = bufferWithBytes(SegmentCommandTestData.getLittleEndian32Bits(), 10);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
SegmentCommand command = SegmentCommandUtils.createFromBuffer(byteBuffer, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()));
assertThat(command.getLoadCommandCommonFields().getOffsetInBinary(), equalTo(10));
SegmentCommandTestData.checkValues(command, false);
}
use of com.facebook.buck.charset.NulTerminatedCharsetDecoder in project buck by facebook.
the class SegmentCommandUtilsTest method testEnumeratingSectionsWorksRegardingOfCmdsize.
@Test
public void testEnumeratingSectionsWorksRegardingOfCmdsize() throws Exception {
// There was a bug when section's offset was computed incorrectly: offset = cmd.offset + cmdsize
// It was fixed roughly by the next formula: offset = cmd.offset + cmd.sizeOfHeader
// So this test checks this
final int sectionSize = SectionTestData.getBigEndian64Bit().length;
byte[] sectionData1 = SectionTestData.getBigEndian64Bit();
// offset = 1
sectionData1[51] = (byte) 0x01;
byte[] sectionData2 = SectionTestData.getBigEndian64Bit();
// sectname = "DECTNAME"
sectionData2[0] = (byte) 0x44;
// segname = "DEGNAME"
sectionData2[16] = (byte) 0x44;
// offset = 2
sectionData2[51] = (byte) 0x02;
byte[] sectionData3 = SectionTestData.getBigEndian64Bit();
// sectname = "LECTNAME"
sectionData3[0] = (byte) 0x4C;
// segname = "LEGNAME"
sectionData3[16] = (byte) 0x4C;
// offset = 3
sectionData3[51] = (byte) 0x03;
byte[] segmentBytes = SegmentCommandTestData.getBigEndian64Bits();
segmentBytes[6] = (byte) 0xAA;
// cmdsize = 0xAAFF == 43755 bytes!!!
segmentBytes[7] = (byte) 0xFF;
// nsects = 3
segmentBytes[67] = (byte) 0x03;
SegmentCommand command = SegmentCommandUtils.createFromBuffer(ByteBuffer.wrap(segmentBytes).order(ByteOrder.BIG_ENDIAN), new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()));
ByteBuffer buffer = ByteBuffer.allocate(command.getLoadCommandCommonFields().getCmdsize().intValue() + 3 * sectionSize);
buffer.order(ByteOrder.BIG_ENDIAN);
SegmentCommandUtils.writeCommandToBuffer(command, buffer, true);
buffer.put(sectionData1);
buffer.put(sectionData2);
buffer.put(sectionData3);
final List<Section> enumeratedSections = new ArrayList<>();
SegmentCommandUtils.enumerateSectionsInSegmentLoadCommand(buffer, new MachoMagicInfo(UnsignedInteger.fromIntBits(0xFEEDFACF)), command, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()), input -> {
enumeratedSections.add(input);
return Boolean.TRUE;
});
assertThat(enumeratedSections.size(), equalTo(3));
assertThat(enumeratedSections.get(0).getSectname(), equalToObject("SECTNAME"));
assertThat(enumeratedSections.get(0).getSegname(), equalToObject("SEGNAME"));
assertThat(enumeratedSections.get(0).getOffset(), equalToObject(UnsignedInteger.fromIntBits(0x01)));
assertThat(enumeratedSections.get(1).getSectname(), equalToObject("DECTNAME"));
assertThat(enumeratedSections.get(1).getSegname(), equalToObject("DEGNAME"));
assertThat(enumeratedSections.get(1).getOffset(), equalToObject(UnsignedInteger.fromIntBits(0x02)));
assertThat(enumeratedSections.get(2).getSectname(), equalToObject("LECTNAME"));
assertThat(enumeratedSections.get(2).getSegname(), equalToObject("LEGNAME"));
assertThat(enumeratedSections.get(2).getOffset(), equalToObject(UnsignedInteger.fromIntBits(0x03)));
}
use of com.facebook.buck.charset.NulTerminatedCharsetDecoder in project buck by facebook.
the class SymTabCommandUtilsTest method testGettingStringTableValueForNlist.
@Test
public void testGettingStringTableValueForNlist() throws Exception {
final String stringTableEntry = "string_table_entry";
byte[] nlistBytes = NlistTestData.getBigEndian64Bit();
// strx - first entry
nlistBytes[3] = (byte) (0x01);
byte[] commandBytes = SymTabCommandTestData.getBigEndian();
final int cmdSize = commandBytes.length;
// symoff
commandBytes[11] = (byte) cmdSize;
// nsyms
commandBytes[15] = (byte) 1;
// stroff
commandBytes[19] = (byte) (cmdSize + nlistBytes.length);
// strsize
commandBytes[23] = (byte) (stringTableEntry.length() + 1);
ByteBuffer byteBuffer = ByteBuffer.allocate(commandBytes.length + nlistBytes.length + 1 + stringTableEntry.length() + 1).order(ByteOrder.BIG_ENDIAN).put(commandBytes).put(nlistBytes).put((byte) 0x00).put(stringTableEntry.getBytes(StandardCharsets.UTF_8)).put((byte) 0x00);
byteBuffer.position(0);
SymTabCommand symTabCommand = SymTabCommandUtils.createFromBuffer(byteBuffer);
assertThat(symTabCommand.getSymoff(), equalToObject(UnsignedInteger.fromIntBits(cmdSize)));
assertThat(symTabCommand.getNsyms(), equalToObject(UnsignedInteger.fromIntBits(1)));
byteBuffer.position(cmdSize);
Nlist nlist = NlistUtils.createFromBuffer(byteBuffer, true);
assertThat(nlist.getN_strx(), equalToObject(UnsignedInteger.fromIntBits(1)));
String result = SymTabCommandUtils.getStringTableEntryForNlist(byteBuffer, symTabCommand, nlist, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()));
assertThat(result, equalToObject(stringTableEntry));
}
use of com.facebook.buck.charset.NulTerminatedCharsetDecoder in project buck by facebook.
the class UnixArchiveTest method testReadingArchive.
@Test
public void testReadingArchive() throws IOException {
Path path = tmp.resolve("test_archive.a");
createArchiveAtPath(path);
UnixArchive archive = new UnixArchive(FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE), new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()));
assertThat(archive.getEntries().size(), equalTo(3));
assertThat(archive.getEntries().get(0).getFileName(), equalToObject("__.SYMDEF SORTED"));
assertThat(archive.getEntries().get(0).getFileModificationTimestamp(), equalTo(1463146035L));
assertThat(archive.getEntries().get(0).getOwnerId(), equalTo(10727));
assertThat(archive.getEntries().get(0).getGroupId(), equalTo(11706));
assertThat(archive.getEntries().get(0).getFileMode(), equalTo(100644));
assertThat(archive.getEntries().get(1).getFileName(), equalToObject("file1.txt"));
assertThat(archive.getEntries().get(1).getFileModificationTimestamp(), equalTo(1463145840L));
assertThat(archive.getEntries().get(1).getFileSize(), equalTo(16L));
MappedByteBuffer buffer1 = archive.getMapForEntry(archive.getEntries().get(1));
byte[] strBytes1 = new byte[(int) archive.getEntries().get(1).getFileSize()];
buffer1.get(strBytes1, 0, (int) archive.getEntries().get(1).getFileSize());
assertThat(new String(strBytes1), equalToObject("FILE1_CONTENTS\n\n"));
assertThat(archive.getEntries().get(2).getFileName(), equalToObject("file2.txt"));
assertThat(archive.getEntries().get(2).getFileModificationTimestamp(), equalTo(1463145848L));
assertThat(archive.getEntries().get(2).getFileSize(), equalTo(32L));
MappedByteBuffer buffer2 = archive.getMapForEntry(archive.getEntries().get(2));
byte[] strBytes2 = new byte[(int) archive.getEntries().get(2).getFileSize()];
buffer2.get(strBytes2, 0, (int) archive.getEntries().get(2).getFileSize());
assertThat(new String(strBytes2), equalToObject("FILE2_CONTENTS_FILE2_CONTENTS\n\n\n"));
archive.close();
}
use of com.facebook.buck.charset.NulTerminatedCharsetDecoder in project buck by facebook.
the class UnixArchiveTest method testModifyingArchive.
@Test
public void testModifyingArchive() throws IOException {
Path path = tmp.resolve("test_archive.a");
createArchiveAtPath(path);
UnixArchive archive = new UnixArchive(FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE), new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()));
MappedByteBuffer buffer = archive.getMapForEntry(archive.getEntries().get(1));
byte[] bytes = new byte[(int) archive.getEntries().get(1).getFileSize()];
buffer.get(bytes, 0, (int) archive.getEntries().get(1).getFileSize());
assertThat(new String(bytes), equalToObject("FILE1_CONTENTS\n\n"));
buffer.position(0);
buffer.put("NEW_CONTENTS!!!!".getBytes(Charsets.UTF_8));
archive.close();
String updatedContents = new String(Files.readAllBytes(path));
assertThat(updatedContents, not(containsString("FILE1_CONTENTS")));
assertThat(updatedContents, containsString("NEW_CONTENTS!!!!"));
}
Aggregations