-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement test utilities (#107)
For when users of this lib can't mock this library (e.g., when using Jest). - [x] Documentation
- Loading branch information
Showing
22 changed files
with
344 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
/* eslint-disable @typescript-eslint/prefer-enum-initializers */ | ||
/** | ||
* DNSSEC security status. | ||
* | ||
* See https://www.rfc-editor.org/rfc/rfc4035#section-4.3 | ||
*/ | ||
export enum SecurityStatus { | ||
SECURE, | ||
INSECURE, | ||
BOGUS, | ||
INDETERMINATE, | ||
SECURE = 'SECURE', | ||
INSECURE = 'INSECURE', | ||
BOGUS = 'BOGUS', | ||
INDETERMINATE = 'INDETERMINATE', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { addMinutes, addSeconds, setMilliseconds, subSeconds } from 'date-fns'; | ||
|
||
import { QUESTION, RECORD, RRSET } from '../../testUtils/dnsStubs.js'; | ||
import { SecurityStatus } from '../SecurityStatus.js'; | ||
import { dnssecLookUp } from '../lookup.js'; | ||
import { type FailureStatus } from '../results.js'; | ||
import { DatePeriod } from '../DatePeriod.js'; | ||
|
||
import { MockChain } from './MockChain.js'; | ||
|
||
describe('MockChain', () => { | ||
describe('generateResolver', () => { | ||
test('SECURE result should be generated if given an RRset', async () => { | ||
const mockChain = await MockChain.generate(RECORD.name); | ||
|
||
const { resolver, trustAnchors } = mockChain.generateFixture(RRSET, SecurityStatus.SECURE); | ||
|
||
const result = await dnssecLookUp(QUESTION, resolver, { trustAnchors }); | ||
expect(result).toStrictEqual({ | ||
status: SecurityStatus.SECURE, | ||
result: RRSET, | ||
}); | ||
}); | ||
|
||
test.each<FailureStatus>([ | ||
SecurityStatus.INSECURE, | ||
SecurityStatus.BOGUS, | ||
SecurityStatus.INDETERMINATE, | ||
])('%s result should be generated if requested', async (status) => { | ||
const mockChain = await MockChain.generate(RECORD.name); | ||
|
||
const { resolver, trustAnchors } = mockChain.generateFixture(RRSET, status); | ||
|
||
const result = await dnssecLookUp(QUESTION, resolver, { trustAnchors }); | ||
expect(result.status).toStrictEqual(status); | ||
}); | ||
|
||
test('Signatures should be valid for 60 seconds by default', async () => { | ||
const testStartDate = new Date(); | ||
const mockChain = await MockChain.generate(RECORD.name); | ||
|
||
const { resolver, trustAnchors } = mockChain.generateFixture(RRSET, SecurityStatus.SECURE); | ||
|
||
await expect(dnssecLookUp(QUESTION, resolver, { trustAnchors })).resolves.toHaveProperty( | ||
'status', | ||
SecurityStatus.SECURE, | ||
); | ||
await expect( | ||
dnssecLookUp(QUESTION, resolver, { | ||
dateOrPeriod: subSeconds(testStartDate, 1), | ||
trustAnchors, | ||
}), | ||
).resolves.toHaveProperty('status', SecurityStatus.BOGUS); | ||
await expect( | ||
dnssecLookUp(QUESTION, resolver, { | ||
dateOrPeriod: addMinutes(addSeconds(testStartDate, 1), 5), | ||
trustAnchors, | ||
}), | ||
).resolves.toHaveProperty('status', SecurityStatus.BOGUS); | ||
}); | ||
|
||
test('Explicit signature period should be honoured', async () => { | ||
const now = setMilliseconds(new Date(), 0); | ||
const period = DatePeriod.init(subSeconds(now, 120), subSeconds(now, 60)); | ||
const mockChain = await MockChain.generate(RECORD.name); | ||
|
||
const { resolver, trustAnchors } = mockChain.generateFixture( | ||
RRSET, | ||
SecurityStatus.SECURE, | ||
period, | ||
); | ||
|
||
await expect( | ||
dnssecLookUp(QUESTION, resolver, { dateOrPeriod: period.end, trustAnchors }), | ||
).resolves.toHaveProperty('status', SecurityStatus.SECURE); | ||
await expect( | ||
dnssecLookUp(QUESTION, resolver, { | ||
dateOrPeriod: subSeconds(period.start, 1), | ||
trustAnchors, | ||
}), | ||
).resolves.toHaveProperty('status', SecurityStatus.BOGUS); | ||
await expect( | ||
dnssecLookUp(QUESTION, resolver, { | ||
dateOrPeriod: addSeconds(period.end, 1), | ||
trustAnchors, | ||
}), | ||
).resolves.toHaveProperty('status', SecurityStatus.BOGUS); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.