|
| 1 | +package org.tron.keystore; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNotEquals; |
| 6 | +import static org.junit.Assert.assertNotNull; |
| 7 | +import static org.junit.Assert.assertTrue; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +public class WalletFilePojoTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + public void testWalletFileGettersSetters() { |
| 16 | + WalletFile wf = new WalletFile(); |
| 17 | + wf.setAddress("TAddr"); |
| 18 | + wf.setId("uuid-123"); |
| 19 | + wf.setVersion(3); |
| 20 | + WalletFile.Crypto c = new WalletFile.Crypto(); |
| 21 | + wf.setCrypto(c); |
| 22 | + |
| 23 | + assertEquals("TAddr", wf.getAddress()); |
| 24 | + assertEquals("uuid-123", wf.getId()); |
| 25 | + assertEquals(3, wf.getVersion()); |
| 26 | + assertEquals(c, wf.getCrypto()); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testWalletFileCryptoV1Setter() { |
| 31 | + WalletFile wf = new WalletFile(); |
| 32 | + WalletFile.Crypto c = new WalletFile.Crypto(); |
| 33 | + wf.setCryptoV1(c); |
| 34 | + assertEquals(c, wf.getCrypto()); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testWalletFileEqualsAllBranches() { |
| 39 | + WalletFile a = new WalletFile(); |
| 40 | + a.setAddress("TAddr"); |
| 41 | + a.setId("id1"); |
| 42 | + a.setVersion(3); |
| 43 | + WalletFile.Crypto c = new WalletFile.Crypto(); |
| 44 | + a.setCrypto(c); |
| 45 | + |
| 46 | + WalletFile b = new WalletFile(); |
| 47 | + b.setAddress("TAddr"); |
| 48 | + b.setId("id1"); |
| 49 | + b.setVersion(3); |
| 50 | + b.setCrypto(c); |
| 51 | + |
| 52 | + assertEquals(a, b); |
| 53 | + assertEquals(a.hashCode(), b.hashCode()); |
| 54 | + assertTrue(a.equals(a)); |
| 55 | + assertFalse(a.equals(null)); |
| 56 | + assertFalse(a.equals("string")); |
| 57 | + |
| 58 | + // Different address |
| 59 | + b.setAddress("TOther"); |
| 60 | + assertNotEquals(a, b); |
| 61 | + b.setAddress("TAddr"); |
| 62 | + |
| 63 | + // Different id |
| 64 | + b.setId("id2"); |
| 65 | + assertNotEquals(a, b); |
| 66 | + b.setId("id1"); |
| 67 | + |
| 68 | + // Different version |
| 69 | + b.setVersion(4); |
| 70 | + assertNotEquals(a, b); |
| 71 | + b.setVersion(3); |
| 72 | + |
| 73 | + // Different crypto |
| 74 | + b.setCrypto(new WalletFile.Crypto()); |
| 75 | + // Still equal since Cryptos are equal (both empty) |
| 76 | + assertEquals(a, b); |
| 77 | + |
| 78 | + // Null fields |
| 79 | + WalletFile empty = new WalletFile(); |
| 80 | + WalletFile empty2 = new WalletFile(); |
| 81 | + assertEquals(empty, empty2); |
| 82 | + assertEquals(empty.hashCode(), empty2.hashCode()); |
| 83 | + |
| 84 | + // One side null |
| 85 | + empty2.setAddress("X"); |
| 86 | + assertNotEquals(empty, empty2); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testCryptoGettersSetters() { |
| 91 | + WalletFile.Crypto c = new WalletFile.Crypto(); |
| 92 | + c.setCipher("aes-128-ctr"); |
| 93 | + c.setCiphertext("ciphertext"); |
| 94 | + c.setKdf("scrypt"); |
| 95 | + c.setMac("mac-value"); |
| 96 | + |
| 97 | + WalletFile.CipherParams cp = new WalletFile.CipherParams(); |
| 98 | + cp.setIv("ivvalue"); |
| 99 | + c.setCipherparams(cp); |
| 100 | + |
| 101 | + WalletFile.ScryptKdfParams kp = new WalletFile.ScryptKdfParams(); |
| 102 | + c.setKdfparams(kp); |
| 103 | + |
| 104 | + assertEquals("aes-128-ctr", c.getCipher()); |
| 105 | + assertEquals("ciphertext", c.getCiphertext()); |
| 106 | + assertEquals("scrypt", c.getKdf()); |
| 107 | + assertEquals("mac-value", c.getMac()); |
| 108 | + assertEquals(cp, c.getCipherparams()); |
| 109 | + assertEquals(kp, c.getKdfparams()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testCryptoEqualsAllBranches() { |
| 114 | + WalletFile.Crypto a = new WalletFile.Crypto(); |
| 115 | + a.setCipher("c1"); |
| 116 | + a.setCiphertext("txt"); |
| 117 | + a.setKdf("kdf"); |
| 118 | + a.setMac("mac"); |
| 119 | + WalletFile.CipherParams cp = new WalletFile.CipherParams(); |
| 120 | + cp.setIv("iv"); |
| 121 | + a.setCipherparams(cp); |
| 122 | + WalletFile.Aes128CtrKdfParams kp = new WalletFile.Aes128CtrKdfParams(); |
| 123 | + a.setKdfparams(kp); |
| 124 | + |
| 125 | + WalletFile.Crypto b = new WalletFile.Crypto(); |
| 126 | + b.setCipher("c1"); |
| 127 | + b.setCiphertext("txt"); |
| 128 | + b.setKdf("kdf"); |
| 129 | + b.setMac("mac"); |
| 130 | + b.setCipherparams(cp); |
| 131 | + b.setKdfparams(kp); |
| 132 | + |
| 133 | + assertEquals(a, b); |
| 134 | + assertEquals(a.hashCode(), b.hashCode()); |
| 135 | + assertTrue(a.equals(a)); |
| 136 | + assertFalse(a.equals(null)); |
| 137 | + assertFalse(a.equals("string")); |
| 138 | + |
| 139 | + // cipher differs |
| 140 | + b.setCipher("c2"); |
| 141 | + assertNotEquals(a, b); |
| 142 | + b.setCipher("c1"); |
| 143 | + |
| 144 | + // ciphertext differs |
| 145 | + b.setCiphertext("other"); |
| 146 | + assertNotEquals(a, b); |
| 147 | + b.setCiphertext("txt"); |
| 148 | + |
| 149 | + // kdf differs |
| 150 | + b.setKdf("other"); |
| 151 | + assertNotEquals(a, b); |
| 152 | + b.setKdf("kdf"); |
| 153 | + |
| 154 | + // mac differs |
| 155 | + b.setMac("other"); |
| 156 | + assertNotEquals(a, b); |
| 157 | + b.setMac("mac"); |
| 158 | + |
| 159 | + // cipherparams differs |
| 160 | + WalletFile.CipherParams cp2 = new WalletFile.CipherParams(); |
| 161 | + cp2.setIv("other"); |
| 162 | + b.setCipherparams(cp2); |
| 163 | + assertNotEquals(a, b); |
| 164 | + b.setCipherparams(cp); |
| 165 | + |
| 166 | + // kdfparams differs |
| 167 | + WalletFile.Aes128CtrKdfParams kp2 = new WalletFile.Aes128CtrKdfParams(); |
| 168 | + kp2.setC(5); |
| 169 | + b.setKdfparams(kp2); |
| 170 | + assertNotEquals(a, b); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testCryptoNullFields() { |
| 175 | + WalletFile.Crypto a = new WalletFile.Crypto(); |
| 176 | + WalletFile.Crypto b = new WalletFile.Crypto(); |
| 177 | + assertEquals(a, b); |
| 178 | + assertEquals(a.hashCode(), b.hashCode()); |
| 179 | + |
| 180 | + a.setCipher("x"); |
| 181 | + assertNotEquals(a, b); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void testCipherParamsGettersSetters() { |
| 186 | + WalletFile.CipherParams cp = new WalletFile.CipherParams(); |
| 187 | + cp.setIv("ivvalue"); |
| 188 | + assertEquals("ivvalue", cp.getIv()); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void testCipherParamsEquals() { |
| 193 | + WalletFile.CipherParams a = new WalletFile.CipherParams(); |
| 194 | + WalletFile.CipherParams b = new WalletFile.CipherParams(); |
| 195 | + assertEquals(a, b); |
| 196 | + a.setIv("iv"); |
| 197 | + assertNotEquals(a, b); |
| 198 | + b.setIv("iv"); |
| 199 | + assertEquals(a, b); |
| 200 | + assertEquals(a.hashCode(), b.hashCode()); |
| 201 | + b.setIv("other"); |
| 202 | + assertNotEquals(a, b); |
| 203 | + assertTrue(a.equals(a)); |
| 204 | + assertFalse(a.equals(null)); |
| 205 | + assertFalse(a.equals("string")); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + public void testAes128CtrKdfParamsAllAccessors() { |
| 210 | + WalletFile.Aes128CtrKdfParams p = new WalletFile.Aes128CtrKdfParams(); |
| 211 | + p.setDklen(32); |
| 212 | + p.setC(262144); |
| 213 | + p.setPrf("hmac-sha256"); |
| 214 | + p.setSalt("saltvalue"); |
| 215 | + |
| 216 | + assertEquals(32, p.getDklen()); |
| 217 | + assertEquals(262144, p.getC()); |
| 218 | + assertEquals("hmac-sha256", p.getPrf()); |
| 219 | + assertEquals("saltvalue", p.getSalt()); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + public void testAes128CtrKdfParamsEquals() { |
| 224 | + WalletFile.Aes128CtrKdfParams a = new WalletFile.Aes128CtrKdfParams(); |
| 225 | + a.setDklen(32); |
| 226 | + a.setC(262144); |
| 227 | + a.setPrf("hmac-sha256"); |
| 228 | + a.setSalt("salt"); |
| 229 | + |
| 230 | + WalletFile.Aes128CtrKdfParams b = new WalletFile.Aes128CtrKdfParams(); |
| 231 | + b.setDklen(32); |
| 232 | + b.setC(262144); |
| 233 | + b.setPrf("hmac-sha256"); |
| 234 | + b.setSalt("salt"); |
| 235 | + |
| 236 | + assertEquals(a, b); |
| 237 | + assertEquals(a.hashCode(), b.hashCode()); |
| 238 | + assertTrue(a.equals(a)); |
| 239 | + assertFalse(a.equals(null)); |
| 240 | + assertFalse(a.equals("string")); |
| 241 | + |
| 242 | + b.setDklen(64); |
| 243 | + assertNotEquals(a, b); |
| 244 | + b.setDklen(32); |
| 245 | + |
| 246 | + b.setC(1); |
| 247 | + assertNotEquals(a, b); |
| 248 | + b.setC(262144); |
| 249 | + |
| 250 | + b.setPrf("other"); |
| 251 | + assertNotEquals(a, b); |
| 252 | + b.setPrf("hmac-sha256"); |
| 253 | + |
| 254 | + b.setSalt("other"); |
| 255 | + assertNotEquals(a, b); |
| 256 | + b.setSalt("salt"); |
| 257 | + |
| 258 | + // null fields |
| 259 | + WalletFile.Aes128CtrKdfParams x = new WalletFile.Aes128CtrKdfParams(); |
| 260 | + WalletFile.Aes128CtrKdfParams y = new WalletFile.Aes128CtrKdfParams(); |
| 261 | + assertEquals(x, y); |
| 262 | + x.setPrf("x"); |
| 263 | + assertNotEquals(x, y); |
| 264 | + } |
| 265 | + |
| 266 | + @Test |
| 267 | + public void testScryptKdfParamsAllAccessors() { |
| 268 | + WalletFile.ScryptKdfParams p = new WalletFile.ScryptKdfParams(); |
| 269 | + p.setDklen(32); |
| 270 | + p.setN(262144); |
| 271 | + p.setP(1); |
| 272 | + p.setR(8); |
| 273 | + p.setSalt("saltvalue"); |
| 274 | + |
| 275 | + assertEquals(32, p.getDklen()); |
| 276 | + assertEquals(262144, p.getN()); |
| 277 | + assertEquals(1, p.getP()); |
| 278 | + assertEquals(8, p.getR()); |
| 279 | + assertEquals("saltvalue", p.getSalt()); |
| 280 | + } |
| 281 | + |
| 282 | + @Test |
| 283 | + public void testScryptKdfParamsEquals() { |
| 284 | + WalletFile.ScryptKdfParams a = new WalletFile.ScryptKdfParams(); |
| 285 | + a.setDklen(32); |
| 286 | + a.setN(262144); |
| 287 | + a.setP(1); |
| 288 | + a.setR(8); |
| 289 | + a.setSalt("salt"); |
| 290 | + |
| 291 | + WalletFile.ScryptKdfParams b = new WalletFile.ScryptKdfParams(); |
| 292 | + b.setDklen(32); |
| 293 | + b.setN(262144); |
| 294 | + b.setP(1); |
| 295 | + b.setR(8); |
| 296 | + b.setSalt("salt"); |
| 297 | + |
| 298 | + assertEquals(a, b); |
| 299 | + assertEquals(a.hashCode(), b.hashCode()); |
| 300 | + assertTrue(a.equals(a)); |
| 301 | + assertFalse(a.equals(null)); |
| 302 | + assertFalse(a.equals("string")); |
| 303 | + |
| 304 | + b.setDklen(64); |
| 305 | + assertNotEquals(a, b); |
| 306 | + b.setDklen(32); |
| 307 | + |
| 308 | + b.setN(1); |
| 309 | + assertNotEquals(a, b); |
| 310 | + b.setN(262144); |
| 311 | + |
| 312 | + b.setP(2); |
| 313 | + assertNotEquals(a, b); |
| 314 | + b.setP(1); |
| 315 | + |
| 316 | + b.setR(16); |
| 317 | + assertNotEquals(a, b); |
| 318 | + b.setR(8); |
| 319 | + |
| 320 | + b.setSalt("other"); |
| 321 | + assertNotEquals(a, b); |
| 322 | + |
| 323 | + // null salt |
| 324 | + WalletFile.ScryptKdfParams x = new WalletFile.ScryptKdfParams(); |
| 325 | + WalletFile.ScryptKdfParams y = new WalletFile.ScryptKdfParams(); |
| 326 | + assertEquals(x, y); |
| 327 | + x.setSalt("x"); |
| 328 | + assertNotEquals(x, y); |
| 329 | + } |
| 330 | + |
| 331 | + @Test |
| 332 | + public void testJsonDeserializeWithScryptKdf() throws Exception { |
| 333 | + String json = "{" |
| 334 | + + "\"address\":\"TAddr\"," |
| 335 | + + "\"version\":3," |
| 336 | + + "\"id\":\"uuid\"," |
| 337 | + + "\"crypto\":{" |
| 338 | + + " \"cipher\":\"aes-128-ctr\"," |
| 339 | + + " \"ciphertext\":\"ct\"," |
| 340 | + + " \"cipherparams\":{\"iv\":\"iv\"}," |
| 341 | + + " \"kdf\":\"scrypt\"," |
| 342 | + + " \"kdfparams\":{\"dklen\":32,\"n\":262144,\"p\":1,\"r\":8,\"salt\":\"salt\"}," |
| 343 | + + " \"mac\":\"mac\"" |
| 344 | + + "}}"; |
| 345 | + |
| 346 | + WalletFile wf = new ObjectMapper().readValue(json, WalletFile.class); |
| 347 | + assertEquals("TAddr", wf.getAddress()); |
| 348 | + assertEquals(3, wf.getVersion()); |
| 349 | + assertNotNull(wf.getCrypto()); |
| 350 | + assertNotNull(wf.getCrypto().getKdfparams()); |
| 351 | + assertTrue(wf.getCrypto().getKdfparams() instanceof WalletFile.ScryptKdfParams); |
| 352 | + } |
| 353 | + |
| 354 | + @Test |
| 355 | + public void testJsonDeserializeWithAes128Kdf() throws Exception { |
| 356 | + String json = "{" |
| 357 | + + "\"address\":\"TAddr\"," |
| 358 | + + "\"version\":3," |
| 359 | + + "\"crypto\":{" |
| 360 | + + " \"cipher\":\"aes-128-ctr\"," |
| 361 | + + " \"ciphertext\":\"ct\"," |
| 362 | + + " \"cipherparams\":{\"iv\":\"iv\"}," |
| 363 | + + " \"kdf\":\"pbkdf2\"," |
| 364 | + + " \"kdfparams\":{\"dklen\":32,\"c\":262144,\"prf\":\"hmac-sha256\",\"salt\":\"salt\"}," |
| 365 | + + " \"mac\":\"mac\"" |
| 366 | + + "}}"; |
| 367 | + |
| 368 | + WalletFile wf = new ObjectMapper().readValue(json, WalletFile.class); |
| 369 | + assertNotNull(wf.getCrypto().getKdfparams()); |
| 370 | + assertTrue(wf.getCrypto().getKdfparams() instanceof WalletFile.Aes128CtrKdfParams); |
| 371 | + } |
| 372 | + |
| 373 | + @Test |
| 374 | + public void testJsonDeserializeCryptoV1Field() throws Exception { |
| 375 | + // Legacy files may use "Crypto" instead of "crypto" |
| 376 | + String json = "{" |
| 377 | + + "\"address\":\"TAddr\"," |
| 378 | + + "\"version\":3," |
| 379 | + + "\"Crypto\":{" |
| 380 | + + " \"cipher\":\"aes-128-ctr\"," |
| 381 | + + " \"kdf\":\"scrypt\"," |
| 382 | + + " \"kdfparams\":{\"dklen\":32,\"n\":1,\"p\":1,\"r\":8,\"salt\":\"s\"}" |
| 383 | + + "}}"; |
| 384 | + |
| 385 | + WalletFile wf = new ObjectMapper().readValue(json, WalletFile.class); |
| 386 | + assertNotNull(wf.getCrypto()); |
| 387 | + assertEquals("aes-128-ctr", wf.getCrypto().getCipher()); |
| 388 | + } |
| 389 | +} |
0 commit comments