diceware: add WordListParser and tests
This commit is contained in:
parent
16392b7581
commit
5395b4853b
2 changed files with 41 additions and 0 deletions
|
@ -0,0 +1,21 @@
|
||||||
|
/*
|
||||||
|
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.msfjarvis.aps.passgen.diceware
|
||||||
|
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
|
internal object WordListParser {
|
||||||
|
fun parse(wordlistStream: InputStream) =
|
||||||
|
wordlistStream
|
||||||
|
.bufferedReader()
|
||||||
|
.lineSequence()
|
||||||
|
.map { line -> line.split(DELIMITER) }
|
||||||
|
.filter { items -> items.size == 2 && items[0].toIntOrNull() != null }
|
||||||
|
.map { items -> items[0].toInt() to items[1] }
|
||||||
|
.toMap()
|
||||||
|
|
||||||
|
private const val DELIMITER = "\t"
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
/*
|
||||||
|
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.msfjarvis.aps.passgen.diceware
|
||||||
|
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class WordListParserTest {
|
||||||
|
@Test
|
||||||
|
fun parseWordList() {
|
||||||
|
val stream = "11111\tabcde\n22222\tfghij".byteInputStream()
|
||||||
|
val parsedMap = WordListParser.parse(stream)
|
||||||
|
assertEquals(2, parsedMap.size)
|
||||||
|
assertEquals("abcde", parsedMap[11111])
|
||||||
|
assertEquals("fghij", parsedMap[22222])
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue