Merge pull request #2022 from vitorpamplona/claude/nostr-ble-messaging-i2rrJ
Add NIP-BE Bluetooth Low Energy mesh networking support
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nipBEBle
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class BleRoleTest {
|
||||
@Test
|
||||
fun higherUuidBecomesServer() {
|
||||
val high = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"
|
||||
val low = "00000000-0000-0000-0000-000000000000"
|
||||
assertEquals(BleRole.SERVER, assignRole(high, low))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun lowerUuidBecomesClient() {
|
||||
val high = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"
|
||||
val low = "00000000-0000-0000-0000-000000000000"
|
||||
assertEquals(BleRole.CLIENT, assignRole(low, high))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rolesAreComplementary() {
|
||||
val a = "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA"
|
||||
val b = "BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB"
|
||||
|
||||
val roleA = assignRole(a, b)
|
||||
val roleB = assignRole(b, a)
|
||||
|
||||
assertEquals(BleRole.CLIENT, roleA)
|
||||
assertEquals(BleRole.SERVER, roleB)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun permanentServerUuid() {
|
||||
val serverUuid = BleConfig.PERMANENT_SERVER_UUID
|
||||
val anyOther = "12345678-1234-1234-1234-123456789012"
|
||||
assertEquals(BleRole.SERVER, assignRole(serverUuid, anyOther))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun permanentClientUuid() {
|
||||
val clientUuid = BleConfig.PERMANENT_CLIENT_UUID
|
||||
val anyOther = "12345678-1234-1234-1234-123456789012"
|
||||
assertEquals(BleRole.CLIENT, assignRole(clientUuid, anyOther))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun caseInsensitiveComparison() {
|
||||
val lower = "abcdef12-3456-7890-abcd-ef1234567890"
|
||||
val upper = "ABCDEF12-3456-7890-ABCD-EF1234567890"
|
||||
val other = "00000000-0000-0000-0000-000000000001"
|
||||
|
||||
assertEquals(assignRole(lower, other), assignRole(upper, other))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun equalUuidsDefaultToClient() {
|
||||
val uuid = "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA"
|
||||
assertEquals(BleRole.CLIENT, assignRole(uuid, uuid))
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nipBEBle.protocol
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
|
||||
class BleChunkAssemblerTest {
|
||||
@Test
|
||||
fun singleChunkMessage() {
|
||||
val assembler = BleChunkAssembler()
|
||||
val message = """["OK","abc",true]"""
|
||||
val chunks = BleMessageChunker.splitIntoChunks(message)
|
||||
assertEquals(1, chunks.size)
|
||||
|
||||
val result = assembler.addChunk(chunks[0])
|
||||
assertNotNull(result)
|
||||
assertEquals(message, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun multiChunkMessage() {
|
||||
val assembler = BleChunkAssembler()
|
||||
val message = """["EVENT",{"content":"hello world from nostr ble mesh networking"}]"""
|
||||
val chunks = BleMessageChunker.splitIntoChunks(message, chunkSize = 10)
|
||||
assert(chunks.size > 1)
|
||||
|
||||
for (i in 0 until chunks.size - 1) {
|
||||
val result = assembler.addChunk(chunks[i])
|
||||
assertNull(result, "Should not produce a message until all chunks received")
|
||||
}
|
||||
|
||||
val result = assembler.addChunk(chunks.last())
|
||||
assertNotNull(result)
|
||||
assertEquals(message, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resetClearsPartialChunks() {
|
||||
val assembler = BleChunkAssembler()
|
||||
val message = """["EVENT",{"content":"hello nostr ble mesh networking data"}]"""
|
||||
val chunks = BleMessageChunker.splitIntoChunks(message, chunkSize = 10)
|
||||
|
||||
assembler.addChunk(chunks[0])
|
||||
assembler.reset()
|
||||
|
||||
// After reset, should accept a new message cleanly
|
||||
val newMessage = """["OK"]"""
|
||||
val newChunks = BleMessageChunker.splitIntoChunks(newMessage)
|
||||
val result = assembler.addChunk(newChunks[0])
|
||||
assertNotNull(result)
|
||||
assertEquals(newMessage, result)
|
||||
}
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nipBEBle.protocol
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFails
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class BleMessageChunkerTest {
|
||||
@Test
|
||||
fun roundTripShortMessage() {
|
||||
val message = """["EVENT",{"id":"abc","kind":1,"content":"hello"}]"""
|
||||
val chunks = BleMessageChunker.splitIntoChunks(message)
|
||||
val result = BleMessageChunker.joinChunks(chunks)
|
||||
assertEquals(message, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun roundTripMultiChunkMessage() {
|
||||
// Use a small chunk size to force multiple chunks even with compressed data
|
||||
val message = """["EVENT",{"id":"abc","kind":1,"content":"hello world from nostr ble"}]"""
|
||||
val chunks = BleMessageChunker.splitIntoChunks(message, chunkSize = 10)
|
||||
assertTrue(chunks.size > 1, "Should produce multiple chunks with chunkSize=10")
|
||||
val result = BleMessageChunker.joinChunks(chunks)
|
||||
assertEquals(message, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun chunksHaveCorrectFormat() {
|
||||
val message = """["EVENT",{"id":"abc","kind":1,"content":"hello world"}]"""
|
||||
val chunkSize = 20
|
||||
val chunks = BleMessageChunker.splitIntoChunks(message, chunkSize = chunkSize)
|
||||
|
||||
for ((i, chunk) in chunks.withIndex()) {
|
||||
// First byte is the index
|
||||
assertEquals(i, BleMessageChunker.chunkIndex(chunk))
|
||||
// Last byte is the total count
|
||||
assertEquals(chunks.size, BleMessageChunker.totalChunks(chunk))
|
||||
// Each chunk should be at most chunkSize + 2 (overhead)
|
||||
assertTrue(
|
||||
chunk.size <= chunkSize + BleMessageChunker.CHUNK_OVERHEAD,
|
||||
"Chunk size ${chunk.size} exceeds max ${chunkSize + BleMessageChunker.CHUNK_OVERHEAD}",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isCompleteReturnsFalseForPartialChunks() {
|
||||
val message = """["EVENT",{"id":"abc","kind":1,"content":"hello world from nostr ble"}]"""
|
||||
val chunks = BleMessageChunker.splitIntoChunks(message, chunkSize = 10)
|
||||
assertTrue(chunks.size > 1, "Need multiple chunks for this test")
|
||||
|
||||
val partial = listOf(chunks[0])
|
||||
assertFalse(BleMessageChunker.isComplete(partial))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isCompleteReturnsTrueWhenAllChunksReceived() {
|
||||
val message = """["EVENT",{"id":"abc","kind":1,"content":"hello"}]"""
|
||||
val chunks = BleMessageChunker.splitIntoChunks(message, chunkSize = 50)
|
||||
assertTrue(BleMessageChunker.isComplete(chunks.toList()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun joinChunksHandlesOutOfOrderDelivery() {
|
||||
val message = """["EVENT",{"id":"abc123","kind":1,"content":"nostr ble mesh networking"}]"""
|
||||
val chunks = BleMessageChunker.splitIntoChunks(message, chunkSize = 10)
|
||||
assertTrue(chunks.size > 2, "Need 3+ chunks for out-of-order test")
|
||||
|
||||
// Reverse the order
|
||||
val reversed = chunks.reversed().toTypedArray()
|
||||
val result = BleMessageChunker.joinChunks(reversed)
|
||||
assertEquals(message, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun singleChunkForSmallMessage() {
|
||||
val message = """["OK"]"""
|
||||
val chunks = BleMessageChunker.splitIntoChunks(message, chunkSize = 500)
|
||||
assertEquals(1, chunks.size)
|
||||
assertEquals(0, BleMessageChunker.chunkIndex(chunks[0]))
|
||||
assertEquals(1, BleMessageChunker.totalChunks(chunks[0]))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rejectsOversizedMessage() {
|
||||
// Generate data that compresses poorly by using all 256 byte values in a
|
||||
// pseudo-random pattern. A 512KB input with this pattern stays above 64KB
|
||||
// after DEFLATE compression.
|
||||
val size = 512 * 1024
|
||||
val sb = StringBuilder(size)
|
||||
var state = 7919L // prime seed
|
||||
for (i in 0 until size) {
|
||||
// Simple LCG to produce varied characters
|
||||
state = (state * 1103515245L + 12345L) and 0x7FFFFFFF
|
||||
sb.append(('!' + (state % 94).toInt())) // printable ASCII range
|
||||
}
|
||||
val message = sb.toString()
|
||||
assertFails {
|
||||
BleMessageChunker.splitIntoChunks(message)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun customChunkSize() {
|
||||
val message = """["EVENT",{"content":"${"a".repeat(100)}"}]"""
|
||||
val smallChunks = BleMessageChunker.splitIntoChunks(message, chunkSize = 10)
|
||||
val largeChunks = BleMessageChunker.splitIntoChunks(message, chunkSize = 500)
|
||||
assertTrue(smallChunks.size > largeChunks.size)
|
||||
|
||||
// Both should produce the same result
|
||||
assertEquals(
|
||||
BleMessageChunker.joinChunks(smallChunks),
|
||||
BleMessageChunker.joinChunks(largeChunks),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.utils
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class DeflateTest {
|
||||
@Test
|
||||
fun roundTripSimpleBytes() {
|
||||
val original = "Hello, NIP-BE!".encodeToByteArray()
|
||||
val result = Deflate.decompress(Deflate.compress(original))
|
||||
assertContentEquals(original, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun roundTripEmptyBytes() {
|
||||
val original = ByteArray(0)
|
||||
val result = Deflate.decompress(Deflate.compress(original))
|
||||
assertContentEquals(original, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun roundTripLargeRepetitiveData() {
|
||||
val original = "abcdefghij".repeat(1000).encodeToByteArray()
|
||||
val compressed = Deflate.compress(original)
|
||||
val result = Deflate.decompress(compressed)
|
||||
assertContentEquals(original, result)
|
||||
assertTrue(compressed.size < original.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun roundTripNostrJsonMessage() {
|
||||
val original = """["EVENT",{"id":"abc","pubkey":"def","created_at":1700000000,"kind":1,"tags":[],"content":"hello","sig":"ghi"}]""".encodeToByteArray()
|
||||
val result = Deflate.decompress(Deflate.compress(original))
|
||||
assertContentEquals(original, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun compressedOutputIsRawDeflate() {
|
||||
val compressed = Deflate.compress("test".encodeToByteArray())
|
||||
// Raw DEFLATE does NOT start with gzip magic bytes (0x1F 0x8B)
|
||||
if (compressed.size >= 2) {
|
||||
val isGzip = compressed[0] == 0x1F.toByte() && compressed[1] == 0x8B.toByte()
|
||||
assertTrue(!isGzip, "Expected raw DEFLATE, not gzip format")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun roundTripUnicodeContent() {
|
||||
val original = "こんにちは世界 🌍 Nostr BLE".encodeToByteArray()
|
||||
val result = Deflate.decompress(Deflate.compress(original))
|
||||
assertContentEquals(original, result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user