aboutsummaryrefslogtreecommitdiff
path: root/tests.js
diff options
context:
space:
mode:
authorIan Coleman <coleman.ian@gmail.com>2016-11-07 16:01:21 +1100
committerIan Coleman <coleman.ian@gmail.com>2016-11-07 16:01:21 +1100
commitadc8ce127d4f8ea0d7e5ede6a82c2791d60ff4d2 (patch)
treeda8a4bfe57f207d52822cab17ae7a8af150271bf /tests.js
parent6606c50fd3af59483a5524170d9d2c3ec213a60f (diff)
downloadBIP39-adc8ce127d4f8ea0d7e5ede6a82c2791d60ff4d2.tar.gz
BIP39-adc8ce127d4f8ea0d7e5ede6a82c2791d60ff4d2.tar.zst
BIP39-adc8ce127d4f8ea0d7e5ede6a82c2791d60ff4d2.zip
Cards can be used for entropy
Format is [A2-9TJQK][CDHS]
Diffstat (limited to 'tests.js')
-rw-r--r--tests.js231
1 files changed, 179 insertions, 52 deletions
diff --git a/tests.js b/tests.js
index 1841af6..8de6391 100644
--- a/tests.js
+++ b/tests.js
@@ -1997,85 +1997,212 @@ page.open(url, function(status) {
1997// Entropy unit tests 1997// Entropy unit tests
1998function() { 1998function() {
1999page.open(url, function(status) { 1999page.open(url, function(status) {
2000 var error = page.evaluate(function() { 2000 var response = page.evaluate(function() {
2001 var e; 2001 var e;
2002 // binary entropy is detected 2002 // binary entropy is detected
2003 e = Entropy.fromString("01010101"); 2003 try {
2004 if (e.base.str != "binary") { 2004 e = Entropy.fromString("01010101");
2005 return "Binary entropy not detected correctly"; 2005 if (e.base.str != "binary") {
2006 return "Binary entropy not detected correctly";
2007 }
2008 }
2009 catch (e) {
2010 return e.message;
2006 } 2011 }
2007 // base6 entropy is detected 2012 // base6 entropy is detected
2008 e = Entropy.fromString("012345012345"); 2013 try {
2009 if (e.base.str != "base 6") { 2014 e = Entropy.fromString("012345012345");
2010 return "base6 entropy not detected correctly"; 2015 if (e.base.str != "base 6") {
2016 return "base6 entropy not detected correctly";
2017 }
2018 }
2019 catch (e) {
2020 return e.message;
2011 } 2021 }
2012 // dice entropy is detected 2022 // dice entropy is detected
2013 e = Entropy.fromString("123456123456"); 2023 try {
2014 if (e.base.str != "base 6 (dice)") { 2024 e = Entropy.fromString("123456123456");
2015 return "dice entropy not detected correctly"; 2025 if (e.base.str != "base 6 (dice)") {
2026 return "dice entropy not detected correctly";
2027 }
2028 }
2029 catch (e) {
2030 return e.message;
2016 } 2031 }
2017 // base10 entropy is detected 2032 // base10 entropy is detected
2018 e = Entropy.fromString("0123456789"); 2033 try {
2019 if (e.base.str != "base 10") { 2034 e = Entropy.fromString("0123456789");
2020 return "base10 entropy not detected correctly"; 2035 if (e.base.str != "base 10") {
2036 return "base10 entropy not detected correctly";
2037 }
2038 }
2039 catch (e) {
2040 return e.message;
2021 } 2041 }
2022 // hex entropy is detected 2042 // hex entropy is detected
2023 e = Entropy.fromString("0123456789ABCDEF"); 2043 try {
2024 if (e.base.str != "hexadecimal") { 2044 e = Entropy.fromString("0123456789ABCDEF");
2025 return "hexadecimal entropy not detected correctly"; 2045 if (e.base.str != "hexadecimal") {
2046 return "hexadecimal entropy not detected correctly";
2047 }
2048 }
2049 catch (e) {
2050 return e.message;
2051 }
2052 // card entropy is detected
2053 try {
2054 e = Entropy.fromString("AC4DTHKS");
2055 if (e.base.str != "card") {
2056 return "card entropy not detected correctly";
2057 }
2058 }
2059 catch (e) {
2060 return e.message;
2026 } 2061 }
2027 // entropy is case insensitive 2062 // entropy is case insensitive
2028 e = Entropy.fromString("aBcDeF"); 2063 try {
2029 if (e.cleanStr != "aBcDeF") { 2064 e = Entropy.fromString("aBcDeF");
2030 return "Entropy should not be case sensitive"; 2065 if (e.cleanStr != "aBcDeF") {
2066 return "Entropy should not be case sensitive";
2067 }
2068 }
2069 catch (e) {
2070 return e.message;
2031 } 2071 }
2032 // dice entropy is converted to base6 2072 // dice entropy is converted to base6
2033 e = Entropy.fromString("123456"); 2073 try {
2034 if (e.cleanStr != "012345") { 2074 e = Entropy.fromString("123456");
2035 return "Dice entropy is not automatically converted to base6"; 2075 if (e.cleanStr != "012345") {
2076 return "Dice entropy is not automatically converted to base6";
2077 }
2078 }
2079 catch (e) {
2080 return e.message;
2036 } 2081 }
2037 // dice entropy is preferred to base6 if ambiguous 2082 // dice entropy is preferred to base6 if ambiguous
2038 e = Entropy.fromString("12345"); 2083 try {
2039 if (e.base.str != "base 6 (dice)") { 2084 e = Entropy.fromString("12345");
2040 return "dice not used as default over base 6"; 2085 if (e.base.str != "base 6 (dice)") {
2086 return "dice not used as default over base 6";
2087 }
2088 }
2089 catch (e) {
2090 return e.message;
2041 } 2091 }
2042 // unused characters are ignored 2092 // unused characters are ignored
2043 e = Entropy.fromString("fghijkl"); 2093 try {
2044 if (e.cleanStr != "f") { 2094 e = Entropy.fromString("fghijkl");
2045 return "additional characters are not ignored"; 2095 if (e.cleanStr != "f") {
2096 return "additional characters are not ignored";
2097 }
2098 }
2099 catch (e) {
2100 return e.message;
2046 } 2101 }
2047 // the lowest base is used by default 2102 // the lowest base is used by default
2048 // 7 could be decimal or hexadecimal, but should be detected as decimal 2103 // 7 could be decimal or hexadecimal, but should be detected as decimal
2049 e = Entropy.fromString("7"); 2104 try {
2050 if (e.base.str != "base 10") { 2105 e = Entropy.fromString("7");
2051 return "lowest base is not used"; 2106 if (e.base.str != "base 10") {
2107 return "lowest base is not used";
2108 }
2052 } 2109 }
2053 // Hexadecimal representation is returned 2110 catch (e) {
2054 e = Entropy.fromString("1010"); 2111 return e.message;
2055 if (e.hexStr != "A") {
2056 return "Hexadecimal representation not returned";
2057 } 2112 }
2058 // Leading zeros are retained 2113 // Leading zeros are retained
2059 e = Entropy.fromString("000A"); 2114 try {
2060 if (e.cleanStr != "000A") { 2115 e = Entropy.fromString("000A");
2061 return "Leading zeros are not retained"; 2116 if (e.cleanStr != "000A") {
2117 return "Leading zeros are not retained";
2118 }
2119 }
2120 catch (e) {
2121 return e.message;
2062 } 2122 }
2063 // Leading zeros are correctly preserved for hex in binary string 2123 // Leading zeros are correctly preserved for hex in binary string
2064 e = Entropy.fromString("2A"); 2124 try {
2065 if (e.binaryStr != "00101010") { 2125 e = Entropy.fromString("2A");
2066 return "Hex leading zeros are not correct in binary"; 2126 if (e.binaryStr != "00101010") {
2127 return "Hex leading zeros are not correct in binary";
2128 }
2129 }
2130 catch (e) {
2131 return e.message;
2132 }
2133 // Leading zeros are correctly preserved for base 6 in binary string
2134 try {
2135 e = Entropy.fromString("2");
2136 if (e.binaryStr != "010") {
2137 return "Base 6 leading zeros are not correct in binary";
2138 }
2139 }
2140 catch (e) {
2141 return e.message;
2067 } 2142 }
2068 // Keyboard mashing results in weak entropy 2143 // Keyboard mashing results in weak entropy
2069 // Despite being a long string, it's less than 30 bits of entropy 2144 // Despite being a long string, it's less than 30 bits of entropy
2070 e = Entropy.fromString("aj;se ifj; ask,dfv js;ifj"); 2145 try {
2071 if (e.binaryStr.length >= 30) { 2146 e = Entropy.fromString("aj;se ifj; ask,dfv js;ifj");
2072 return "Keyboard mashing should produce weak entropy"; 2147 if (e.binaryStr.length >= 30) {
2148 return "Keyboard mashing should produce weak entropy";
2149 }
2073 } 2150 }
2074 return false; 2151 catch (e) {
2152 return e.message;
2153 }
2154 // Card entropy is used if every pair could be a card
2155 try {
2156 e = Entropy.fromString("4c3c2c");
2157 if (e.base.str != "card") {
2158 return "Card entropy not used if all pairs are cards";
2159 }
2160 }
2161 catch (e) {
2162 return e.message;
2163 }
2164 // Card entropy uses base 52
2165 // [ cards, binary ]
2166 try {
2167 var cards = [
2168 [ "ac", "00000" ],
2169 [ "acac", "00000000000" ],
2170 [ "acac2c", "00000000000000001" ],
2171 [ "acks", "00000110011" ],
2172 [ "acacks", "00000000000110011" ],
2173 [ "2c", "000001" ],
2174 [ "3d", "001111" ],
2175 [ "4h", "011101" ],
2176 [ "5s", "101011" ],
2177 [ "6c", "000101" ],
2178 [ "7d", "010011" ],
2179 [ "8h", "100001" ],
2180 [ "9s", "101111" ],
2181 [ "tc", "001001" ],
2182 [ "jd", "010111" ],
2183 [ "qh", "100101" ],
2184 [ "ks", "110011" ],
2185 [ "ks2c", "101001011101" ],
2186 [ "KS2C", "101001011101" ],
2187 ];
2188 for (var i=0; i<cards.length; i++) {
2189 var card = cards[i][0];
2190 var result = cards[i][1];
2191 e = Entropy.fromString(card);
2192 console.log(e.binary + " " + result);
2193 if (e.binaryStr !== result) {
2194 return "card entropy not parsed correctly: " + result + " != " + e.binaryStr;
2195 }
2196 }
2197 }
2198 catch (e) {
2199 return e.message;
2200 }
2201 return "PASS";
2075 }); 2202 });
2076 if (error) { 2203 if (response != "PASS") {
2077 console.log("Entropy unit tests"); 2204 console.log("Entropy unit tests");
2078 console.log(error); 2205 console.log(response);
2079 fail(); 2206 fail();
2080 }; 2207 };
2081 next(); 2208 next();
@@ -2339,10 +2466,10 @@ page.open(url, function(status) {
2339 [ "0", "1" ], 2466 [ "0", "1" ],
2340 [ "0000", "4" ], 2467 [ "0000", "4" ],
2341 [ "6", "3" ], 2468 [ "6", "3" ],
2342 [ "7", "3" ], 2469 [ "7", "4" ],
2343 [ "8", "4" ], 2470 [ "8", "4" ],
2344 [ "F", "4" ], 2471 [ "F", "4" ],
2345 [ "29", "5" ], 2472 [ "29", "7" ],
2346 [ "0A", "8" ], 2473 [ "0A", "8" ],
2347 [ "1A", "8" ], // hex is always multiple of 4 bits of entropy 2474 [ "1A", "8" ], // hex is always multiple of 4 bits of entropy
2348 [ "2A", "8" ], 2475 [ "2A", "8" ],
@@ -2350,9 +2477,9 @@ page.open(url, function(status) {
2350 [ "8A", "8" ], 2477 [ "8A", "8" ],
2351 [ "FA", "8" ], 2478 [ "FA", "8" ],
2352 [ "000A", "16" ], 2479 [ "000A", "16" ],
2353 [ "2220", "10" ], 2480 [ "2220", "11" ],
2354 [ "2221", "9" ], // uses dice, so entropy is actually 1110 2481 [ "2221", "11" ], // uses dice, so entropy is actually 1110
2355 [ "2227", "12" ], 2482 [ "2227", "14" ],
2356 [ "222F", "16" ], 2483 [ "222F", "16" ],
2357 [ "FFFF", "16" ], 2484 [ "FFFF", "16" ],
2358 ] 2485 ]