Skip to content

Commit

Permalink
Implement unit tests for QRCodeOnboardingPayload (#27587)
Browse files Browse the repository at this point in the history
* Implement unit tests for QRCodeOnboardingPayload

* Restyled by gn

* Address review comments

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Aug 21, 2023
1 parent 54500e8 commit 1102094
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/java-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ jobs:
chip.tlv.TlvReadWriteTest \
chip.tlv.TlvReaderTest \
chip.jsontlv.JsonToTlvToJsonTest \
chip.onboardingpayload.ManualCodeTest
chip.onboardingpayload.ManualCodeTest \
chip.onboardingpayload.QRCodeTest
- name: Build Java Matter Controller and all clusters app
run: |
scripts/run_in_build_env.sh './scripts/build_python.sh --install_virtual_env out/venv'
Expand Down
6 changes: 6 additions & 0 deletions kotlin-detect-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ style:
examples/android/CHIPTest/app/src/androidTest/java/com/tcl/chip/chiptest/ExampleInstrumentedTest.kt"
- "**/src/controller/java/tests/chip/jsontlv/JsonToTlvToJsonTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
- "**/src/controller/java/tests/chip/tlv/TlvReaderTest.kt"
- "**/src/controller/java/tests/chip/tlv/TlvReadWriteTest.kt"
- "**/src/controller/java/tests/chip/tlv/TlvWriterTest.kt"
Expand Down Expand Up @@ -128,6 +129,7 @@ style:
- "**/src/controller/java/src/chip/onboardingpayload/OnboardingPayload.kt"
- "**/src/controller/java/src/chip/onboardingpayload/QRCodeBasicOnboardingPayloadGenerator.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
UnusedParameter:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/bluetooth/BluetoothManager.kt"
Expand Down Expand Up @@ -201,6 +203,7 @@ exceptions:
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Argument.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/Main.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
SwallowedException:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/SensorClientFragment.kt"
Expand All @@ -221,6 +224,7 @@ naming:
- "**/src/controller/java/tests/chip/tlv/TlvReaderTest.kt"
- "**/src/controller/java/tests/chip/jsontlv/JsonToTlvToJsonTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
- "**/examples/android/CHIPTest/app/src/test/java/com/tcl/chip/chiptest/ExampleUnitTest.kt"
ConstructorParameterNaming:
excludes:
Expand Down Expand Up @@ -264,6 +268,7 @@ complexity:
- "**/src/controller/java/src/chip/tlv/TlvWriter.kt"
- "**/src/controller/java/tests/chip/jsontlv/JsonToTlvToJsonTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
- "**/src/controller/java/tests/chip/tlv/TlvReadWriteTest.kt"
NestedBlockDepth:
excludes:
Expand All @@ -282,6 +287,7 @@ complexity:
- "**/src/controller/java/src/chip/onboardingpayload/ManualOnboardingPayloadParser.kt"
- "**/src/controller/java/tests/chip/jsontlv/JsonToTlvToJsonTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
- "**/src/controller/java/tests/chip/tlv/TlvReaderTest.kt"
LargeClass:
excludes:
Expand Down
15 changes: 15 additions & 0 deletions src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,25 @@ kotlin_library("onboardingpayload_manual_code_test") {
kotlinc_flags = [ "-Xlint:deprecation" ]
}

kotlin_library("onboardingpayload_qr_code_test") {
output_name = "OnboardingPayloadQRCodeTest.jar"

deps = [
":onboarding_payload",
"${chip_root}/third_party/java_deps:junit-4",
"${chip_root}/third_party/java_deps:truth",
]

sources = [ "tests/chip/onboardingpayload/QRCodeTest.kt" ]

kotlinc_flags = [ "-Xlint:deprecation" ]
}

group("unit_tests") {
deps = [
":json_to_tlv_to_json_test",
":onboardingpayload_manual_code_test",
":onboardingpayload_qr_code_test",
":tlv_read_write_test",
":tlv_reader_test",
":tlv_writer_test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,6 @@ import java.util.concurrent.atomic.AtomicInteger
* to a OnboardingPayload object
*/
class QRCodeOnboardingPayloadParser(private val mBase38Representation: String) {
private fun extractPayload(inString: String): String {
var chipSegment = ""
val delimiter = '%'
val startIndices = mutableListOf<Int>()
startIndices.add(0)

for (i in inString.indices) {
if (inString[i] == delimiter) {
startIndices.add(i + 1)
}
}

// Find the first string between delimiters that starts with kQRCodePrefix
for (i in 0 until startIndices.size) {
val startIndex = startIndices[i]
val endIndex = if (i == startIndices.size - 1) inString.length else startIndices[i + 1] - 1
val length = if (endIndex != inString.length) endIndex - startIndex else inString.length
val segment = inString.substring(startIndex, startIndex + length)

// Find a segment that starts with kQRCodePrefix
if (segment.startsWith(kQRCodePrefix) && segment.length > kQRCodePrefix.length) {
chipSegment = segment
break
}
}

if (chipSegment.length > 0) {
return chipSegment.substring(kQRCodePrefix.length) // strip out prefix before returning
}

return chipSegment
}

fun populatePayload(outPayload: OnboardingPayload) {
var indexToReadFrom: AtomicInteger = AtomicInteger(0)

Expand Down Expand Up @@ -113,5 +80,14 @@ class QRCodeOnboardingPayloadParser(private val mBase38Representation: String) {
index.addAndGet(numberOfBitsToRead)
return dest
}

fun extractPayload(inString: String?): String {
return inString
?.split('%')
?.filter { s -> s.startsWith(kQRCodePrefix) }
?.firstOrNull()
?.substring(kQRCodePrefix.length)
?: ""
}
}
}
Loading

0 comments on commit 1102094

Please sign in to comment.