Skip to content

Commit

Permalink
New build
Browse files Browse the repository at this point in the history
  • Loading branch information
pfumagalli committed Apr 21, 2023
1 parent abd79ce commit 7b72962
Show file tree
Hide file tree
Showing 7 changed files with 385 additions and 364 deletions.
577 changes: 293 additions & 284 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@juit/lib-ping",
"version": "1.1.0",
"version": "1.1.1",
"type": "module",
"description": "Rootless ICMPv4/ICMPv6 Ping library for NodeJS",
"main": "./dist/index.cjs",
Expand Down Expand Up @@ -45,10 +45,10 @@
"author": "Juit Developers <[email protected]>",
"license": "Apache-2.0",
"devDependencies": {
"@plugjs/build": "^0.3.0",
"@types/node": "^18.15.5",
"@plugjs/build": "^0.4.0",
"@types/node": "^18.15.13",
"node-gyp": "^9.3.1",
"typescript": "^5.0.2"
"typescript": "^5.0.4"
},
"directories": {
"test": "test"
Expand Down
60 changes: 30 additions & 30 deletions test/00-constructor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,156 +34,156 @@ describe('Constructor', () => {
it('should construct with an IPv4 address', async () => {
pinger = await createPinger('127.0.0.1')

expect(pinger).toEqual(jasmine.objectContaining({
expect(pinger).toInclude({
from: undefined,
source: undefined,
target: '127.0.0.1',
timeout: 30000,
interval: 1000,
protocol: 'ipv4',
}))
})
})

it('should construct with an IPv6 address', async () => {
pinger = await createPinger('::1')

expect(pinger).toEqual(jasmine.objectContaining({
expect(pinger).toInclude({
from: undefined,
source: undefined,
target: '::1',
timeout: 30000,
interval: 1000,
protocol: 'ipv6',
}))
})
})

it('should construct with an IPv4 host name', async () => {
pinger = await createPinger('www.google.com', { protocol: 'ipv4' })
expect(pinger).toEqual(jasmine.objectContaining({
expect(pinger).toInclude({
from: undefined,
source: undefined,
target: pinger.target,
timeout: 30000,
interval: 1000,
protocol: 'ipv4',
}))
})

const addrs = await resolve4('www.google.com')
expect(addrs).toEqual(jasmine.arrayContaining([ pinger.target ]))
expect(addrs).toInclude([ pinger.target ])
})

it('should construct with an IPv6 host name', async () => {
pinger = await createPinger('www.google.com', { protocol: 'ipv6' })
expect(pinger).toEqual(jasmine.objectContaining({
expect(pinger).toInclude({
from: undefined,
source: undefined,
target: pinger.target,
timeout: 30000,
interval: 1000,
protocol: 'ipv6',
}))
})

const addrs = await resolve6('www.google.com')
expect(addrs).toEqual(jasmine.arrayContaining([ pinger.target ]))
expect(addrs).toInclude([ pinger.target ])
})

it('should construct with an IPv4 from address and target', async () => {
pinger = await createPinger('127.0.0.1', { from: '127.0.0.1' })

expect(pinger).toEqual(jasmine.objectContaining({
expect(pinger).toInclude({
from: '127.0.0.1',
source: undefined,
target: '127.0.0.1',
timeout: 30000,
interval: 1000,
protocol: 'ipv4',
}))
})
})

it('should construct with an IPv6 from address and target', async () => {
pinger = await createPinger('::1', { from: '::1' })

expect(pinger).toEqual(jasmine.objectContaining({
expect(pinger).toInclude({
from: '::1',
source: undefined,
target: '::1',
timeout: 30000,
interval: 1000,
protocol: 'ipv6',
}))
})
})

it('should construct with an IPv4 source interface and target', async () => {
pinger = await createPinger('127.0.0.1', { source: localIf4 })

expect(pinger).toEqual(jasmine.objectContaining({
expect(pinger).toInclude({
from: undefined,
source: localIf4,
target: '127.0.0.1',
timeout: 30000,
interval: 1000,
protocol: 'ipv4',
}))
})
})

it('should construct with an IPv6 source interface and target', async () => {
pinger = await createPinger('::1', { source: localIf6 })

expect(pinger).toEqual(jasmine.objectContaining({
expect(pinger).toInclude({
from: undefined,
source: localIf6,
target: '::1',
timeout: 30000,
interval: 1000,
protocol: 'ipv6',
}))
})
})

/* ======================================================================== */

it('should not construct with the wrong protocol', async () => {
await expectAsync(createPinger('::1', { protocol: 0 } as any))
await expect(createPinger('::1', { protocol: 0 } as any))
.toBeRejectedWithError(AssertionError, 'Invalid protocol "0" specified')
})

it('should not construct with a wrong host name', async () => {
await expectAsync(createPinger('no-wrong.juit.com'))
await expect(createPinger('no-wrong.juit.com'))
.toBeRejectedWithError(Error, 'Unable to resolve ping target "no-wrong.juit.com" as an ipv4 address')
await expectAsync(createPinger('no-wrong.juit.com', { protocol: 'ipv4' }))
await expect(createPinger('no-wrong.juit.com', { protocol: 'ipv4' }))
.toBeRejectedWithError(Error, 'Unable to resolve ping target "no-wrong.juit.com" as an ipv4 address')
await expectAsync(createPinger('no-wrong.juit.com', { protocol: 'ipv6' }))
await expect(createPinger('no-wrong.juit.com', { protocol: 'ipv6' }))
.toBeRejectedWithError(Error, 'Unable to resolve ping target "no-wrong.juit.com" as an ipv6 address')
})

it('should not construct when an address does not match the protocol', async () => {
await expectAsync(createPinger('127.0.0.1', { protocol: 'ipv6' }))
await expect(createPinger('127.0.0.1', { protocol: 'ipv6' }))
.toBeRejectedWithError(Error, 'Invalid IPv4 ping target "127.0.0.1"')
await expectAsync(createPinger('::1', { protocol: 'ipv4' }))
await expect(createPinger('::1', { protocol: 'ipv4' }))
.toBeRejectedWithError(Error, 'Invalid IPv6 ping target "::1"')
})

it('should not construct when the from address is invalid', async () => {
await expectAsync(createPinger('127.0.0.1', { from: 'wrong-address' }))
await expect(createPinger('127.0.0.1', { from: 'wrong-address' }))
.toBeRejectedWithError(Error, 'Invalid IP address to ping from "wrong-address"')
})

it('should not construct when the source interface is invalid', async () => {
await expectAsync(createPinger('127.0.0.1', { source: 'wrong-interface' }))
await expect(createPinger('127.0.0.1', { source: 'wrong-interface' }))
.toBeRejectedWithError(Error, 'Invalid source interface name "wrong-interface"')
})

it('should not construct when a source address does not match the target', async () => {
await expectAsync(createPinger('127.0.0.1', { from: '::1' }))
await expect(createPinger('127.0.0.1', { from: '::1' }))
.toBeRejectedWithError(Error, 'Invalid IPv4 address to ping from "::1"')
await expectAsync(createPinger('::1', { from: '127.0.0.1' }))
await expect(createPinger('::1', { from: '127.0.0.1' }))
.toBeRejectedWithError(Error, 'Invalid IPv6 address to ping from "127.0.0.1"')
})

it('should not construct when a source address does not match an interface', async () => {
// sorry, cloudflare 1.1.1.1
await expectAsync(createPinger('127.0.0.1', { from: '1.1.1.1' }))
await expect(createPinger('127.0.0.1', { from: '1.1.1.1' }))
.toBeRejectedWithError(Error, 'address not available')
await expectAsync(createPinger('::1', { from: '2606:4700:4700::1111' }))
await expect(createPinger('::1', { from: '2606:4700:4700::1111' }))
.toBeRejectedWithError(Error, 'address not available')
})
})
56 changes: 30 additions & 26 deletions test/02-ping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ describe('Ping Test', () => {

const { sent, received, latency } = stats

expect(sent).withContext('sent').toBeGreaterThanOrEqual(9)
expect(sent).withContext('sent').toBeLessThanOrEqual(10)
expect(received).withContext('received').toEqual(sent)
expect(latency).withContext('pong').toBeLessThan(10)
expect(sent).toBeGreaterThanOrEqual(9)
expect(sent).toBeLessThanOrEqual(10)
expect(received).toEqual(sent)
expect(latency).toBeLessThan(10)

expect(stats).toEqual({ sent, received, latency })
} finally {
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('Ping Test', () => {
try {
// await for our file descriptor to be open, socket to be bound and close
await new Promise((resolve) => setTimeout(resolve, 200))
expect((<any> pinger).__fd).toBeInstanceOf(Number)
expect((<any> pinger).__fd).toBeA('number')
expect((<any> pinger).__fd).toBeGreaterThan(0)
fs.closeSync((<any> pinger).__fd)

Expand Down Expand Up @@ -125,16 +125,16 @@ describe('Ping Test', () => {
try {
// await for our file descriptor to be open, socket to be bound and close
await new Promise((resolve) => setTimeout(resolve, 200))
expect((<any> pinger).__fd).toBeInstanceOf(Number)
expect((<any> pinger).__fd).toBeA('number')
expect((<any> pinger).__fd).toBeGreaterThan(0)
fs.closeSync((<any> pinger).__fd)

expect(pinger.closed).toBeFalse()
expect(pinger.running).toBeFalse()

await expectAsync(pinger.ping()).toBeRejectedWith(jasmine.objectContaining({
code: 'EBADF',
}))
await expect(pinger.ping()).toBeRejected((assert) => {
assert.toInclude({ code: 'EBADF' })
})

expect(pinger.closed).toBeTrue()
expect(pinger.running).toBeFalse()
Expand Down Expand Up @@ -169,27 +169,31 @@ describe('Ping Test', () => {
it('should emit warnings when the wrong packet is received', async () => {
const pinger = await createPinger('127.0.0.1')

const warnings: string[][] = []
pinger.on('warning', (...args) => warnings.push(args))
try {
const warnings: string[][] = []
pinger.on('warning', (...args) => warnings.push(args))

// first ping
await pinger.ping()
// first ping
await pinger.ping()

// mess up whe sequence number in the protocol handler
;((<any> pinger).__handler.__seq_out --)
// mess up whe sequence number in the protocol handler
;((<any> pinger).__handler.__seq_out --)

// ping once again, we should get ERR_SEQUENCE_TOO_SMALL
await pinger.ping()
// ping once again, we should get ERR_SEQUENCE_TOO_SMALL
await pinger.ping()

// give it a jiffy to do stuff on the network
await new Promise((resolve) => setTimeout(resolve, 100))
// give it a jiffy to do stuff on the network
await new Promise((resolve) => setTimeout(resolve, 100))

// check we got our code
expect(warnings).toEqual(jasmine.arrayContaining([
jasmine.arrayWithExactContents([
'ERR_SEQUENCE_TOO_SMALL',
'Received packet with sequence in the past (duplicate packet?)',
]),
]))
// check we got our code
expect(warnings).toInclude([
expect.toMatchContents([
'ERR_SEQUENCE_TOO_SMALL',
'Received packet with sequence in the past (duplicate packet?)',
]),
])
} finally {
await pinger.close()
}
})
})
44 changes: 26 additions & 18 deletions test/03-native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,19 @@ describe('Native Adapter', () => {
it('should not bind to the wrong source interface', async () => {
const open = promisify(native.open)

await expectAsync(open(native.AF_INET, null, 'xyznope'))
.toBeRejectedWith(jasmine.objectContaining({
syscall: jasmine.stringMatching(/^(setsockopt)|(if_nametoindex)$/),
}))

await expectAsync(open(native.AF_INET6, null, 'xyznope'))
.toBeRejectedWith(jasmine.objectContaining({
syscall: jasmine.stringMatching(/^(setsockopt)|(if_nametoindex)$/),
}))
await expect(open(native.AF_INET, null, 'xyznope'))
.toBeRejected((assert) => {
assert.toInclude({
syscall: expect.toMatch(/^(setsockopt)|(if_nametoindex)$/),
})
})

await expect(open(native.AF_INET6, null, 'xyznope'))
.toBeRejected((assert) => {
assert.toInclude({
syscall: expect.toMatch(/^(setsockopt)|(if_nametoindex)$/),
})
})
})


Expand All @@ -97,14 +101,18 @@ describe('Native Adapter', () => {

const open = promisify(native.open)

await expectAsync(open(native.AF_INET, '1.1.1.1', null))
.toBeRejectedWith(jasmine.objectContaining({
syscall: 'bind',
}))

await expectAsync(open(native.AF_INET6, '2606:4700:4700::1111', null))
.toBeRejectedWith(jasmine.objectContaining({
syscall: 'bind',
}))
await expect(open(native.AF_INET, '1.1.1.1', null))
.toBeRejected((assert) => {
assert.toInclude({
syscall: 'bind',
})
})

await expect(open(native.AF_INET6, '2606:4700:4700::1111', null))
.toBeRejected((assert) => {
assert.toInclude({
syscall: 'bind',
})
})
})
})
2 changes: 1 addition & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": [ "jasmine", "node" ],
"types": [ "@plugjs/expect5/globals", "node" ],
"rootDir": "..",
},
"include": [ "." ],
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"verbatimModuleSyntax": true,
},
"include": [
"./src",
"./build.ts",
"./src",
],
}

0 comments on commit 7b72962

Please sign in to comment.