Skip to content

Commit aa9e845

Browse files
authored
Change semantics of DNSInput.saveActive() (#228)
Previously, DNSInput.saveActive() would return the active range without taking prior calls to setActive() into account. Add a test case that illustrates the failure that the user experienced, as well as a test case verifying DNSInput semantics when called recursively with ByteBuffers with limit and length set. Please see #225 for further details.
1 parent 71c7222 commit aa9e845

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/main/java/org/xbill/DNS/DNSInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void clearActive() {
7979

8080
/** Returns the position of the end of the current active region. */
8181
public int saveActive() {
82-
return limit - offset;
82+
return byteBuffer.limit() - offset;
8383
}
8484

8585
/**

src/test/java/org/xbill/DNS/DNSInputTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import static org.junit.jupiter.api.Assertions.assertEquals;
4040
import static org.junit.jupiter.api.Assertions.assertThrows;
4141

42+
import java.io.ByteArrayOutputStream;
43+
import java.io.IOException;
4244
import java.nio.ByteBuffer;
4345
import org.junit.jupiter.api.BeforeEach;
4446
import org.junit.jupiter.api.Test;
@@ -84,6 +86,19 @@ void setUp() {
8486
}
8587
}
8688

89+
static class DNSInputByteBufferLimitOffsetTest extends DNSInputBase {
90+
@BeforeEach
91+
void setUp() throws IOException {
92+
m_raw = new byte[] {0, 1, 2, 3, 4, 5, (byte) 255, (byte) 255, (byte) 255, (byte) 255};
93+
// create a new byte array with a prefix and a suffix to be ignored
94+
ByteArrayOutputStream out = new ByteArrayOutputStream();
95+
out.write(42);
96+
out.write(m_raw);
97+
out.write(47);
98+
m_di = new DNSInput(ByteBuffer.wrap(out.toByteArray(), 1, 10));
99+
}
100+
}
101+
87102
@Test
88103
void initial_state() {
89104
assertEquals(0, m_di.current());
@@ -319,4 +334,28 @@ void readCountedSting() throws WireParseException {
319334
assertEquals(3, m_di.current());
320335
assertEquals(2, out[0]);
321336
}
337+
338+
@Test
339+
void setActive_recursive() throws WireParseException {
340+
int outer = m_di.saveActive();
341+
m_di.setActive(3);
342+
343+
assertEquals(0x00, m_di.readU8());
344+
assertEquals(2, m_di.remaining());
345+
346+
int inner = m_di.saveActive();
347+
348+
m_di.setActive(1);
349+
assertArrayEquals(new byte[] {0x01}, m_di.readByteArray());
350+
351+
m_di.restoreActive(inner);
352+
353+
assertArrayEquals(new byte[] {0x02}, m_di.readByteArray());
354+
assertEquals(0, m_di.remaining());
355+
356+
m_di.restoreActive(outer);
357+
358+
assertEquals(0x03, m_di.readU8());
359+
assertEquals(6, m_di.remaining());
360+
}
322361
}

src/test/java/org/xbill/DNS/OPTRecordTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import static org.junit.jupiter.api.Assertions.assertThrows;
77
import static org.junit.jupiter.api.Assertions.assertTrue;
88

9+
import java.io.IOException;
10+
import java.util.Collections;
911
import org.junit.jupiter.api.Test;
12+
import org.xbill.DNS.utils.base16;
1013

1114
class OPTRecordTest {
1215

@@ -47,6 +50,24 @@ void rdataFromString() {
4750
assertTrue(thrown.getMessage().contains("no text format defined for OPT"));
4851
}
4952

53+
@Test
54+
void rdataFromWire() throws IOException {
55+
byte[] buf = base16.fromString("000029100000000000000C000A00084531D089BA80C6EB");
56+
OPTRecord record = (OPTRecord) OPTRecord.fromWire(new DNSInput(buf), Section.ADDITIONAL);
57+
assertEquals(
58+
Collections.singletonList(new CookieOption(base16.fromString("4531D089BA80C6EB"))),
59+
record.getOptions());
60+
}
61+
62+
@Test
63+
void rdataFromWire_nullPadded() throws IOException {
64+
byte[] buf = base16.fromString("000029100000000000000C000A00084531D089BA80C6EB00");
65+
OPTRecord record = (OPTRecord) OPTRecord.fromWire(new DNSInput(buf), Section.ADDITIONAL);
66+
assertEquals(
67+
Collections.singletonList(new CookieOption(base16.fromString("4531D089BA80C6EB"))),
68+
record.getOptions());
69+
}
70+
5071
private void assertNotEqual(final OPTRecord optRecordOne, final OPTRecord optRecordTwo) {
5172
assertFalse(optRecordOne.equals(optRecordTwo));
5273
assertFalse(optRecordTwo.equals(optRecordOne));

0 commit comments

Comments
 (0)