decred.org/dcrwallet/v3@v3.1.0/pgpwordlist/pgpwordlist_test.go (about)

     1  /*
     2   * Copyright (c) 2015-2016 The decred developers
     3   *
     4   * Permission to use, copy, modify, and distribute this software for any
     5   * purpose with or without fee is hereby granted, provided that the above
     6   * copyright notice and this permission notice appear in all copies.
     7   *
     8   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     9   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    10   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    11   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    12   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    13   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    14   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    15   */
    16  
    17  package pgpwordlist
    18  
    19  import (
    20  	"bytes"
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  var tests = []struct {
    26  	mnemonics string
    27  	data      []byte
    28  }{
    29  	{
    30  		mnemonics: "topmost Istanbul Pluto vagabond treadmill Pacific brackish dictator goldfish Medusa afflict bravado chatter revolver Dupont midsummer stopwatch whimsical cowbell bottomless",
    31  		data: []byte{0xE5, 0x82, 0x94, 0xF2, 0xE9, 0xA2, 0x27, 0x48,
    32  			0x6E, 0x8B, 0x06, 0x1B, 0x31, 0xCC, 0x52, 0x8F, 0xD7,
    33  			0xFA, 0x3F, 0x19},
    34  	},
    35  	{
    36  		mnemonics: "stairway souvenir flytrap recipe adrift upcoming artist positive spearhead Pandora spaniel stupendous tonic concurrent transit Wichita lockup visitor flagpole escapade",
    37  		data: []byte{0xD1, 0xD4, 0x64, 0xC0, 0x04, 0xF0, 0x0F, 0xB5,
    38  			0xC9, 0xA4, 0xC8, 0xD8, 0xE4, 0x33, 0xE7, 0xFB, 0x7F,
    39  			0xF5, 0x62, 0x56},
    40  	},
    41  }
    42  
    43  func TestDecode(t *testing.T) {
    44  	for _, test := range tests {
    45  		data, err := DecodeMnemonics(strings.Split(test.mnemonics, " "))
    46  		if err != nil {
    47  			t.Error(err)
    48  			continue
    49  		}
    50  		if !bytes.Equal(data, test.data) {
    51  			t.Errorf("decoded data %x differs from expected %x", data, test.data)
    52  		}
    53  	}
    54  }
    55  
    56  func TestEncodeMnemonics(t *testing.T) {
    57  	for _, test := range tests {
    58  		mnemonicsSlice := strings.Split(test.mnemonics, " ")
    59  		for i, b := range test.data {
    60  			mnemonic := ByteToMnemonic(b, i)
    61  			if mnemonic != mnemonicsSlice[i] {
    62  				t.Errorf("returned mnemonic %s differs from expected %s", mnemonic, mnemonicsSlice[i])
    63  			}
    64  		}
    65  	}
    66  }