private int findWordIndex(final CharSequence buffer) throws WordNotFoundException { final WordAndIndex key = new WordAndIndex(-1, buffer); final int index = Arrays.binarySearch(words, key, wordListSortOrder); if (index < 0) { final int insertionPoint = -index - 1; int suggestion = insertionPoint == 0 ? insertionPoint : insertionPoint - 1; if (suggestion + 1 == words.length) suggestion--; throw new WordNotFoundException(buffer, words[suggestion].word, words[suggestion + 1].word); } return words[index].index; }
private int[] findWordIndexes(final Collection<? extends CharSequence> split) throws UnexpectedWhiteSpaceException, WordNotFoundException { final int ms = split.size(); final int[] result = new int[ms]; int i = 0; for (final CharSequence buffer : split) { if (buffer.length() == 0) { throw new UnexpectedWhiteSpaceException(); } result[i++] = findWordIndex(buffer); } return result; }
private static void validate(final int[] wordIndexes) throws InvalidWordCountException, InvalidChecksumException { final int ms = wordIndexes.length; final int entPlusCs = ms * 11; final int ent = (entPlusCs * 32) / 33; final int cs = ent / 32; if (entPlusCs != ent + cs) throw new InvalidWordCountException(); final byte[] entropyWithChecksum = new byte[(entPlusCs + 7) / 8]; wordIndexesToEntropyWithCheckSum(wordIndexes, entropyWithChecksum); Arrays.fill(wordIndexes, 0); final byte[] entropy = Arrays.copyOf(entropyWithChecksum, entropyWithChecksum.length - 1); final byte lastByte = entropyWithChecksum[entropyWithChecksum.length - 1]; Arrays.fill(entropyWithChecksum, (byte) 0); final byte sha = firstByteOfSha256(entropy); final byte mask = maskOfFirstNBits(cs); if (((sha ^ lastByte) & mask) != 0) throw new InvalidChecksumException(); }
@Test(expected = WordNotFoundException.class) public void bad_english_word_alphabetically_before_all_others() throws Exception { try { validateExpectingBadWord( "aardvark abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon alan", English.INSTANCE); } catch (WordNotFoundException e) { assertEquals("Word not found in word list \"aardvark\", suggestions \"abandon\", \"ability\"", e.getMessage()); assertEquals("aardvark", e.getWord()); assertEquals("abandon", e.getSuggestion1()); assertEquals("ability", e.getSuggestion2()); throw e; } }
@Test public void InvalidWordCountException_message() throws Exception { assertEquals("Not a correct number of words", new InvalidWordCountException().getMessage()); }
@Test public void InvalidChecksumException_message() throws Exception { assertEquals("Invalid checksum", new InvalidChecksumException().getMessage()); }
@Test public void UnexpectedWhiteSpaceException_message() throws Exception { assertEquals("Unexpected whitespace", new UnexpectedWhiteSpaceException().getMessage()); }
@Test(expected = WordNotFoundException.class) public void bad_english_word() throws Exception { try { validateExpectingBadWord( "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon alan", English.INSTANCE); } catch (WordNotFoundException e) { assertEquals("Word not found in word list \"alan\", suggestions \"aisle\", \"alarm\"", e.getMessage()); assertEquals("alan", e.getWord()); assertEquals("aisle", e.getSuggestion1()); assertEquals("alarm", e.getSuggestion2()); throw e; } }
private static void validate(final int[] wordIndexes) throws InvalidWordCountException, InvalidChecksumException { final int ms = wordIndexes.length; final int entPlusCs = ms * 11; final int ent = (entPlusCs * 32) / 33; final int cs = ent / 32; if (entPlusCs != ent + cs) throw new InvalidWordCountException(); final byte[] entropyWithChecksum = new byte[(entPlusCs + 7) / 8]; wordIndexesToEntropyWithCheckSum(wordIndexes, entropyWithChecksum); Arrays.fill(wordIndexes, 0); final byte[] entropy = Arrays.copyOf(entropyWithChecksum, entropyWithChecksum.length - 1); final byte lastByte = entropyWithChecksum[entropyWithChecksum.length - 1]; Arrays.fill(entropyWithChecksum, (byte) 0); final byte sha = firstByteOfSha256(entropy); final byte mask = maskOfFirstNBits(cs); if (((sha ^ lastByte) & mask) != 0) throw new InvalidChecksumException(); }
private int[] findWordIndexes(final Collection<? extends CharSequence> split) throws UnexpectedWhiteSpaceException, WordNotFoundException { final int ms = split.size(); final int[] result = new int[ms]; int i = 0; for (final CharSequence buffer : split) { if (buffer.length() == 0) { throw new UnexpectedWhiteSpaceException(); } result[i++] = findWordIndex(buffer); } return result; }
private int findWordIndex(final CharSequence buffer) throws WordNotFoundException { final WordAndIndex key = new WordAndIndex(-1, buffer); final int index = Arrays.binarySearch(words, key, wordListSortOrder); if (index < 0) { final int insertionPoint = -index - 1; int suggestion = insertionPoint == 0 ? insertionPoint : insertionPoint - 1; if (suggestion + 1 == words.length) suggestion--; throw new WordNotFoundException(buffer, words[suggestion].word, words[suggestion + 1].word); } return words[index].index; }
@Test(expected = WordNotFoundException.class) public void bad_english_word_alphabetically_after_all_others() throws Exception { try { validateExpectingBadWord( "zymurgy abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon alan", English.INSTANCE); } catch (WordNotFoundException e) { assertEquals("Word not found in word list \"zymurgy\", suggestions \"zone\", \"zoo\"", e.getMessage()); assertEquals("zymurgy", e.getWord()); assertEquals("zone", e.getSuggestion1()); assertEquals("zoo", e.getSuggestion2()); throw e; } }
@Test(expected = WordNotFoundException.class) public void word_too_short() throws Exception { try { validateExpectingBadWord( "aero abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon alan", English.INSTANCE); } catch (WordNotFoundException e) { assertEquals("Word not found in word list \"aero\", suggestions \"advice\", \"aerobic\"", e.getMessage()); assertEquals("aero", e.getWord()); assertEquals("advice", e.getSuggestion1()); assertEquals("aerobic", e.getSuggestion2()); throw e; } }
@Test(expected = WordNotFoundException.class) public void bad_japanese_word() throws Exception { try { validateExpectingBadWord( "そつう れきだ ほんやく わかす りくつ ばいか ろせん やちん そつう れきだい ほんやく わかめ", Japanese.INSTANCE); } catch (WordNotFoundException e) { assertEquals("Word not found in word list \"れきだ\", suggestions \"れきし\", \"れきだい\"", e.getMessage()); assertEquals("れきだ", e.getWord()); assertEquals("れきし", e.getSuggestion1()); assertEquals("れきだい", e.getSuggestion2()); throw e; } }
@Test(expected = WordNotFoundException.class) public void bad_japanese_word_normalized_behaviour() throws Exception { try { validateExpectingBadWord( "そつう れきだ ほんやく わかす りくつ ばいか ろせん やちん そつう れきだい ほんやく わかめ", Japanese.INSTANCE); } catch (WordNotFoundException e) { assertEquals("Word not found in word list \"れきだ\", suggestions \"れきし\", \"れきだい\"", e.getMessage()); assertEquals("れきだ", e.getWord()); assertEquals("れきし", e.getSuggestion1()); assertEquals("れきだい", e.getSuggestion2()); throw e; } }