Skip to content

Commit

Permalink
MOAR spec
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdionysus committed May 15, 2020
1 parent 7a45ce2 commit 7e38e12
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function main () {
var handle = () => {
logger.debug('SIGTERM/SIGINT Received - Stopping')
svr.stop()
logger.info('Stopped')
process.exit()
}

Expand Down
27 changes: 15 additions & 12 deletions lib/DNSServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class DNSServer {
if (this._running) return
this._running = true
this._socket.bind(this.port)
this.logger.debug('Started on UDP Port ' + this.port)
this.logger.info('Started on UDP Port ' + this.port)
}

stop () {
if (!this._running) return
this._socket.close()
this._running = false
this.logger.info('Stopped')
}

_onMessage (msg, rinfo) {
Expand Down Expand Up @@ -112,19 +113,21 @@ class DNSServer {
qclass: DNSPacket.QClass.IN,
ttl: row.ttl
}
switch (row.rrtype) {
case 'A': return this._getRRDataA(row, out)
case 'MX': return this._getRRDataMX(row, out)
case 'NS': return this._getRRDataNS(row, out)
case 'SOA': return this._getRRDataSOA(row, out)
case 'CNAME': return this._getRRDataCNAME(row, out)
case 'PTR': return this._getRRDataPTR(row, out)
case 'TXT': return this._getRRDataTXT(row, out)
case 'SRV': return this._getRRDataSRV(row, out)
case 'AAAA': return this._getRRDataAAAA(row, out)
switch (DNSPacket.QType[row.rrtype]) {
case DNSPacket.QType.A: return this._getRRDataA(row, out)
case DNSPacket.QType.MX: return this._getRRDataMX(row, out)
case DNSPacket.QType.NS: return this._getRRDataNS(row, out)
case DNSPacket.QType.SOA: return this._getRRDataSOA(row, out)
case DNSPacket.QType.CNAME: return this._getRRDataCNAME(row, out)
case DNSPacket.QType.PTR: return this._getRRDataPTR(row, out)
case DNSPacket.QType.TXT: return this._getRRDataTXT(row, out)
case DNSPacket.QType.SRV: return this._getRRDataSRV(row, out)
case DNSPacket.QType.AAAA: return this._getRRDataAAAA(row, out)
default:
this.logger.info('Unknown Resource Record CTYPE ' + row.rrtype + ' - ignoring')
}
} catch (e) {
this.logger.error('Database Parse Error: Resource Record ' + row.id + ' is malformed.')
this.logger.warn('Database Parse Error: Resource Record ' + row.id + ' is malformed.')
}
return null
}
Expand Down
58 changes: 57 additions & 1 deletion spec/DNSServer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,66 @@
const DNSServer = require('../lib/DNSServer')

describe('DNSServer', () => {
var x1

beforeEach(()=>{
x1 = new DNSServer()
})

it('should allow New', () => {
var x1 = new DNSServer()
var x2 = new DNSServer()

expect(x1).not.toBe(x2)
})

describe('start', ()=>{
it('should call _socket.bind and set _running true',()=>{
spyOn(x1._socket,'bind')
spyOn(x1.logger,'info')

x1.start()

expect(x1._running).toEqual(true)
expect(x1._socket.bind).toHaveBeenCalledWith(53)
expect(x1.logger.info).toHaveBeenCalledWith('Started on UDP Port 53')
})

it('should return if _running true',()=>{
spyOn(x1._socket,'bind')
spyOn(x1.logger,'info')

x1._running = true
x1.start()

expect(x1._running).toEqual(true)
expect(x1._socket.bind).not.toHaveBeenCalled()
expect(x1.logger.info).not.toHaveBeenCalled()
})
})

describe('stop', ()=>{
it('should call _socket.close and set _running false',()=>{
spyOn(x1._socket,'close')
spyOn(x1.logger,'info')

x1._running = true
x1.stop()

expect(x1._running).toEqual(false)
expect(x1._socket.close).toHaveBeenCalledWith()
expect(x1.logger.info).toHaveBeenCalledWith('Stopped')
})

it('should return if _running false',()=>{
spyOn(x1._socket,'close')
spyOn(x1.logger,'info')

x1._running = false
x1.stop()

expect(x1._running).toEqual(false)
expect(x1._socket.close).not.toHaveBeenCalled()
expect(x1.logger.info).not.toHaveBeenCalled()
})
})
})

0 comments on commit 7e38e12

Please sign in to comment.