|
| 1 | +package ocr |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/cosmos/go-bip39" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestNewBIP39Mnemonic(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + entropySize int |
| 19 | + expectError bool |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "valid entropy 128 bits", |
| 23 | + entropySize: 128, |
| 24 | + expectError: false, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "valid entropy 160 bits", |
| 28 | + entropySize: 160, |
| 29 | + expectError: false, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "valid entropy 192 bits", |
| 33 | + entropySize: 192, |
| 34 | + expectError: false, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "valid entropy 224 bits", |
| 38 | + entropySize: 224, |
| 39 | + expectError: false, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "valid entropy 256 bits", |
| 43 | + entropySize: 256, |
| 44 | + expectError: false, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "invalid entropy size - too small", |
| 48 | + entropySize: 127, |
| 49 | + expectError: true, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "invalid entropy size - too large", |
| 53 | + entropySize: 257, |
| 54 | + expectError: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "invalid entropy size - not multiple of 32", |
| 58 | + entropySize: 129, |
| 59 | + expectError: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "zero entropy size", |
| 63 | + entropySize: 0, |
| 64 | + expectError: true, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "negative entropy size", |
| 68 | + entropySize: -128, |
| 69 | + expectError: true, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + for _, tt := range tests { |
| 74 | + t.Run(tt.name, func(t *testing.T) { |
| 75 | + t.Parallel() |
| 76 | + |
| 77 | + mnemonic, err := NewBIP39Mnemonic(tt.entropySize) |
| 78 | + |
| 79 | + if tt.expectError { |
| 80 | + require.Error(t, err) |
| 81 | + assert.Empty(t, mnemonic) |
| 82 | + } else { |
| 83 | + require.NoError(t, err) |
| 84 | + assert.NotEmpty(t, mnemonic) |
| 85 | + |
| 86 | + // Validate that the returned mnemonic is a valid BIP39 mnemonic |
| 87 | + assert.True(t, bip39.IsMnemonicValid(mnemonic), "generated mnemonic should be valid") |
| 88 | + |
| 89 | + // Check word count based on entropy size |
| 90 | + words := strings.Fields(mnemonic) |
| 91 | + expectedWordCount := (tt.entropySize + tt.entropySize/32) / 11 |
| 92 | + assert.Len(t, words, expectedWordCount, "word count should match expected for entropy size") |
| 93 | + |
| 94 | + // Verify mnemonic can be converted back to bytes |
| 95 | + _, err := bip39.MnemonicToByteArray(mnemonic) |
| 96 | + require.NoError(t, err, "should be able to convert mnemonic to byte array") |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestNewBIP39Mnemonic_Uniqueness(t *testing.T) { |
| 103 | + t.Parallel() |
| 104 | + |
| 105 | + const entropySize = 256 |
| 106 | + const numMnemonics = 100 |
| 107 | + |
| 108 | + mnemonics := make(map[string]bool) |
| 109 | + |
| 110 | + for range numMnemonics { |
| 111 | + mnemonic, err := NewBIP39Mnemonic(entropySize) |
| 112 | + require.NoError(t, err) |
| 113 | + require.NotEmpty(t, mnemonic) |
| 114 | + |
| 115 | + // Ensure each generated mnemonic is unique |
| 116 | + assert.False(t, mnemonics[mnemonic], "generated mnemonic should be unique") |
| 117 | + mnemonics[mnemonic] = true |
| 118 | + |
| 119 | + // Validate the mnemonic |
| 120 | + assert.True(t, bip39.IsMnemonicValid(mnemonic)) |
| 121 | + } |
| 122 | + |
| 123 | + assert.Len(t, mnemonics, numMnemonics, "should have generated exactly %d unique mnemonics", numMnemonics) |
| 124 | +} |
| 125 | + |
| 126 | +func TestNewBIP39Mnemonic_WordCount(t *testing.T) { |
| 127 | + t.Parallel() |
| 128 | + |
| 129 | + tests := []struct { |
| 130 | + entropySize int |
| 131 | + expectedWordCount int |
| 132 | + }{ |
| 133 | + {128, 12}, // 128 bits -> 12 words |
| 134 | + {160, 15}, // 160 bits -> 15 words |
| 135 | + {192, 18}, // 192 bits -> 18 words |
| 136 | + {224, 21}, // 224 bits -> 21 words |
| 137 | + {256, 24}, // 256 bits -> 24 words |
| 138 | + } |
| 139 | + |
| 140 | + for _, tt := range tests { |
| 141 | + t.Run(fmt.Sprintf("entropy_%d_bits", tt.entropySize), func(t *testing.T) { |
| 142 | + t.Parallel() |
| 143 | + |
| 144 | + mnemonic, err := NewBIP39Mnemonic(tt.entropySize) |
| 145 | + require.NoError(t, err) |
| 146 | + |
| 147 | + words := strings.Fields(mnemonic) |
| 148 | + assert.Len(t, words, tt.expectedWordCount, |
| 149 | + "entropy size %d should generate %d words", tt.entropySize, tt.expectedWordCount) |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
0 commit comments