cid_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2016 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 ens
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "encoding/hex"
  21. "fmt"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/common"
  24. )
  25. // Tests for the decoding of the example ENS
  26. func TestEIPSpecCidDecode(t *testing.T) {
  27. const (
  28. eipSpecHash = "e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
  29. eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
  30. dagPb = 0x70
  31. sha2256 = 0x12
  32. )
  33. b, err := hex.DecodeString(eipSpecHash)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. hashBytes, err := hex.DecodeString(eipHash)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. storageNs, contentType, hashType, hashLength, decodedHashBytes, err := decodeEIP1577ContentHash(b)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. if storageNs != nsIpfs {
  46. t.Fatal("wrong ns")
  47. }
  48. if contentType != dagPb {
  49. t.Fatal("should be ipfs typecode")
  50. }
  51. if hashType != sha2256 {
  52. t.Fatal("should be sha2-256")
  53. }
  54. if hashLength != 32 {
  55. t.Fatal("should be 32")
  56. }
  57. if !bytes.Equal(hashBytes, decodedHashBytes) {
  58. t.Fatal("should be equal")
  59. }
  60. }
  61. func TestManualCidDecode(t *testing.T) {
  62. // call cid encode method with hash. expect byte slice returned, compare according to spec
  63. for _, v := range []struct {
  64. name string
  65. headerBytes []byte
  66. wantErr bool
  67. }{
  68. {
  69. name: "values correct, should not fail",
  70. headerBytes: []byte{0xe4, 0x01, 0xfa, 0x1b, 0x20},
  71. wantErr: false,
  72. },
  73. {
  74. name: "cid version wrong, should fail",
  75. headerBytes: []byte{0xe4, 0x00, 0xfa, 0x1b, 0x20},
  76. wantErr: true,
  77. },
  78. {
  79. name: "hash length wrong, should fail",
  80. headerBytes: []byte{0xe4, 0x01, 0xfa, 0x1b, 0x1f},
  81. wantErr: true,
  82. },
  83. {
  84. name: "values correct for ipfs, should fail",
  85. headerBytes: []byte{0xe3, 0x01, 0x70, 0x12, 0x20},
  86. wantErr: true,
  87. },
  88. {
  89. name: "loose values for swarm, todo remove, should not fail",
  90. headerBytes: []byte{0xe4, 0x01, 0x70, 0x12, 0x20},
  91. wantErr: false,
  92. },
  93. {
  94. name: "loose values for swarm, todo remove, should not fail",
  95. headerBytes: []byte{0xe4, 0x01, 0x99, 0x99, 0x20},
  96. wantErr: false,
  97. },
  98. } {
  99. t.Run(v.name, func(t *testing.T) {
  100. const eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
  101. var bb []byte
  102. buf := make([]byte, binary.MaxVarintLen64)
  103. for _, vv := range v.headerBytes {
  104. n := binary.PutUvarint(buf, uint64(vv))
  105. bb = append(bb, buf[:n]...)
  106. }
  107. h := common.HexToHash(eipHash)
  108. bb = append(bb, h[:]...)
  109. str := hex.EncodeToString(bb)
  110. fmt.Println(str)
  111. decodedHash, e := extractContentHash(bb)
  112. switch v.wantErr {
  113. case true:
  114. if e == nil {
  115. t.Fatal("the decode should fail")
  116. }
  117. case false:
  118. if e != nil {
  119. t.Fatalf("the deccode shouldnt fail: %v", e)
  120. }
  121. if !bytes.Equal(decodedHash[:], h[:]) {
  122. t.Fatal("hashes not equal")
  123. }
  124. }
  125. })
  126. }
  127. }
  128. func TestManuelCidEncode(t *testing.T) {
  129. // call cid encode method with hash. expect byte slice returned, compare according to spec
  130. const eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
  131. cidBytes, err := EncodeSwarmHash(common.HexToHash(eipHash))
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. // logic in extractContentHash is unit tested thoroughly
  136. // hence we just check that the returned hash is equal
  137. h, err := extractContentHash(cidBytes)
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. if bytes.Equal(h[:], cidBytes) {
  142. t.Fatal("should be equal")
  143. }
  144. }