From 1b747bcbc7c521e208774ab255c206a65f693292 Mon Sep 17 00:00:00 2001 From: Andrea Franz Date: Wed, 14 Mar 2018 11:21:20 +0100 Subject: [PATCH] add test for Mnemonic.WordList func --- extkeys/mnemonic.go | 2 +- extkeys/mnemonic_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/extkeys/mnemonic.go b/extkeys/mnemonic.go index 0d59e6414..1509d14b0 100644 --- a/extkeys/mnemonic.go +++ b/extkeys/mnemonic.go @@ -240,7 +240,7 @@ func (m *Mnemonic) ValidMnemonic(mnemonic string, language Language) bool { // WordList returns list of words for a given language func (m *Mnemonic) WordList(language Language) (*WordList, error) { - if m.wordLists[language] == nil { + if int(language) < 0 || int(language) > len(m.wordLists)-1 || m.wordLists[language] == nil { return nil, fmt.Errorf("language word list is missing (language id: %d)", language) } return m.wordLists[language], nil diff --git a/extkeys/mnemonic_test.go b/extkeys/mnemonic_test.go index 4953ed0d8..fbcf4915d 100644 --- a/extkeys/mnemonic_test.go +++ b/extkeys/mnemonic_test.go @@ -29,6 +29,22 @@ func TestNewMnemonic(t *testing.T) { } } +func TestMnemonic_WordList(t *testing.T) { + m := NewMnemonic("") + _, err := m.WordList(EnglishLanguage) + if err != nil { + t.Errorf("expected WordList to return WordList without errors, got: %s", err) + } + + indexes := []Language{-1, Language(len(m.wordLists))} + for _, index := range indexes { + _, err := m.WordList(index) + if err == nil { + t.Errorf("expected WordList to return an error with index %d", index) + } + } +} + // TestMnemonicPhrase func TestMnemonicPhrase(t *testing.T) {