Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit 8a247c4

Browse files
committed
tests needed for p2p
1 parent aa1ce11 commit 8a247c4

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

test/src/dgram.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
import * as dgram from '@socketsupply/io/dgram'
3+
//import dgram from 'dgram' //uncomment to tests the tests, should pass running node
4+
import { test } from 'tapzero'
5+
6+
test('bind a dgram port and send a packet to it', function (t) {
7+
var resolve
8+
var socket = dgram.createSocket('udp4'), socket2
9+
// var p = ~~(Math.random()*0xffff)
10+
socket.on('message', function (b) {
11+
t.equal(b.toString(), 'hello!')
12+
socket.close(function () {})
13+
resolve()
14+
})
15+
t.equal(socket.bind(), socket, 'bind returns this') //any port
16+
17+
socket.on('listening', function () {
18+
var addr = socket.address()
19+
var p = addr.port
20+
t.equal(addr.address, '0.0.0.0')
21+
t.ok(Number.isInteger(addr.port))
22+
socket.send(Buffer.from('hello!'), p, '127.0.0.1') //any port
23+
})
24+
return new Promise(_resolve => resolve = _resolve)
25+
})
26+
27+
test('error if port is already bound', function (t) {
28+
var resolve
29+
var socket = dgram.createSocket('udp4')
30+
var socket2 = dgram.createSocket('udp4')
31+
var received = []
32+
socket.on('message', function (b) {
33+
console.log('receive:', b)
34+
socket.close(function () {})
35+
resolve()
36+
})
37+
t.equal(socket.bind(), socket, 'bind returns this') //any port
38+
39+
socket.on('listening', function () {
40+
var addr = socket.address()
41+
var p = addr.port
42+
t.equal(addr.address, '0.0.0.0')
43+
t.ok(Number.isInteger(addr.port))
44+
socket2.on('error', function (err) {
45+
t.equal(err.code, 'EADDRINUSE', 'address in use err')
46+
socket.close()
47+
resolve()
48+
})
49+
t.equal(socket2.bind(p), socket2)
50+
})
51+
return new Promise(_resolve => resolve = _resolve)
52+
})

test/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ setTimeout(function poll () {
2525
import './console.js'
2626
import './fs.js'
2727
import './os.js'
28+
import './dgram.js'

test/src/os.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as os from '@socketsupply/io/os'
2+
//import os from 'os' //uncomment to tests the tests, should pass running node
23
import { test } from 'tapzero'
34

45
test('os.arch()', (t) => {
@@ -14,6 +15,26 @@ test('os.type()', (t) => {
1415
})
1516

1617
test('os.networkInterfaces()', (t) => {
18+
var int = os.networkInterfaces()
19+
function isAddress (i) {
20+
console.log(i)
21+
if(i.family === 4) {
22+
t.ok(/\d+\.\d+\.\d+\.\d+/.test(i.address))
23+
}
24+
else t.equal(i.family, 6)
25+
26+
t.ok(i.netmask, 'has netmask')
27+
t.ok(i.mac, 'has mac address')
28+
t.equal(typeof i.internal, 'boolean')
29+
t.ok(i.cidr, 'has cidr')
30+
}
31+
32+
t.ok(Array.isArray(int.lo))
33+
var interfaces = Object.keys(int).length
34+
t.ok(interfaces >= 2, 'network interfaces has at least two keys, loopback + wifi, was:'+interfaces)
35+
for(var intf in int) {
36+
int[intf].forEach(isAddress)
37+
}
1738
})
1839

1940
test('os.EOL', (t) => {

0 commit comments

Comments
 (0)