From 5395b4853bac4bc7f3683d1122dd9b234b6f4174 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 10 Dec 2021 00:32:25 +0530 Subject: diceware: add WordListParser and tests --- .../aps/passgen/diceware/WordListParser.kt | 21 +++++++++++++++++++++ .../aps/passgen/diceware/WordListParserTest.kt | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParser.kt create mode 100644 passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParserTest.kt (limited to 'passgen') diff --git a/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParser.kt b/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParser.kt new file mode 100644 index 00000000..5351aaa1 --- /dev/null +++ b/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParser.kt @@ -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" +} diff --git a/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParserTest.kt b/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParserTest.kt new file mode 100644 index 00000000..e60db38c --- /dev/null +++ b/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParserTest.kt @@ -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]) + } +} -- cgit v1.2.3