arrays_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package ndr
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "fmt"
  6. "reflect"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. const TestHeader = "01100800cccccccca00400000000000000000200"
  11. func TestParseDimensions(t *testing.T) {
  12. a := [2][2][2][]SimpleTest{}
  13. l, ta := parseDimensions(reflect.ValueOf(a))
  14. assert.Equal(t, 4, len(l), "dimension count not as expected")
  15. assert.Equal(t, []int{2, 2, 2, 0}, l, "lengths list not as expected")
  16. assert.Equal(t, "SimpleTest", ta.Name(), "type within array not as expected")
  17. }
  18. func TestMakeSubSlices(t *testing.T) {
  19. l := []int{2, 5, 3, 1}
  20. a := new([][][][]uint32)
  21. v := reflect.ValueOf(a)
  22. v = v.Elem()
  23. ty := v.Type()
  24. s := reflect.MakeSlice(ty, l[0], l[0])
  25. v.Set(s)
  26. makeSubSlices(v, l[1:])
  27. assert.Equal(t, "[[[[0] [0] [0]] [[0] [0] [0]] [[0] [0] [0]] [[0] [0] [0]] [[0] [0] [0]]] [[[0] [0] [0]] [[0] [0] [0]] [[0] [0] [0]] [[0] [0] [0]] [[0] [0] [0]]]]", fmt.Sprintf("%v", *a))
  28. }
  29. func TestDimensionCountFromTag(t *testing.T) {
  30. var a StructWithMultiDimensionalConformantSlice
  31. v := reflect.ValueOf(a)
  32. d, err := intFromTag(v.Type().Field(0).Tag, "test")
  33. if err != nil {
  34. t.Errorf("error getting dimensions from tag: %v", err)
  35. }
  36. assert.Equal(t, 3, d, "number of dimensions not as expected")
  37. }
  38. type StructWithArray struct {
  39. A [4]uint32
  40. }
  41. type StructWithMultiDimArray struct {
  42. A [2][3][2]uint32
  43. }
  44. type StructWithConformantSlice struct {
  45. A []uint32 `ndr:"conformant"`
  46. }
  47. type StructWithVaryingSlice struct {
  48. A []uint32 `ndr:"varying"`
  49. }
  50. type StructWithConformantVaryingSlice struct {
  51. A []uint32 `ndr:"conformant,varying"`
  52. }
  53. type StructWithMultiDimensionalConformantSlice struct {
  54. A [][][]uint32 `ndr:"conformant,test:3"`
  55. }
  56. type StructWithMultiDimensionalVaryingSlice struct {
  57. A [][][]uint32 `ndr:"varying"`
  58. }
  59. type StructWithMultiDimensionalConformantVaryingSlice struct {
  60. A [][][]uint32 `ndr:"conformant,varying"`
  61. }
  62. func TestReadUniDimensionalFixedArray(t *testing.T) {
  63. hexStr := TestHeader + "01000000020000000300000004000000"
  64. b, _ := hex.DecodeString(hexStr)
  65. a := new(StructWithArray)
  66. dec := NewDecoder(bytes.NewReader(b))
  67. err := dec.Decode(a)
  68. if err != nil {
  69. t.Fatalf("%v", err)
  70. }
  71. for i := range a.A {
  72. assert.Equal(t, uint32(i+1), a.A[i], "Value of index %d not as expected", i)
  73. }
  74. }
  75. func TestReadMultiDimensionalFixedArray(t *testing.T) {
  76. hexStr := TestHeader + "0100000002000000030000000400000005000000060000000700000008000000090000000a0000000b0000000c0000000d0000000e0000000f000000100000001100000012000000130000001400000015000000160000001700000018000000190000001a0000001b0000001c0000001d0000001e0000001f0000002000000021000000220000002300000024000000"
  77. b, _ := hex.DecodeString(hexStr)
  78. a := new(StructWithMultiDimArray)
  79. dec := NewDecoder(bytes.NewReader(b))
  80. err := dec.Decode(a)
  81. if err != nil {
  82. t.Fatalf("%v", err)
  83. }
  84. ar := [2][3][2]uint32{
  85. {
  86. {1, 2},
  87. {3, 4},
  88. {5, 6},
  89. },
  90. {
  91. {7, 8},
  92. {9, 10},
  93. {11, 12},
  94. },
  95. }
  96. assert.Equal(t, ar, a.A, "multi-dimensional fixed array not as expected")
  97. }
  98. func TestReadUniDimensionalConformantArray(t *testing.T) {
  99. hexStr := TestHeader + "0400000001000000020000000300000004000000"
  100. b, _ := hex.DecodeString(hexStr)
  101. a := new(StructWithConformantSlice)
  102. dec := NewDecoder(bytes.NewReader(b))
  103. err := dec.Decode(a)
  104. if err != nil {
  105. t.Fatalf("%v", err)
  106. }
  107. for i := range a.A {
  108. assert.Equal(t, uint32(i+1), a.A[i], "Value of index %d not as expected", i)
  109. }
  110. }
  111. func TestReadMultiDimensionalConformantArray(t *testing.T) {
  112. hexStr := TestHeader + "0200000003000000020000000100000002000000030000000400000005000000060000000700000008000000090000000a0000000b0000000c0000000d0000000e0000000f000000100000001100000012000000130000001400000015000000160000001700000018000000190000001a0000001b0000001c0000001d0000001e0000001f0000002000000021000000220000002300000024000000"
  113. b, _ := hex.DecodeString(hexStr)
  114. a := new(StructWithMultiDimensionalConformantSlice)
  115. dec := NewDecoder(bytes.NewReader(b))
  116. err := dec.Decode(a)
  117. if err != nil {
  118. t.Fatalf("%v", err)
  119. }
  120. ar := [][][]uint32{
  121. {
  122. {1, 2},
  123. {3, 4},
  124. {5, 6},
  125. },
  126. {
  127. {7, 8},
  128. {9, 10},
  129. {11, 12},
  130. },
  131. }
  132. assert.Equal(t, ar, a.A, "multi-dimensional conformant array not as expected")
  133. }
  134. func TestReadUniDimensionalVaryingArray(t *testing.T) {
  135. hexStr := TestHeader + "000000000400000001000000020000000300000004000000"
  136. b, _ := hex.DecodeString(hexStr)
  137. a := new(StructWithVaryingSlice)
  138. dec := NewDecoder(bytes.NewReader(b))
  139. err := dec.Decode(a)
  140. if err != nil {
  141. t.Fatalf("%v", err)
  142. }
  143. for i := range a.A {
  144. assert.Equal(t, uint32(i+1), a.A[i], "Value of index %d not as expected", i)
  145. }
  146. }
  147. func TestReadMultiDimensionalVaryingArray(t *testing.T) {
  148. hexStr := TestHeader + "0000000002000000000000000300000000000000020000000100000002000000030000000400000005000000060000000700000008000000090000000a0000000b0000000c0000000d0000000e0000000f000000100000001100000012000000130000001400000015000000160000001700000018000000190000001a0000001b0000001c0000001d0000001e0000001f0000002000000021000000220000002300000024000000"
  149. b, _ := hex.DecodeString(hexStr)
  150. a := new(StructWithMultiDimensionalVaryingSlice)
  151. dec := NewDecoder(bytes.NewReader(b))
  152. err := dec.Decode(a)
  153. if err != nil {
  154. t.Fatalf("%v", err)
  155. }
  156. ar := [][][]uint32{
  157. {
  158. {1, 2},
  159. {3, 4},
  160. {5, 6},
  161. },
  162. {
  163. {7, 8},
  164. {9, 10},
  165. {11, 12},
  166. },
  167. }
  168. assert.Equal(t, ar, a.A, "multi-dimensional conformant varying array not as expected")
  169. }
  170. func TestReadUniDimensionalConformantVaryingArray(t *testing.T) {
  171. hexStr := TestHeader + "04000000000000000400000001000000020000000300000004000000"
  172. b, _ := hex.DecodeString(hexStr)
  173. a := new(StructWithConformantVaryingSlice)
  174. dec := NewDecoder(bytes.NewReader(b))
  175. err := dec.Decode(a)
  176. if err != nil {
  177. t.Fatalf("%v", err)
  178. }
  179. for i := range a.A {
  180. assert.Equal(t, uint32(i+1), a.A[i], "Value of index %d not as expected", i)
  181. }
  182. }
  183. func TestReadMultiDimensionalConformantVaryingArray(t *testing.T) {
  184. hexStr := TestHeader + "0200000003000000020000000000000002000000000000000300000000000000020000000100000002000000030000000400000005000000060000000700000008000000090000000a0000000b0000000c0000000d0000000e0000000f000000100000001100000012000000130000001400000015000000160000001700000018000000190000001a0000001b0000001c0000001d0000001e0000001f0000002000000021000000220000002300000024000000"
  185. b, _ := hex.DecodeString(hexStr)
  186. a := new(StructWithMultiDimensionalConformantVaryingSlice)
  187. dec := NewDecoder(bytes.NewReader(b))
  188. err := dec.Decode(a)
  189. if err != nil {
  190. t.Fatalf("%v", err)
  191. }
  192. ar := [][][]uint32{
  193. {
  194. {1, 2},
  195. {3, 4},
  196. {5, 6},
  197. },
  198. {
  199. {7, 8},
  200. {9, 10},
  201. {11, 12},
  202. },
  203. }
  204. assert.Equal(t, ar, a.A, "multi-dimensional conformant varying array not as expected")
  205. }