Skip to content

Commit

Permalink
fix: reading and writing NULL values
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkpunkd committed May 11, 2020
1 parent 463462c commit fcf979e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/Ber/Reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ class ExtendedReader extends Reader {
return { type: ParameterType.Octets, value: this.readString(UNIVERSAL(4), true) }
case BERDataTypes.RELATIVE_OID:
return { type: ParameterType.String, value: this.readOID(BERDataTypes.RELATIVE_OID) }
case BERDataTypes.NULL:
return { type: ParameterType.Null, value: null } // TODO should you read something
case BERDataTypes.NULL: // Note: No readNull in BER library but writer writes 2 bytes
this.readByte(false) // Read past - ASN1.NULL tag 0x05
this.readByte(false) // and - 0x00 length
return { type: ParameterType.Null, value: null }
default:
throw new UnimplementedEmberTypeError(tag)
}
Expand Down
10 changes: 8 additions & 2 deletions src/Ber/Writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ class ExtendedWriter extends Writer {
} else {
value = arg1 as EmberValue
}
// this is inconsistent with the original behavior, which would have thrown a TypeError if value was null or undef
// TypeScript won't allow doing a value.toString() if value can be null, not sure what to do here

if (tag === BERDataTypes.NULL && (value === null || value === undefined)) {
this.writeNull()
return
}
if (value === null || value === undefined) {
this.writeNull()
return
}

Expand Down Expand Up @@ -230,6 +234,8 @@ function parameterTypetoBERTAG(parameterType: ParameterType): number {
return BERDataTypes.INTEGER // TODO: guess
case ParameterType.Octets:
return BERDataTypes.OCTETSTRING
case ParameterType.Null:
return BERDataTypes.NULL
default:
throw new Error(``)
}
Expand Down
2 changes: 1 addition & 1 deletion src/encodings/ber/__tests__/StreamEntry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('encodings/ber/StreamEntry', () => {
expect(decoded).toEqual(se)
})

test('write and read stream entry - null', () => {
test.only('write and read stream entry - null', () => {
const se = {
identifier: 42,
value: { type: ParameterType.Null, value: null }
Expand Down
2 changes: 0 additions & 2 deletions src/encodings/ber/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ describe('encoders/Ber/index', () => {
]
if (!res[0].children) {
fail(`Tree must have children`)
return
}
res[0].children[0].parent = res[0]
roundTrip(res, RootType.Elements)
Expand All @@ -46,7 +45,6 @@ describe('encoders/Ber/index', () => {
]
if (!res[0].children) {
fail(`Tree must have children`)
return
}
res[0].children[0].parent = res[0]
roundTrip(res, RootType.Elements)
Expand Down
5 changes: 3 additions & 2 deletions src/encodings/ber/encoder/Command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as Ber from '../../../Ber'
import { Command, CommandType, GetDirectory, FieldFlags, Invoke } from '../../../model/Command'
import { encodeInvocation } from './Invocation'
import { CommandBERID } from '../constants'

export function encodeCommand(el: Command, writer: Ber.Writer): void {
writer.startSequence(Ber.APPLICATION(2)) // TODO - make non magic number?
writer.startSequence(CommandBERID) // TODO - make non magic number?

writer.startSequence(Ber.CONTEXT(0))
writer.writeInt(el.number)
Expand All @@ -21,7 +22,7 @@ export function encodeCommand(el: Command, writer: Ber.Writer): void {
writer.endSequence()
}

writer.endSequence() // BER.APPLICATION(2)
writer.endSequence() // CommandBERID
}

function isInvoke(command: Command): command is Invoke {
Expand Down

0 comments on commit fcf979e

Please sign in to comment.