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.

70 lines
1.3 KiB

  1. package lsat
  2. import (
  3. "bytes"
  4. "errors"
  5. "testing"
  6. "github.com/lightningnetwork/lnd/lntypes"
  7. )
  8. var (
  9. testPaymentHash lntypes.Hash
  10. testTokenID [TokenIDSize]byte
  11. )
  12. // TestIdentifierSerialization ensures proper serialization of known identifier
  13. // versions and failures for unknown versions.
  14. func TestIdentifierSerialization(t *testing.T) {
  15. t.Parallel()
  16. tests := []struct {
  17. name string
  18. id Identifier
  19. err error
  20. }{
  21. {
  22. name: "valid identifier",
  23. id: Identifier{
  24. Version: LatestVersion,
  25. PaymentHash: testPaymentHash,
  26. TokenID: testTokenID,
  27. },
  28. err: nil,
  29. },
  30. {
  31. name: "unknown version",
  32. id: Identifier{
  33. Version: LatestVersion + 1,
  34. PaymentHash: testPaymentHash,
  35. TokenID: testTokenID,
  36. },
  37. err: ErrUnknownVersion,
  38. },
  39. }
  40. for _, test := range tests {
  41. test := test
  42. success := t.Run(test.name, func(t *testing.T) {
  43. var buf bytes.Buffer
  44. err := EncodeIdentifier(&buf, &test.id)
  45. if !errors.Is(err, test.err) {
  46. t.Fatalf("expected err \"%v\", got \"%v\"",
  47. test.err, err)
  48. }
  49. if test.err != nil {
  50. return
  51. }
  52. id, err := DecodeIdentifier(&buf)
  53. if err != nil {
  54. t.Fatalf("unable to decode identifier: %v", err)
  55. }
  56. if *id != test.id {
  57. t.Fatalf("expected id %v, got %v", test.id, *id)
  58. }
  59. })
  60. if !success {
  61. return
  62. }
  63. }
  64. }