Skip to content

Commit

Permalink
Validator.ktl: Fixed bug inside parsing node URL and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzopalazzo committed Jul 28, 2020
1 parent f9336e8 commit a274b2c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
buildToolsVersion "29.0.2"
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.lvaccaro.lamp"
minSdkVersion 19
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/com/lvaccaro/lamp/util/Validator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import android.util.Log
import com.lvaccaro.lamp.LightningCli
import com.lvaccaro.lamp.toJSONObject
import java.nio.charset.Charset
import java.util.*
import kotlin.collections.HashMap

Expand Down Expand Up @@ -66,8 +65,9 @@ class Validator {
// Match with the pattern STRING:STRING
if (tokenizer.countTokens() == 2) {
//The variable tmp will contain all trash (not util) information
var tmp = tokenizer.nextToken();
var tmp = tokenizer.nextToken()
Log.d(TAG, "**** Bitcoin protocol: ${tmp} *********")
if(!isProtocolPrefix(tmp)) return result;
val queryString = tokenizer.nextToken()
Log.d(TAG, "**** bitcoin URI: ${queryString} *********")
// Reassign the tokenizer variable a new token object
Expand Down Expand Up @@ -98,6 +98,11 @@ class Validator {
return result
}

private fun isProtocolPrefix(protocol: String?): Boolean {
return protocol.equals("bitcoin", false) ||
protocol.equals("lightning", false)
}

private fun parseParameter(uri: String, result: HashMap<String, String>): Boolean{
val tokes = StringTokenizer(uri, "=")
if (tokes.countTokens() == 2) {
Expand Down
59 changes: 59 additions & 0 deletions app/src/test/java/com/lvaccaro/lamp/ValidatorUnitTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,63 @@ class ValidatorUnitTest {
assertFalse(result)
}

@Test
fun isNodeID_testFive() {
val nodeid =
"""
0219f8900ee78a89f050c24d8b69492954f9fdbabed753710845eb75d3a75a5880@139.162.159.79:9735
""".trimIndent().trim()
val result = Validator.isLightningNodURI(nodeid)
assertTrue(result)
}

@Test
fun isCorrectValue_testOne() {
val nodeid =
"""
0219f8900ee78a89f050c24d8b69492954f9fdbabed753710845eb75d3a75a5880@139.162.159.79:9735
""".trimIndent().trim()
val bitcoinAddress = Validator.isBitcoinAddress(nodeid)
val bitcoinURI = Validator.isBitcoinURL(nodeid)
val bolt11 = Validator.isBolt11(nodeid)
val nodeURL = Validator.isLightningNodURI(nodeid)
assertFalse(bitcoinAddress)
assertFalse(bitcoinURI)
assertFalse(bolt11)
assertTrue(nodeURL)
}

@Test
fun isCorrectValue_testTwo() {
val invoice =
"""
lnbc2500u1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpquwpc4curk03c9
wlrswe78q4eyqc7d8d0xqzpuyk0sg5g70me25alkluzd2x62aysf2pyy8edtjeevuv4p2d5p76r4zkmneet7
uvyakky2zr4cusd45tftc9c5fh0nnqpnl2jfll544esqchsrny
""".trimIndent().trim()
val bitcoinAddress = Validator.isBitcoinAddress(invoice)
val bitcoinURI = Validator.isBitcoinURL(invoice)
val bolt11 = Validator.isBolt11(invoice)
val nodeURL = Validator.isLightningNodURI(invoice)
assertFalse(bitcoinAddress)
assertFalse(bitcoinURI)
assertTrue(bolt11)
assertFalse(nodeURL)
}

@Test
fun isCorrectValue_testThree() {
val adderss =
"""
bcrt1q7ezrtfqx2w4wg6vmlsk9uaxqeuxgpewdn09zc8
""".trimIndent().trim()
val bitcoinAddress = Validator.isBitcoinAddress(adderss)
val bitcoinURI = Validator.isBitcoinURL(adderss)
val bolt11 = Validator.isBolt11(adderss)
val nodeURL = Validator.isLightningNodURI(adderss)
assertTrue(bitcoinAddress)
assertFalse(bitcoinURI)
assertFalse(bolt11)
assertFalse(nodeURL)
}
}

0 comments on commit a274b2c

Please sign in to comment.