123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package test1
- import (
- "bytes"
- "context"
- "fmt"
- zmostp_go "git.swzry.com/zry/zmostp/zmostp-go"
- "io"
- "os"
- "sync"
- "testing"
- )
- type TestCase struct {
- Channel uint32
- Payload []byte
- }
- var ENC *zmostp_go.Transmitter
- var DEC *zmostp_go.Receiver
- var PIPE_W *io.PipeWriter
- var PIPE_R *io.PipeReader
- var LH *LogHandler
- var INCH chan *zmostp_go.Message
- var OUTCH chan *zmostp_go.Message
- func TestMain(m *testing.M) {
- pr, pw := io.Pipe()
- PIPE_W = pw
- PIPE_R = pr
- LH = &LogHandler{}
- ENC = zmostp_go.NewTransmitter(&zmostp_go.TransmitterConfig{
- Writer: PIPE_W,
- HumanFriendlySending: false,
- EnableCRC: true,
- })
- DEC = zmostp_go.NewReceiver(&zmostp_go.ReceiverConfig{
- ByteReader: zmostp_go.NewSimpleByteReader(PIPE_R),
- EnableCRC: true,
- DecodeFailureLog: LH,
- })
- INCH = ENC.GetMessageInChannel()
- OUTCH = DEC.GetMessageOutChannel()
- wg := sync.WaitGroup{}
- wg.Add(2)
- ctx, cncl := context.WithCancel(context.Background())
- go func() {
- err := ENC.Run(ctx)
- if err != nil {
- panic(err)
- }
- wg.Done()
- }()
- go func() {
- err := DEC.Run(ctx)
- if err != nil {
- panic(err)
- }
- wg.Done()
- }()
- fmt.Println("==== Init Ok ====")
- exitCode := m.Run()
- fmt.Println("==== Test End ====")
- cncl()
- _ = PIPE_W.Close()
- _ = PIPE_R.Close()
- fmt.Println("==== Waiting for exit ====")
- wg.Wait()
- os.Exit(exitCode)
- }
- func FuzzLooping(f *testing.F) {
- f.Log(" ==== fuzz looping test ====")
- testcases := []TestCase{
- {0, []byte("hello")},
- {114514, []byte("hello, gensoukyo")},
- {0xAA550101, []byte{0xAA, 0xA5, 0x5A, 0x01, 0x55, 0x00}},
- }
- for _, tci := range testcases {
- f.Add(tci.Channel, tci.Payload)
- }
- f.Fuzz(func(t *testing.T, ch uint32, payload []byte) {
- INCH <- &zmostp_go.Message{
- Channel: ch,
- Payload: payload,
- }
- o := <-OUTCH
- if o.Channel != ch {
- t.Errorf("got channel: %d, want: %d", o.Channel, ch)
- }
- if !bytes.Equal(o.Payload, payload) {
- t.Errorf("got payload: %s, want: %s", o.Payload, payload)
- }
- })
- }
- func FuzzLoopingWithHumanFriendlySwitching(f *testing.F) {
- f.Log(" ==== fuzz looping test ====")
- testcases := []TestCase{
- {0, []byte("hello")},
- {114514, []byte("hello, gensoukyo")},
- {0xAA550101, []byte{0xAA, 0xA5, 0x5A, 0x01, 0x55, 0x00}},
- }
- for _, tci := range testcases {
- f.Add(tci.Channel, tci.Payload, true)
- f.Add(tci.Channel, tci.Payload, false)
- }
- f.Fuzz(func(t *testing.T, ch uint32, payload []byte, hf bool) {
- ENC.SetHumanFriendOutputOption(hf)
- INCH <- &zmostp_go.Message{
- Channel: ch,
- Payload: payload,
- }
- o := <-OUTCH
- if o.Channel != ch {
- t.Errorf("got channel: %d, want: %d", o.Channel, ch)
- }
- if !bytes.Equal(o.Payload, payload) {
- t.Errorf("got payload: %s, want: %s", o.Payload, payload)
- }
- })
- }
|