extra_bytes_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package lnwire
  2. import (
  3. "bytes"
  4. "math/rand"
  5. "reflect"
  6. "testing"
  7. "testing/quick"
  8. "github.com/lightningnetwork/lnd/tlv"
  9. "github.com/stretchr/testify/require"
  10. )
  11. // TestExtraOpaqueDataEncodeDecode tests that we're able to encode/decode
  12. // arbitrary payloads.
  13. func TestExtraOpaqueDataEncodeDecode(t *testing.T) {
  14. t.Parallel()
  15. type testCase struct {
  16. // emptyBytes indicates if we should try to encode empty bytes
  17. // or not.
  18. emptyBytes bool
  19. // inputBytes if emptyBytes is false, then we'll read in this
  20. // set of bytes instead.
  21. inputBytes []byte
  22. }
  23. // We should be able to read in an arbitrary set of bytes as an
  24. // ExtraOpaqueData, then encode those new bytes into a new instance.
  25. // The final two instances should be identical.
  26. scenario := func(test testCase) bool {
  27. var (
  28. extraData ExtraOpaqueData
  29. b bytes.Buffer
  30. )
  31. copy(extraData[:], test.inputBytes)
  32. if err := extraData.Encode(&b); err != nil {
  33. t.Fatalf("unable to encode extra data: %v", err)
  34. return false
  35. }
  36. var newBytes ExtraOpaqueData
  37. if err := newBytes.Decode(&b); err != nil {
  38. t.Fatalf("unable to decode extra bytes: %v", err)
  39. return false
  40. }
  41. if !bytes.Equal(extraData[:], newBytes[:]) {
  42. t.Fatalf("expected %x, got %x", extraData,
  43. newBytes)
  44. return false
  45. }
  46. return true
  47. }
  48. // We'll make a function to generate random test data. Half of the
  49. // time, we'll actually feed in blank bytes.
  50. quickCfg := &quick.Config{
  51. Values: func(v []reflect.Value, r *rand.Rand) {
  52. var newTestCase testCase
  53. if r.Int31()%2 == 0 {
  54. newTestCase.emptyBytes = true
  55. }
  56. if !newTestCase.emptyBytes {
  57. numBytes := r.Int31n(1000)
  58. newTestCase.inputBytes = make([]byte, numBytes)
  59. _, err := r.Read(newTestCase.inputBytes)
  60. if err != nil {
  61. t.Fatalf("unable to gen random bytes: %v", err)
  62. return
  63. }
  64. }
  65. v[0] = reflect.ValueOf(newTestCase)
  66. },
  67. }
  68. if err := quick.Check(scenario, quickCfg); err != nil {
  69. t.Fatalf("encode+decode test failed: %v", err)
  70. }
  71. }
  72. type recordProducer struct {
  73. record tlv.Record
  74. }
  75. func (r *recordProducer) Record() tlv.Record {
  76. return r.record
  77. }
  78. // TestExtraOpaqueDataPackUnpackRecords tests that we're able to pack a set of
  79. // tlv.Records into a stream, and unpack them on the other side to obtain the
  80. // same set of records.
  81. func TestExtraOpaqueDataPackUnpackRecords(t *testing.T) {
  82. t.Parallel()
  83. var (
  84. type1 tlv.Type = 1
  85. type2 tlv.Type = 2
  86. channelType1 uint8 = 2
  87. channelType2 uint8
  88. hop1 uint32 = 99
  89. hop2 uint32
  90. )
  91. testRecordsProducers := []tlv.RecordProducer{
  92. &recordProducer{tlv.MakePrimitiveRecord(type1, &channelType1)},
  93. &recordProducer{tlv.MakePrimitiveRecord(type2, &hop1)},
  94. }
  95. // Now that we have our set of sample records and types, we'll encode
  96. // them into the passed ExtraOpaqueData instance.
  97. var extraBytes ExtraOpaqueData
  98. if err := extraBytes.PackRecords(testRecordsProducers...); err != nil {
  99. t.Fatalf("unable to pack records: %v", err)
  100. }
  101. // We'll now simulate decoding these types _back_ into records on the
  102. // other side.
  103. newRecords := []tlv.RecordProducer{
  104. &recordProducer{tlv.MakePrimitiveRecord(type1, &channelType2)},
  105. &recordProducer{tlv.MakePrimitiveRecord(type2, &hop2)},
  106. }
  107. typeMap, err := extraBytes.ExtractRecords(newRecords...)
  108. require.NoError(t, err, "unable to extract record")
  109. // We should find that the new backing values have been populated with
  110. // the proper value.
  111. switch {
  112. case channelType1 != channelType2:
  113. t.Fatalf("wrong record for channel type: expected %v, got %v",
  114. channelType1, channelType2)
  115. case hop1 != hop2:
  116. t.Fatalf("wrong record for hop: expected %v, got %v", hop1,
  117. hop2)
  118. }
  119. // Both types we created above should be found in the type map.
  120. if _, ok := typeMap[type1]; !ok {
  121. t.Fatalf("type1 not found in typeMap")
  122. }
  123. if _, ok := typeMap[type2]; !ok {
  124. t.Fatalf("type2 not found in typeMap")
  125. }
  126. }