V0.4.0加入锁定二维码
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.qft.optical
|
||||
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class LockRectTest {
|
||||
@Test fun finderPointsProduceBoundedSquareRoi() {
|
||||
val lock = LockRect.fromPoints(
|
||||
listOf(300f to 180f, 660f to 180f, 300f to 540f),
|
||||
960,
|
||||
720,
|
||||
) ?: error("lock missing")
|
||||
assertTrue(lock.width == lock.height)
|
||||
assertTrue(lock.left >= 0 && lock.top >= 0)
|
||||
assertTrue(lock.right <= 960 && lock.bottom <= 720)
|
||||
assertTrue(lock.areaFraction(960, 720) < 0.36f)
|
||||
}
|
||||
|
||||
@Test fun missesCanExpandRoiWithoutLeavingFrame() {
|
||||
val base = LockRect(250, 100, 420, 420)
|
||||
val expanded = base.expand(960, 540, 1.35f)
|
||||
assertTrue(expanded.width >= base.width)
|
||||
assertTrue(expanded.left >= 0 && expanded.top >= 0)
|
||||
assertTrue(expanded.right <= 960 && expanded.bottom <= 540)
|
||||
}
|
||||
|
||||
@Test fun smoothingReducesLockJitter() {
|
||||
val a = LockRect(100, 100, 300, 300)
|
||||
val b = LockRect(140, 130, 340, 340)
|
||||
val smoothed = a.smoothToward(b, 0.25f)
|
||||
assertTrue(smoothed.left in 101..139)
|
||||
assertTrue(smoothed.top in 101..129)
|
||||
assertTrue(smoothed.width in 301..339)
|
||||
}
|
||||
}
|
||||
@@ -4,35 +4,30 @@ import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.util.Base64
|
||||
import java.util.zip.CRC32
|
||||
|
||||
class ProtocolTest {
|
||||
@Test
|
||||
fun parsesDataFrameAndRejectsCorruption() {
|
||||
fun base45KnownVectors() {
|
||||
assertArrayEquals("AB".toByteArray(), Base45.decode("BB8"))
|
||||
assertArrayEquals("Hello!!".toByteArray(), Base45.decode("%69 VD92EX0"))
|
||||
assertArrayEquals("base-45".toByteArray(), Base45.decode("UJCLQE7W581"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesQft2DataFrameAndRejectsCorruption() {
|
||||
val payload = byteArrayOf(1, 2, 3, 4, 5)
|
||||
val text = encode(kind = 2, session = 7L, seq = 9, total = 11, payload = payload)
|
||||
val text = encodeQft2(kind = 2, session = 7L, seq = 9, total = 11, payload = payload)
|
||||
val f = QftProtocol.parseText(text)!!
|
||||
assertEquals(FrameKind.DATA, f.kind)
|
||||
assertEquals(7L, f.sessionId)
|
||||
assertEquals(9L, f.seq)
|
||||
assertArrayEquals(payload, f.payload)
|
||||
|
||||
val corrupted = text.dropLast(1) + if (text.last() == 'A') "B" else "A"
|
||||
val corrupted = text.dropLast(1) + if (text.last() == '0') "1" else "0"
|
||||
assertNull(QftProtocol.parseText(corrupted))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesCrossLanguageKnownVector() {
|
||||
val text = "QFT1:UUZUMQECAQIDBAUGBwgAAAADAAAACgAJaGVsbG8tcWZ0XhNTAg"
|
||||
val f = QftProtocol.parseText(text)!!
|
||||
assertEquals(FrameKind.DATA, f.kind)
|
||||
assertEquals(0x0102030405060708L, f.sessionId)
|
||||
assertEquals(3L, f.seq)
|
||||
assertEquals(10L, f.totalChunks)
|
||||
assertEquals("hello-qft", f.payload.toString(Charsets.UTF_8))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesManifestSenderDevice() {
|
||||
val name = "test.txt".toByteArray()
|
||||
@@ -41,26 +36,25 @@ class ProtocolTest {
|
||||
val payload = ByteBuffer.allocate(8 + 2 + 32 + 2 + name.size + 2 + device.size)
|
||||
.order(ByteOrder.BIG_ENDIAN)
|
||||
.putLong(1234L)
|
||||
.putShort(700.toShort())
|
||||
.putShort(450.toShort())
|
||||
.put(sha)
|
||||
.putShort(name.size.toShort())
|
||||
.put(name)
|
||||
.putShort(device.size.toShort())
|
||||
.put(device)
|
||||
.array()
|
||||
val text = encode(kind = 1, session = 77L, seq = 0, total = 2, payload = payload)
|
||||
val frame = QftProtocol.parseText(text)!!
|
||||
val manifest = QftProtocol.parseManifest(frame)!!
|
||||
val text = encodeQft2(kind = 1, session = 77L, seq = 0, total = 3, payload = payload)
|
||||
val manifest = QftProtocol.parseManifest(QftProtocol.parseText(text)!!)!!
|
||||
assertEquals("DESKTOP-ZHY", manifest.senderDevice)
|
||||
assertEquals("test.txt", manifest.fileName)
|
||||
assertEquals(1234L, manifest.fileSize)
|
||||
assertEquals(700, manifest.chunkSize)
|
||||
assertEquals(450, manifest.chunkSize)
|
||||
}
|
||||
|
||||
private fun encode(kind: Int, session: Long, seq: Int, total: Int, payload: ByteArray): String {
|
||||
private fun encodeQft2(kind: Int, session: Long, seq: Int, total: Int, payload: ByteArray): String {
|
||||
val bb = ByteBuffer.allocate(24 + payload.size + 4).order(ByteOrder.BIG_ENDIAN)
|
||||
bb.put("QFT1".toByteArray())
|
||||
bb.put(1)
|
||||
bb.put("QFT2".toByteArray())
|
||||
bb.put(2)
|
||||
bb.put(kind.toByte())
|
||||
bb.putLong(session)
|
||||
bb.putInt(seq)
|
||||
@@ -70,6 +64,25 @@ class ProtocolTest {
|
||||
val bodyLen = bb.position()
|
||||
val crc = CRC32().apply { update(bb.array(), 0, bodyLen) }.value
|
||||
bb.putInt(crc.toInt())
|
||||
return "QFT1:" + Base64.getUrlEncoder().withoutPadding().encodeToString(bb.array())
|
||||
return "QFT2:" + base45Encode(bb.array())
|
||||
}
|
||||
|
||||
private fun base45Encode(data: ByteArray): String {
|
||||
val alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
|
||||
val out = StringBuilder((data.size * 3 + 1) / 2)
|
||||
var i = 0
|
||||
while (i + 1 < data.size) {
|
||||
val value = (data[i].toInt() and 0xff) * 256 + (data[i + 1].toInt() and 0xff)
|
||||
out.append(alphabet[value % 45])
|
||||
out.append(alphabet[(value / 45) % 45])
|
||||
out.append(alphabet[value / 2025])
|
||||
i += 2
|
||||
}
|
||||
if (i < data.size) {
|
||||
val value = data[i].toInt() and 0xff
|
||||
out.append(alphabet[value % 45])
|
||||
out.append(alphabet[value / 45])
|
||||
}
|
||||
return out.toString()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user