Skip to content

Commit

Permalink
fix(): reject write call when value contains invalid data
Browse files Browse the repository at this point in the history
  • Loading branch information
pwespi committed Jun 11, 2021
1 parent 77ca3ca commit 24e0c7a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ fun bytesToString(bytes: ByteArray): String {
}

fun stringToBytes(value: String): ByteArray {
if (value == "") {
val bytes = ByteArray(1)
bytes[0] = 0
return bytes
}
val hexValues = value.split(" ")
val bytes = ByteArray(hexValues.size)
for (i in hexValues.indices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ class Device(
reject(key, "Characteristic not found.")
return
}
if (value == "") {
reject(key, "Invalid data.")
return
}
val bytes = stringToBytes(value)
characteristic.value = bytes
characteristic.writeType = writeType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ class ConversionKtTest : TestCase() {
}
}

fun testEmptyStringToBytes() {
val input = ""
val output = stringToBytes(input)
val expected = byteArrayOfInts(0x00)
expected.forEachIndexed { index, byte ->
assertEquals(byte, output[index])
}
}

fun testHexToByte() {
assertEquals(0.toByte(), hexToByte("00"))
assertEquals(205.toByte(), hexToByte("CD"))
Expand Down
4 changes: 4 additions & 0 deletions ios/Plugin/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ class Device: NSObject, CBPeripheralDelegate {
self.reject(key, "Characteristic not found.")
return
}
if value == "" {
self.reject(key, "Invalid data.")
return
}
let data: Data = stringToData(value)
self.peripheral.writeValue(data, for: characteristic, type: writeType)
if writeType == CBCharacteristicWriteType.withResponse {
Expand Down
9 changes: 9 additions & 0 deletions ios/PluginTests/ConversionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class ConversionTests: XCTestCase {
}
}

func testEmptyStringToData() throws {
let input = ""
let output = stringToData(input)
let expected = Data([0x00])
for (index, byte) in output.enumerated() {
XCTAssertEqual(byte, expected[index])
}
}

func testCbuuidToString() throws {
XCTAssertEqual("0000180d-0000-1000-8000-00805f9b34fb", cbuuidToString(CBUUID(string: "180D")))
XCTAssertEqual("fb005c80-02e7-f387-1cad-8acd2d8df0c8", cbuuidToString(CBUUID(string: "fb005c80-02e7-f387-1cad-8acd2d8df0c8")))
Expand Down
6 changes: 6 additions & 0 deletions src/bleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ class BleClientClass implements BleClientInterface {
value: DataView,
): Promise<void> {
return this.queue(async () => {
if (!value?.buffer) {
throw new Error('Invalid data.');
}
let writeValue: DataView | string = value;
if (Capacitor.getPlatform() !== 'web') {
// on native we can only write strings
Expand All @@ -349,6 +352,9 @@ class BleClientClass implements BleClientInterface {
value: DataView,
): Promise<void> {
await this.queue(async () => {
if (!value?.buffer) {
throw new Error('Invalid data.');
}
let writeValue: DataView | string = value;
if (Capacitor.getPlatform() !== 'web') {
// on native we can only write strings
Expand Down

0 comments on commit 24e0c7a

Please sign in to comment.