2016-03-23 15:07:25 +01:00
|
|
|
/* eslint-env mocha */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
var expect = require('chai').expect
|
|
|
|
var mafmt = require('./../src')
|
|
|
|
|
|
|
|
describe('multiaddr validation', function () {
|
|
|
|
it('basic stuff works', function () {
|
2016-05-17 22:15:32 +01:00
|
|
|
var goodIP = [
|
2016-03-23 15:07:25 +01:00
|
|
|
'/ip4/0.0.0.0',
|
|
|
|
'/ip6/fc00::'
|
|
|
|
]
|
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
var badIP = [
|
2016-03-23 15:07:25 +01:00
|
|
|
'/ip4/0.0.0.0/tcp/555',
|
|
|
|
'/udp/789/ip6/fc00::'
|
|
|
|
]
|
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
var goodTCP = [
|
2016-03-23 15:07:25 +01:00
|
|
|
'/ip4/0.0.7.6/tcp/1234',
|
|
|
|
'/ip6/::/tcp/0'
|
|
|
|
]
|
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
var badTCP = [
|
2016-03-23 15:07:25 +01:00
|
|
|
'/tcp/12345',
|
|
|
|
'/ip6/fc00::/udp/5523/tcp/9543'
|
|
|
|
]
|
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
var goodUDP = [
|
2016-03-23 15:07:25 +01:00
|
|
|
'/ip4/0.0.7.6/udp/1234',
|
|
|
|
'/ip6/::/udp/0'
|
|
|
|
]
|
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
var badUDP = [
|
2016-03-23 15:07:25 +01:00
|
|
|
'/udp/12345',
|
|
|
|
'/ip6/fc00::/tcp/5523/udp/9543'
|
|
|
|
]
|
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
var goodUTP = [
|
2016-03-23 15:07:25 +01:00
|
|
|
'/ip4/1.2.3.4/udp/3456/utp',
|
|
|
|
'/ip6/::/udp/0/utp'
|
|
|
|
]
|
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
var badUTP = [
|
2016-03-23 15:07:25 +01:00
|
|
|
'/ip4/0.0.0.0/tcp/12345/utp',
|
|
|
|
'/ip6/::/ip4/0.0.0.0/udp/1234/utp'
|
|
|
|
]
|
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
var goodWS = [
|
|
|
|
'/ip4/1.2.3.4/tcp/3456/ws',
|
|
|
|
'/ip6/::/tcp/0/ws'
|
2016-03-23 15:07:25 +01:00
|
|
|
]
|
|
|
|
|
2016-05-21 19:57:26 +01:00
|
|
|
var goodWebRTCStar = [
|
|
|
|
'/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws',
|
|
|
|
'/libp2p-webrtc-star/ip6/::/tcp/0/ws'
|
|
|
|
]
|
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
var badWS = [
|
|
|
|
'/ip4/0.0.0.0/tcp/12345/udp/2222/ws',
|
|
|
|
'/ip6/::/ip4/0.0.0.0/udp/1234/ws'
|
2016-03-23 15:07:25 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
function assertMatches (p) {
|
|
|
|
var tests = Array.from(arguments).slice(1)
|
|
|
|
tests.forEach(function (test) {
|
|
|
|
test.forEach(function (testcase) {
|
|
|
|
expect(p.matches(testcase)).to.be.eql(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertMismatches (p) {
|
|
|
|
var tests = Array.from(arguments).slice(1)
|
|
|
|
tests.forEach(function (test) {
|
|
|
|
test.forEach(function (testcase) {
|
|
|
|
expect(p.matches(testcase)).to.be.eql(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
assertMatches(mafmt.IP, goodIP)
|
|
|
|
assertMismatches(mafmt.IP, badIP, goodTCP)
|
2016-03-23 15:07:25 +01:00
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
assertMatches(mafmt.TCP, goodTCP)
|
|
|
|
assertMismatches(mafmt.TCP, badTCP, goodIP)
|
2016-03-23 15:07:25 +01:00
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
assertMatches(mafmt.UDP, goodUDP)
|
|
|
|
assertMismatches(mafmt.UDP, badUDP, goodIP, goodTCP, goodUTP)
|
2016-03-23 15:07:25 +01:00
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
assertMatches(mafmt.UTP, goodUTP)
|
|
|
|
assertMismatches(mafmt.UTP, badUTP, goodIP, goodTCP, goodUDP)
|
2016-03-23 15:07:25 +01:00
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
assertMatches(mafmt.Reliable, goodUTP, goodTCP)
|
|
|
|
assertMismatches(mafmt.Reliable, goodIP, goodUDP)
|
2016-03-23 15:07:25 +01:00
|
|
|
|
2016-05-17 22:15:32 +01:00
|
|
|
assertMatches(mafmt.WebSockets, goodWS)
|
|
|
|
assertMismatches(mafmt.WebSockets, goodIP, goodUDP, badWS)
|
2016-05-21 19:57:26 +01:00
|
|
|
|
|
|
|
assertMatches(mafmt.WebRTCStar, goodWebRTCStar)
|
|
|
|
assertMismatches(mafmt.WebRTCStar, goodIP, goodUDP, badWS)
|
2016-03-23 15:07:25 +01:00
|
|
|
})
|
|
|
|
})
|