fix encoding of empty string in TXT data - fixes #39

This commit is contained in:
silverwind 2018-07-02 21:50:31 +02:00
parent e29a832aa8
commit db4e271a59
No known key found for this signature in database
GPG Key ID: 2E62B41C93869443
2 changed files with 17 additions and 2 deletions

View File

@ -1238,7 +1238,8 @@ answer.decode = function (buf, offset) {
answer.decode.bytes = 0
answer.encodingLength = function (a) {
return name.encodingLength(a.name) + 8 + renc(a.type).encodingLength(a.data || a.options)
const data = (a.data !== null && a.data !== undefined) ? a.data : a.options
return name.encodingLength(a.name) + 8 + renc(a.type).encodingLength(data)
}
const question = exports.question = {}

16
test.js
View File

@ -221,6 +221,15 @@ tape('response', function (t) {
}]
})
testEncoder(t, packet, {
type: 'response',
answers: [{
type: 'TXT',
name: 'emptytxt.com',
data: ''
}]
})
t.end()
})
@ -497,8 +506,13 @@ function compare (t, a, b) {
if (typeof a === 'object' && a && b) {
const keys = Object.keys(a)
for (let i = 0; i < keys.length; i++) {
if (!compare(t, a[keys[i]], b[keys[i]])) return false
if (!compare(t, a[keys[i]], b[keys[i]])) {
return false
}
}
} else if (Array.isArray(b) && !Array.isArray(a)) {
// TXT always decode as array
return a.toString() === b[0].toString()
} else {
return a === b
}