loop_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package test1
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. zmostp_go "git.swzry.com/zry/zmostp/zmostp-go"
  7. "io"
  8. "os"
  9. "sync"
  10. "testing"
  11. )
  12. type TestCase struct {
  13. Channel uint32
  14. Payload []byte
  15. }
  16. var ENC *zmostp_go.Transmitter
  17. var DEC *zmostp_go.Receiver
  18. var PIPE_W *io.PipeWriter
  19. var PIPE_R *io.PipeReader
  20. var LH *LogHandler
  21. var INCH chan *zmostp_go.Message
  22. var OUTCH chan *zmostp_go.Message
  23. func TestMain(m *testing.M) {
  24. pr, pw := io.Pipe()
  25. PIPE_W = pw
  26. PIPE_R = pr
  27. LH = &LogHandler{}
  28. ENC = zmostp_go.NewTransmitter(&zmostp_go.TransmitterConfig{
  29. Writer: PIPE_W,
  30. HumanFriendlySending: false,
  31. EnableCRC: true,
  32. })
  33. DEC = zmostp_go.NewReceiver(&zmostp_go.ReceiverConfig{
  34. ByteReader: zmostp_go.NewSimpleByteReader(PIPE_R),
  35. EnableCRC: true,
  36. DecodeFailureLog: LH,
  37. })
  38. INCH = ENC.GetMessageInChannel()
  39. OUTCH = DEC.GetMessageOutChannel()
  40. wg := sync.WaitGroup{}
  41. wg.Add(2)
  42. ctx, cncl := context.WithCancel(context.Background())
  43. go func() {
  44. err := ENC.Run(ctx)
  45. if err != nil {
  46. panic(err)
  47. }
  48. wg.Done()
  49. }()
  50. go func() {
  51. err := DEC.Run(ctx)
  52. if err != nil {
  53. panic(err)
  54. }
  55. wg.Done()
  56. }()
  57. fmt.Println("==== Init Ok ====")
  58. exitCode := m.Run()
  59. fmt.Println("==== Test End ====")
  60. cncl()
  61. _ = PIPE_W.Close()
  62. _ = PIPE_R.Close()
  63. fmt.Println("==== Waiting for exit ====")
  64. wg.Wait()
  65. os.Exit(exitCode)
  66. }
  67. func FuzzLooping(f *testing.F) {
  68. f.Log(" ==== fuzz looping test ====")
  69. testcases := []TestCase{
  70. {0, []byte("hello")},
  71. {114514, []byte("hello, gensoukyo")},
  72. {0xAA550101, []byte{0xAA, 0xA5, 0x5A, 0x01, 0x55, 0x00}},
  73. }
  74. for _, tci := range testcases {
  75. f.Add(tci.Channel, tci.Payload)
  76. }
  77. f.Fuzz(func(t *testing.T, ch uint32, payload []byte) {
  78. INCH <- &zmostp_go.Message{
  79. Channel: ch,
  80. Payload: payload,
  81. }
  82. o := <-OUTCH
  83. if o.Channel != ch {
  84. t.Errorf("got channel: %d, want: %d", o.Channel, ch)
  85. }
  86. if !bytes.Equal(o.Payload, payload) {
  87. t.Errorf("got payload: %s, want: %s", o.Payload, payload)
  88. }
  89. })
  90. }
  91. func FuzzLoopingWithHumanFriendlySwitching(f *testing.F) {
  92. f.Log(" ==== fuzz looping test ====")
  93. testcases := []TestCase{
  94. {0, []byte("hello")},
  95. {114514, []byte("hello, gensoukyo")},
  96. {0xAA550101, []byte{0xAA, 0xA5, 0x5A, 0x01, 0x55, 0x00}},
  97. }
  98. for _, tci := range testcases {
  99. f.Add(tci.Channel, tci.Payload, true)
  100. f.Add(tci.Channel, tci.Payload, false)
  101. }
  102. f.Fuzz(func(t *testing.T, ch uint32, payload []byte, hf bool) {
  103. ENC.SetHumanFriendOutputOption(hf)
  104. INCH <- &zmostp_go.Message{
  105. Channel: ch,
  106. Payload: payload,
  107. }
  108. o := <-OUTCH
  109. if o.Channel != ch {
  110. t.Errorf("got channel: %d, want: %d", o.Channel, ch)
  111. }
  112. if !bytes.Equal(o.Payload, payload) {
  113. t.Errorf("got payload: %s, want: %s", o.Payload, payload)
  114. }
  115. })
  116. }