You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.5 KiB

  1. package common
  2. import (
  3. "database/sql"
  4. "database/sql/driver"
  5. "encoding/binary"
  6. "encoding/hex"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestSignatureConstant(t *testing.T) {
  11. signatureConstant := uint32(3322668559)
  12. var signatureConstantBytes [4]byte
  13. binary.BigEndian.PutUint32(signatureConstantBytes[:], signatureConstant)
  14. assert.Equal(t, SignatureConstantBytes, signatureConstantBytes[:])
  15. assert.Equal(t, "c60be60f", hex.EncodeToString(SignatureConstantBytes))
  16. }
  17. func TestTxIDScannerValue(t *testing.T) {
  18. txid0 := &TxID{}
  19. txid1 := &TxID{}
  20. txid0B := [TxIDLen]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
  21. 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}
  22. txid1B := [TxIDLen]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  23. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  24. copy(txid0[:], txid0B[:])
  25. copy(txid1[:], txid1B[:])
  26. var value driver.Valuer
  27. var scan sql.Scanner
  28. value = txid0
  29. scan = txid1
  30. fromDB, err := value.Value()
  31. assert.NoError(t, err)
  32. assert.NoError(t, scan.Scan(fromDB))
  33. assert.Equal(t, value, scan)
  34. }
  35. func TestTxIDMarshalers(t *testing.T) {
  36. h := []byte("0x02f8b4197b990fcef7ab11021675b4532e584b2c6b3f32562a5128ff00dceb9a5b")
  37. var txid TxID
  38. err := txid.UnmarshalText(h)
  39. assert.NoError(t, err)
  40. assert.Equal(t, h, []byte(txid.String()))
  41. h2, err := txid.MarshalText()
  42. assert.NoError(t, err)
  43. assert.Equal(t, h, h2)
  44. var txid2 TxID
  45. err = txid2.UnmarshalText(h2)
  46. assert.NoError(t, err)
  47. assert.Equal(t, h2, []byte(txid2.String()))
  48. assert.Equal(t, h, h2)
  49. }