Skip to content

Commit

Permalink
feat(Record): Allow copying with custom data fields (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea authored Jan 20, 2023
1 parent 4874a20 commit 44665f1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/lib/Zone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('Zone', () => {
tldDnskey.rrsig.data.signature,
);
const mismatchingDnskeyRrsig = tldDnskey.rrsig.record.shallowCopy({
dataSerialised: mismatchingDnskeyRrsigData.serialise(),
data: mismatchingDnskeyRrsigData.serialise(),
});
const result = Zone.init(
RECORD_TLD,
Expand Down Expand Up @@ -149,7 +149,7 @@ describe('Zone', () => {
tldDnskey.rrsig.data.signature,
);
const mismatchingDnskeyRrsig = tldDnskey.rrsig.record.shallowCopy({
dataSerialised: mismatchingDnskeyRrsigData.serialise(),
data: mismatchingDnskeyRrsigData.serialise(),
});
const result = Zone.init(
RECORD_TLD,
Expand Down
11 changes: 9 additions & 2 deletions src/lib/utils/dns/DnsRecord.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,22 @@ describe('DnsRecord', () => {
expect(newRecord.dataSerialised).toBe(RECORD.dataSerialised);
});

test('New data should be used if set', () => {
test('New data serialisation should be used if set', () => {
const newData = Buffer.alloc(8);
const newRecord = RECORD.shallowCopy({ dataSerialised: newData });
const newRecord = RECORD.shallowCopy({ data: newData });

expect(newRecord.name).toStrictEqual(RECORD.name);
expect(newRecord.typeId).toStrictEqual(RECORD.typeId);
expect(newRecord.classId).toStrictEqual(RECORD.classId);
expect(newRecord.ttl).toStrictEqual(RECORD.ttl);
expect(newRecord.dataSerialised).toBe(newData);
});

test('New data fields should be used if set', () => {
const newData = `not-${RECORD.dataFields}`;
const newRecord = RECORD.shallowCopy({ data: newData });

expect(newRecord.dataFields).toBe(newData);
});
});
});
6 changes: 3 additions & 3 deletions src/lib/utils/dns/DnsRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface RecordFields {
readonly type: number;
readonly class: DnsClass;
readonly ttl: number;
readonly dataSerialised: Buffer;
readonly data: unknown;
}

function deserialiseRdata(serialisation: Buffer, typeName: string, codec: Codec<any>): any {
Expand Down Expand Up @@ -107,8 +107,8 @@ export class DnsRecord {
const type = partialRecord.type ?? this.typeId;
const classId = partialRecord.class ?? this.classId;
const ttl = partialRecord.ttl ?? this.ttl;
const dataSerialised = partialRecord.dataSerialised ?? this.dataSerialised;
return new DnsRecord(name, type, classId, ttl, dataSerialised);
const data = partialRecord.data ?? this.dataSerialised;
return new DnsRecord(name, type, classId, ttl, data);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/lib/utils/dns/RrSet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('RrSet', () => {
});

test('Multiple records should be supported', () => {
const record2 = RECORD.shallowCopy({ dataSerialised: Buffer.from([1, 2]) });
const record2 = RECORD.shallowCopy({ data: Buffer.from([1, 2]) });

const rrset = RrSet.init(QUESTION, [RECORD, record2]);

Expand Down Expand Up @@ -92,8 +92,8 @@ describe('RrSet', () => {
});

test('RDATA should be sorted from the left if they have same length', () => {
const record1 = RECORD.shallowCopy({ dataSerialised: Buffer.from([1, 0]) });
const record2 = RECORD.shallowCopy({ dataSerialised: Buffer.from([1, 1]) });
const record1 = RECORD.shallowCopy({ data: Buffer.from([1, 0]) });
const record2 = RECORD.shallowCopy({ data: Buffer.from([1, 1]) });

const rrset = RrSet.init(QUESTION, [record2, record1]);

Expand Down
2 changes: 1 addition & 1 deletion src/testUtils/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export function copyDnssecRecordData<
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
return {
data: newData,
record: originalRecord.record.shallowCopy({ dataSerialised: newData.serialise() }),
record: originalRecord.record.shallowCopy({ data: newData.serialise() }),
} as DnsRecord;
}

0 comments on commit 44665f1

Please sign in to comment.