union_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package ndr
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. const (
  9. testUnionSelected1Enc = "0100000001"
  10. testUnionSelected2Enc = "020000000200"
  11. testUnionSelected1NonEnc = "010000000100000001"
  12. testUnionSelected2NonEnc = "02000000020000000200"
  13. )
  14. type testUnionEncapsulated struct {
  15. Tag uint32 `ndr:"unionTag,encapsulated"`
  16. Value1 uint8 `ndr:"unionField"`
  17. Value2 uint16 `ndr:"unionField"`
  18. }
  19. type testUnionNonEncapsulated struct {
  20. Tag uint32 `ndr:"unionTag"`
  21. Value1 uint8 `ndr:"unionField"`
  22. Value2 uint16 `ndr:"unionField"`
  23. }
  24. func (u testUnionEncapsulated) SwitchFunc(tag interface{}) string {
  25. t := tag.(uint32)
  26. switch t {
  27. case 1:
  28. return "Value1"
  29. case 2:
  30. return "Value2"
  31. }
  32. return ""
  33. }
  34. func (u testUnionNonEncapsulated) SwitchFunc(tag interface{}) string {
  35. t := tag.(uint32)
  36. switch t {
  37. case 1:
  38. return "Value1"
  39. case 2:
  40. return "Value2"
  41. }
  42. return ""
  43. }
  44. func Test_readUnionEncapsulated(t *testing.T) {
  45. var tests = []struct {
  46. Hex string
  47. Tag uint32
  48. V1 uint8
  49. V2 uint16
  50. }{
  51. {testUnionSelected1Enc, uint32(1), uint8(1), uint16(0)},
  52. {testUnionSelected2Enc, uint32(2), uint8(0), uint16(2)},
  53. }
  54. for i, test := range tests {
  55. a := new(testUnionEncapsulated)
  56. hexStr := TestHeader + test.Hex
  57. b, _ := hex.DecodeString(hexStr)
  58. dec := NewDecoder(bytes.NewReader(b))
  59. err := dec.Decode(a)
  60. if err != nil {
  61. t.Fatalf("test %d: %v", i+1, err)
  62. }
  63. assert.Equal(t, test.Tag, a.Tag, "Tag value not as expected for test: %d", i+1)
  64. assert.Equal(t, test.V1, a.Value1, "Value1 not as expected for test: %d", i+1)
  65. assert.Equal(t, test.V2, a.Value2, "Value2 value not as expected for test: %d", i+1)
  66. }
  67. }
  68. func Test_readUnionNonEncapsulated(t *testing.T) {
  69. var tests = []struct {
  70. Hex string
  71. Tag uint32
  72. V1 uint8
  73. V2 uint16
  74. }{
  75. {testUnionSelected1NonEnc, uint32(1), uint8(1), uint16(0)},
  76. {testUnionSelected2NonEnc, uint32(2), uint8(0), uint16(2)},
  77. }
  78. for i, test := range tests {
  79. a := new(testUnionNonEncapsulated)
  80. hexStr := TestHeader + test.Hex
  81. b, _ := hex.DecodeString(hexStr)
  82. dec := NewDecoder(bytes.NewReader(b))
  83. err := dec.Decode(a)
  84. if err != nil {
  85. t.Fatalf("test %d: %v", i+1, err)
  86. }
  87. assert.Equal(t, test.Tag, a.Tag, "Tag value not as expected for test: %d", i+1)
  88. assert.Equal(t, test.V1, a.Value1, "Value1 not as expected for test: %d", i+1)
  89. assert.Equal(t, test.V2, a.Value2, "Value2 value not as expected for test: %d", i+1)
  90. }
  91. }