|
| 1 | +/* |
| 2 | + * Copyright 2026 The Netty Project |
| 3 | + * |
| 4 | + * The Netty Project licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package io.netty.handler.codec.dns; |
| 17 | + |
| 18 | +import io.netty.buffer.ByteBuf; |
| 19 | +import io.netty.buffer.Unpooled; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | + |
| 24 | +public class AbstractDnsOptPseudoRrRecordTest { |
| 25 | + |
| 26 | + // DNSSEC OK, see https://tools.ietf.org/html/rfc3225#section-3 |
| 27 | + private static final int DO = 0x8000; |
| 28 | + |
| 29 | + // Compact Answers OK, see https://www.rfc-editor.org/rfc/rfc9824.html#section-5.1 |
| 30 | + private static final int CO = 0x4000; |
| 31 | + |
| 32 | + private static DnsOptPseudoRecord newRecord(int maxPayloadSize, int extendedRcode, int version, int flags) { |
| 33 | + return new AbstractDnsOptPseudoRrRecord(maxPayloadSize, extendedRcode, version, flags) { }; |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testFlagsCanCarryDo() { |
| 38 | + DnsOptPseudoRecord record = newRecord(4096, 0, 0, DO); |
| 39 | + assertEquals(DO, record.flags()); |
| 40 | + assertEquals(0, record.extendedRcode()); |
| 41 | + assertEquals(0, record.version()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testFlagsIsNotTruncatedToEightBits() { |
| 46 | + // The flags field is 16 bits wide, so bits 8..15 must survive as well. |
| 47 | + DnsOptPseudoRecord record = newRecord(4096, 0, 0, 0x7fff); |
| 48 | + assertEquals(0x7fff, record.flags()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testFlagsCanCarryBitsOtherThanDo() { |
| 53 | + // Bits below DO are assigned by the IANA EDNS Header Flags registry, so they must not be masked off. |
| 54 | + assertEquals(CO, newRecord(4096, 0, 0, CO).flags()); |
| 55 | + assertEquals(DO | CO, newRecord(4096, 0, 0, DO | CO).flags()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testTtlLayoutIsExtendedRcodeVersionFlags() { |
| 60 | + // Asymmetric values so that swapping or widening any of the three fields is detectable. |
| 61 | + DnsOptPseudoRecord record = newRecord(4096, 0x12, 0x34, 0x8abc); |
| 62 | + assertEquals(0x12348abcL, record.timeToLive()); |
| 63 | + assertEquals(0x12, record.extendedRcode()); |
| 64 | + assertEquals(0x34, record.version()); |
| 65 | + assertEquals(0x8abc, record.flags()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testAllFieldsAtMaximum() { |
| 70 | + DnsOptPseudoRecord record = newRecord(1232, 0xff, 0xff, 0xffff); |
| 71 | + assertEquals(0xff, record.extendedRcode()); |
| 72 | + assertEquals(0xff, record.version()); |
| 73 | + assertEquals(0xffff, record.flags()); |
| 74 | + assertEquals(0xffffffffL, record.timeToLive()); |
| 75 | + assertEquals(1232, record.dnsClass()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testFlagsDoNotLeakIntoExtendedRcodeOrVersion() { |
| 80 | + DnsOptPseudoRecord record = newRecord(4096, 0, 0, 0xffff); |
| 81 | + assertEquals(0, record.extendedRcode()); |
| 82 | + assertEquals(0, record.version()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testExtendedRcodeAndVersionDoNotLeakIntoFlags() { |
| 87 | + DnsOptPseudoRecord record = newRecord(4096, 0xff, 0xff, 0); |
| 88 | + assertEquals(0, record.flags()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testOutOfRangeValuesAreTruncatedToTheirFieldWidth() { |
| 93 | + // 0x01 for the extended RCODE so a bit bleeding out of an over-wide VERSION is visible. |
| 94 | + DnsOptPseudoRecord record = newRecord(4096, 0x01, 0x2ff, 0x3ffff); |
| 95 | + assertEquals(0x01, record.extendedRcode()); |
| 96 | + assertEquals(0xff, record.version()); |
| 97 | + assertEquals(0xffff, record.flags()); |
| 98 | + assertEquals(0x01ffffffL, record.timeToLive()); |
| 99 | + |
| 100 | + // A negative value must never produce a negative TTL, which AbstractDnsRecord rejects. |
| 101 | + DnsOptPseudoRecord negative = newRecord(4096, -1, -1, -1); |
| 102 | + assertEquals(0xffffffffL, negative.timeToLive()); |
| 103 | + assertEquals(0xffff, negative.flags()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testPreExistingConstructorsKeepTtlUnchanged() { |
| 108 | + // The constructors that predate the flags parameter must produce exactly the TTL they did before. |
| 109 | + DnsOptPseudoRecord threeArg = new AbstractDnsOptPseudoRrRecord(4096, 0xab, 0xcd) { }; |
| 110 | + assertEquals(0xabcd0000L, threeArg.timeToLive()); |
| 111 | + assertEquals(0, threeArg.flags()); |
| 112 | + |
| 113 | + DnsOptPseudoRecord oneArg = new AbstractDnsOptPseudoRrRecord(4096) { }; |
| 114 | + assertEquals(0L, oneArg.timeToLive()); |
| 115 | + assertEquals(0, oneArg.flags()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testDoIsEncodedOnTheWire() throws Exception { |
| 120 | + assertEncoded(newRecord(4096, 0, 0, DO), 4096, 0, 0, DO); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testExtendedRcodeVersionAndFlagsAreEncodedOnTheWire() throws Exception { |
| 125 | + assertEncoded(newRecord(1232, 0x01, 0x02, DO | CO), 1232, 0x01, 0x02, DO | CO); |
| 126 | + } |
| 127 | + |
| 128 | + private static void assertEncoded(DnsOptPseudoRecord record, int maxPayloadSize, |
| 129 | + int extendedRcode, int version, int flags) throws Exception { |
| 130 | + ByteBuf out = Unpooled.buffer(); |
| 131 | + try { |
| 132 | + new DefaultDnsRecordEncoder().encodeRecord(record, out); |
| 133 | + |
| 134 | + // See https://tools.ietf.org/html/rfc6891#section-6.1.2 |
| 135 | + assertEquals(0, out.readByte()); // NAME must be the root domain. |
| 136 | + assertEquals(DnsRecordType.OPT.intValue(), out.readUnsignedShort()); // TYPE |
| 137 | + assertEquals(maxPayloadSize, out.readUnsignedShort()); // CLASS holds the UDP payload size. |
| 138 | + assertEquals(extendedRcode, out.readUnsignedByte()); // TTL: EXTENDED-RCODE |
| 139 | + assertEquals(version, out.readUnsignedByte()); // TTL: VERSION |
| 140 | + assertEquals(flags, out.readUnsignedShort()); // TTL: DO + Z |
| 141 | + assertEquals(0, out.readUnsignedShort()); // RDLENGTH |
| 142 | + assertEquals(0, out.readableBytes()); |
| 143 | + } finally { |
| 144 | + out.release(); |
| 145 | + } |
| 146 | + } |
| 147 | +} |