icap_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package common
  17. import "testing"
  18. /* More test vectors:
  19. https://github.com/ethereum/web3.js/blob/master/test/iban.fromAddress.js
  20. https://github.com/ethereum/web3.js/blob/master/test/iban.toAddress.js
  21. https://github.com/ethereum/web3.js/blob/master/test/iban.isValid.js
  22. https://github.com/ethereum/libethereum/blob/develop/test/libethcore/icap.cpp
  23. */
  24. type icapTest struct {
  25. name string
  26. addr string
  27. icap string
  28. }
  29. var icapOKTests = []icapTest{
  30. {"Direct1", "0x52dc504a422f0e2a9e7632a34a50f1a82f8224c7", "XE499OG1EH8ZZI0KXC6N83EKGT1BM97P2O7"},
  31. {"Direct2", "0x11c5496aee77c1ba1f0854206a26dda82a81d6d8", "XE1222Q908LN1QBBU6XUQSO1OHWJIOS46OO"},
  32. {"DirectZeroPrefix", "0x00c5496aee77c1ba1f0854206a26dda82a81d6d8", "XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS"},
  33. {"DirectDoubleZeroPrefix", "0x0000a5327eab78357cbf2ae8f3d49fd9d90c7d22", "XE0600DQK33XDTYUCRI0KYM5ELAKXDWWF6"},
  34. }
  35. var icapInvalidTests = []icapTest{
  36. {"DirectInvalidCheckSum", "", "XE7438O073KYGTWWZN0F2WZ0R8PX5ZPPZS"},
  37. {"DirectInvalidCountryCode", "", "XD7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS"},
  38. {"DirectInvalidLength36", "", "XE499OG1EH8ZZI0KXC6N83EKGT1BM97P2O77"},
  39. {"DirectInvalidLength33", "", "XE499OG1EH8ZZI0KXC6N83EKGT1BM97P2"},
  40. {"IndirectInvalidCheckSum", "", "XE35ETHXREGGOPHERSSS"},
  41. {"IndirectInvalidAssetIdentifier", "", "XE34ETHXREGGOPHERSSS"},
  42. {"IndirectInvalidLength19", "", "XE34ETHXREGGOPHERSS"},
  43. {"IndirectInvalidLength21", "", "XE34ETHXREGGOPHERSSSS"},
  44. }
  45. func TestICAPOK(t *testing.T) {
  46. for _, test := range icapOKTests {
  47. decodeEncodeTest(HexToAddress(test.addr), test.icap, t)
  48. }
  49. }
  50. func TestICAPInvalid(t *testing.T) {
  51. for _, test := range icapInvalidTests {
  52. failedDecodingTest(test.icap, t)
  53. }
  54. }
  55. func decodeEncodeTest(addr0 Address, icap0 string, t *testing.T) {
  56. icap1, err := AddressToICAP(addr0)
  57. if err != nil {
  58. t.Errorf("ICAP encoding failed: %s", err)
  59. }
  60. if icap1 != icap0 {
  61. t.Errorf("ICAP mismatch: have: %s want: %s", icap1, icap0)
  62. }
  63. addr1, err := ICAPToAddress(icap0)
  64. if err != nil {
  65. t.Errorf("ICAP decoding failed: %s", err)
  66. }
  67. if addr1 != addr0 {
  68. t.Errorf("Address mismatch: have: %x want: %x", addr1, addr0)
  69. }
  70. }
  71. func failedDecodingTest(icap string, t *testing.T) {
  72. addr, err := ICAPToAddress(icap)
  73. if err == nil {
  74. t.Errorf("Expected ICAP decoding to fail.")
  75. }
  76. if addr != (Address{}) {
  77. t.Errorf("Expected empty Address on failed ICAP decoding.")
  78. }
  79. }