decoder_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package ndr
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestReadCommonHeader(t *testing.T) {
  9. var tests = []struct {
  10. EncodedHex string
  11. ExpectFail bool
  12. }{
  13. {"01100800cccccccc", false}, // Little Endian
  14. {"01000008cccccccc", false}, // Big Endian have to change the bytes for the header size? This test vector was artificially created. Need proper test vector
  15. //{"01100800cccccccc1802000000000000", false},
  16. //{"01100800cccccccc0002000000000000", false},
  17. //{"01100800cccccccc0001000000000000", false},
  18. //{"01100800cccccccce000000000000000", false},
  19. //{"01100800ccccccccf000000000000000", false},
  20. //{"01100800cccccccc7801000000000000", false},
  21. //{"01100800cccccccc4801000000000000", false},
  22. //{"01100800ccccccccd001000000000000", false},
  23. {"02100800cccccccc", true}, // Incorrect version
  24. {"02100900cccccccc", true}, // Incorrect length
  25. }
  26. for i, test := range tests {
  27. b, _ := hex.DecodeString(test.EncodedHex)
  28. dec := NewDecoder(bytes.NewReader(b))
  29. err := dec.readCommonHeader()
  30. if err != nil && !test.ExpectFail {
  31. t.Errorf("error reading common header of test %d: %v", i, err)
  32. }
  33. if err == nil && test.ExpectFail {
  34. t.Errorf("expected failure on reading common header of test %d: %v", i, err)
  35. }
  36. }
  37. }
  38. func TestReadPrivateHeader(t *testing.T) {
  39. var tests = []struct {
  40. EncodedHex string
  41. ExpectFail bool
  42. Length int
  43. }{
  44. {"01100800cccccccc1802000000000000", false, 536},
  45. {"01100800cccccccc0002000000000000", false, 512},
  46. {"01100800cccccccc0001000000000000", false, 256},
  47. {"01100800ccccccccFF00000000000000", true, 255}, // Length not multiple of 8
  48. {"01100800cccccccc00010000000000", true, 256}, // Too short
  49. }
  50. for i, test := range tests {
  51. b, _ := hex.DecodeString(test.EncodedHex)
  52. dec := NewDecoder(bytes.NewReader(b))
  53. err := dec.readCommonHeader()
  54. if err != nil {
  55. t.Errorf("error reading common header of test %d: %v", i, err)
  56. }
  57. err = dec.readPrivateHeader()
  58. if err != nil && !test.ExpectFail {
  59. t.Errorf("error reading private header of test %d: %v", i, err)
  60. }
  61. if err == nil && test.ExpectFail {
  62. t.Errorf("expected failure on reading private header of test %d: %v", i, err)
  63. }
  64. if dec.ph.ObjectBufferLength != uint32(test.Length) {
  65. t.Errorf("Objectbuffer length expected %d actual %d", test.Length, dec.ph.ObjectBufferLength)
  66. }
  67. }
  68. }
  69. type SimpleTest struct {
  70. A uint32
  71. B uint32
  72. }
  73. func TestBasicDecode(t *testing.T) {
  74. hexStr := "01100800cccccccca00400000000000000000200d186660f656ac601"
  75. b, _ := hex.DecodeString(hexStr)
  76. ft := new(SimpleTest)
  77. dec := NewDecoder(bytes.NewReader(b))
  78. err := dec.Decode(ft)
  79. if err != nil {
  80. t.Fatalf("error decoding: %v", err)
  81. }
  82. assert.Equal(t, uint32(258377425), ft.A, "Value of field A not as expected")
  83. assert.Equal(t, uint32(29780581), ft.B, "Value of field B not as expected %d")
  84. }
  85. func TestBasicDecodeOverRun(t *testing.T) {
  86. hexStr := "01100800cccccccca00400000000000000000200d186660f"
  87. b, _ := hex.DecodeString(hexStr)
  88. ft := new(SimpleTest)
  89. dec := NewDecoder(bytes.NewReader(b))
  90. err := dec.Decode(ft)
  91. if err == nil {
  92. t.Errorf("Expected error for trying to read more than the bytes we have")
  93. }
  94. }
  95. type testEmbeddingPointer struct {
  96. A testEmbeddedPointer `ndr:"pointer"`
  97. B uint32 // 1
  98. }
  99. type testEmbeddedPointer struct {
  100. C testEmbeddedPointer2 `ndr:"pointer"`
  101. D uint32 `ndr:"pointer"` // 2
  102. E uint32 // 3
  103. }
  104. type testEmbeddedPointer2 struct {
  105. F uint32 `ndr:"pointer"` // 4
  106. G uint32 // 5
  107. }
  108. func Test_EmbeddedPointers(t *testing.T) {
  109. hexStr := TestHeader + "00040002" + "01000000" + "00040002" + "00040002" + "03000000" + "00040002" + "05000000" + "04000000" + "02000000"
  110. b, _ := hex.DecodeString(hexStr)
  111. ft := new(testEmbeddingPointer)
  112. dec := NewDecoder(bytes.NewReader(b))
  113. err := dec.Decode(ft)
  114. if err != nil {
  115. t.Fatalf("error decoding: %v", err)
  116. }
  117. assert.Equal(t, uint32(1), ft.B)
  118. assert.Equal(t, uint32(2), ft.A.D)
  119. assert.Equal(t, uint32(3), ft.A.E)
  120. assert.Equal(t, uint32(4), ft.A.C.F)
  121. assert.Equal(t, uint32(5), ft.A.C.G)
  122. }