Skip to content

Commit

Permalink
updated: moved test files
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiIgna committed Jan 23, 2025
1 parent d850f82 commit 4146ab7
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- name: npm install and test
run: npm cit
run: npm install-ci-test
env:
CI: true
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"scripts": {
"build": "tsc",
"dev": "node --enable-source-maps --trace-deprecation --import tsx/esm --watch src/index.ts",
"test": "node --test --import tsx/esm test/**/*.ts"
"test": "node --test --import tsx/esm src/*.test.ts"
},
"dependencies": {
"punycode": "^2.1.1"
Expand Down
2 changes: 1 addition & 1 deletion test/dns-resolvers.ts → src/dns-resolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { strict as assert } from 'node:assert'
import { test, suite } from 'node:test'
import { isIPv4, isIPv6 } from 'node:net'

import { dnsRecordsCloudflare, dnsRecordsGoogle, dnsRecordsNodeDns } from '../src/dns-resolvers.ts'
import { dnsRecordsCloudflare, dnsRecordsGoogle, dnsRecordsNodeDns } from './dns-resolvers.js'

suite('Cloudflare DNS resolver', () => {
test('NS records', async () => {
Expand Down
97 changes: 97 additions & 0 deletions src/get-dns-records.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { strict as assert } from 'node:assert'
import { test, suite } from 'node:test'

import { getDnsRecords } from './get-dns-records.js'
import { isIPv4 } from 'node:net'

suite('NS for google.com', async () => {
const expectedNs = ['ns1.google.com', 'ns2.google.com', 'ns3.google.com', 'ns4.google.com']

const [ nsRecordsWithCloudflareDns, nsRecordsWithGoogleDns, nsRecordsWithNodeDns, nsRecordsWithNodeDig ] = await Promise.all([
getDnsRecords('google.com', 'NS', 'cloudflare-dns'),
getDnsRecords('google.com', 'NS', 'google-dns'),
getDnsRecords('google.com', 'NS', 'node-dns'),
getDnsRecords('google.com', 'NS', 'node-dig'),
])

test('same number of NS from all resolvers', () => {
assert.equal(nsRecordsWithCloudflareDns.length, expectedNs.length, 'Number of NameServers doesn\'t match')
assert.equal(nsRecordsWithGoogleDns.length, expectedNs.length, 'Number of NameServers doesn\'t match')
assert.equal(nsRecordsWithNodeDns.length, expectedNs.length, 'Number of NameServers doesn\'t match')
assert.equal(nsRecordsWithNodeDig.length, expectedNs.length, 'Number of NameServers doesn\'t match')
})

test('validate NS from `cloudflare-dns`', () => {
assert.equal(nsRecordsWithCloudflareDns[0].name, 'google.com', 'Returned NS doesn\'t match')
assert.equal(nsRecordsWithCloudflareDns[0].type, 'NS', 'Returned record type is not NS')
assert.ok(expectedNs.some(ns => ns === nsRecordsWithCloudflareDns[0].data), 'Returned NS doesn\'t match')
assert.ok(expectedNs.some(ns => ns === nsRecordsWithCloudflareDns[1].data), 'Returned NS doesn\'t match')
});

test('validate NS from `google-dns`', () => {
assert.equal(nsRecordsWithGoogleDns[0].name, 'google.com', 'Returned NS doesn\'t match')
assert.equal(nsRecordsWithGoogleDns[0].type, 'NS', 'Returned record type is not NS')
assert.ok(expectedNs.some(ns => ns === nsRecordsWithGoogleDns[0].data), 'Returned NS doesn\'t match')
assert.ok(expectedNs.some(ns => ns === nsRecordsWithGoogleDns[1].data), 'Returned NS doesn\'t match')
});

test('validate NS from `node-dns`', () => {
assert.equal(nsRecordsWithNodeDns[0].name, 'google.com', 'Returned NS doesn\'t match')
assert.equal(nsRecordsWithNodeDns[0].type, 'NS', 'Returned record type is not NS')
assert.ok(expectedNs.some(ns => ns === nsRecordsWithNodeDns[0].data), 'Returned NS doesn\'t match')
assert.ok(expectedNs.some(ns => ns === nsRecordsWithNodeDns[1].data), 'Returned NS doesn\'t match')
});

test('validate NS from `node-dig`', () => {
assert.equal(nsRecordsWithNodeDig[0].name, 'google.com', 'Returned NS doesn\'t match')
assert.equal(nsRecordsWithNodeDig[0].type, 'NS', 'Returned record type is not NS')
assert.ok(expectedNs.some(ns => ns === nsRecordsWithNodeDig[0].data), 'Returned NS doesn\'t match')
assert.ok(expectedNs.some(ns => ns === nsRecordsWithNodeDig[1].data), 'Returned NS doesn\'t match')
});
})

suite('A records for "mañana.com" (IDN)', async () => {
const [ aRecordsWithCloudflareDns, aRecordsWithGoogleDns, aRecordsWithNodeDns, aRecordsWithNodeDig ] = await Promise.all([
getDnsRecords('mañana.com', 'A', 'cloudflare-dns'),
getDnsRecords('mañana.com', 'A', 'google-dns'),
getDnsRecords('mañana.com', 'A', 'node-dns'),
getDnsRecords('mañana.com', 'A', 'node-dig'),
])

test('validate length of records', () => {
assert.notEqual(aRecordsWithCloudflareDns.length, 0)
assert.equal(aRecordsWithCloudflareDns.length, aRecordsWithGoogleDns.length)
assert.equal(aRecordsWithGoogleDns.length, aRecordsWithNodeDns.length)
assert.equal(aRecordsWithNodeDns.length, aRecordsWithNodeDig.length)
});

test('validate returned data', () => {
assert.ok(isIPv4(aRecordsWithCloudflareDns[0].data))
assert.equal(aRecordsWithCloudflareDns[0].data, aRecordsWithGoogleDns[0].data)
assert.equal(aRecordsWithGoogleDns[0].data, aRecordsWithNodeDns[0].data)
assert.equal(aRecordsWithNodeDns[0].data, aRecordsWithNodeDig[0].data)
});
})

suite('TXT records for "cloudflare.com"', async () => {
const [ txtRecordsWithCloudflareDns, txtRecordsWithGoogleDns, txtRecordsWithNodeDns, txtRecordsWithNodeDig ] = await Promise.all([
getDnsRecords('cloudflare.com', 'TXT', 'cloudflare-dns'),
getDnsRecords('cloudflare.com', 'TXT', 'google-dns'),
getDnsRecords('cloudflare.com', 'TXT', 'node-dns'),
getDnsRecords('cloudflare.com', 'TXT', 'node-dig'),
])

test('validate number of records', () => {
assert.notEqual(txtRecordsWithCloudflareDns.length, 0)
assert.equal(txtRecordsWithCloudflareDns.length, txtRecordsWithGoogleDns.length, 'TXT records length between `google-dns` and `cloudflare-dns` doesn\'t match')
assert.equal(txtRecordsWithGoogleDns.length, txtRecordsWithNodeDns.length, 'TXT records length between `cloudflare-dns` and `node-dns` doesn\'t match')
assert.equal(txtRecordsWithNodeDns.length, txtRecordsWithNodeDig.length)
})

test('find spf record (cloudflare.com must have one)', () => {
assert.ok(txtRecordsWithCloudflareDns.some(record => record.data.includes('v=spf1')))
assert.ok(txtRecordsWithGoogleDns.some(record => record.data.includes('v=spf1')))
assert.ok(txtRecordsWithNodeDns.some(record => record.data.includes('v=spf1')))
assert.ok(txtRecordsWithNodeDig.some(record => record.data.includes('v=spf1')))
})
})
25 changes: 25 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { strict as assert } from 'node:assert'
import { test } from 'node:test'

import { getAllDnsRecords } from './index.js'

test('get all DNS records for "x.com"', async () => {
const dnsRecords = await getAllDnsRecords('x.com')

assert.notEqual(dnsRecords.length, 0, 'No DNS Records returned')
assert.ok(dnsRecords.find(record => record.type === 'NS'), 'No NS records returned')
assert.ok(dnsRecords.find(record => record.type === 'A'), 'No A records returned')
assert.ok(dnsRecords.find(record => record.type === 'MX'), 'No MX records returned')
assert.ok(dnsRecords.find(record => record.type === 'TXT'), 'No TXT records returned')
});

test('should detect the wildcard subdomains for "wordpress.org"', async () => {
const dnsRecords = await getAllDnsRecords('wordpress.org')

const nsRecords = dnsRecords.filter(record => record.type === 'NS')
const aRecords = dnsRecords.filter(record => record.type === 'A')

assert.notEqual(nsRecords.length, 0, 'No NS records returned')
assert.notEqual(aRecords.length, 0, 'No A records returned')
assert.ok(dnsRecords.find(record => record.name === '*.wordpress.org'), 'Expected *.wordpress.org record not found')
});
35 changes: 35 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { strict as assert } from 'node:assert'
import { test, suite } from 'node:test'

import { isDomain, isTld } from './utils.js'

suite('isDomain()', () => {
test('valid domains', () => {
assert.ok(isDomain('google.com'))
assert.ok(isDomain('invalid-but-good-format.example'))
assert.ok(isDomain('dns.cloudflare-dns.com'))
assert.ok(isDomain('example.example.example.example'))
})

test('invalid domains', () => {
assert.equal(isDomain(''), false)
assert.equal(isDomain('1'), false)
assert.equal(isDomain('google'), false)
})
})

suite('isTld()', () => {
test('valid TLDs', () => {
assert.ok(isTld('com'))
assert.ok(isTld('.com'))
assert.ok(isTld('.validtldformat'))
})

test('invalid TLDs', () => {
assert.equal(isTld(''), false)
assert.equal(isTld('c'), false)
assert.equal(isTld('-'), false)
assert.equal(isTld('1'), false)
assert.equal(isTld('.example-tld'), false)
})
})
3 changes: 0 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ export function isDomain(domain: string): boolean {
const labelTest = /^([a-z0-9-_]{1,64}|xn[a-z0-9-]{5,})$/i

return labels.length > 1 && labels.every((label, index) => {

console.log(index, label, index ? !label.startsWith('-') && !label.endsWith('-') && labelTest.test(label) : isTld(label))

return index ? !label.startsWith('-') && !label.endsWith('-') && labelTest.test(label) : isTld(label)
})
}
76 changes: 0 additions & 76 deletions test/test.ts

This file was deleted.

0 comments on commit 4146ab7

Please sign in to comment.