echoServer decodes the incoming batch, echoes each frame's payload back (with the SYN bit cleared and seq reset per session), and returns it.
(t *testing.T, aead *frame.Crypto)
| 45 | // echoServer decodes the incoming batch, echoes each frame's payload back |
| 46 | // (with the SYN bit cleared and seq reset per session), and returns it. |
| 47 | func echoServer(t *testing.T, aead *frame.Crypto) (*httptest.Server, *int) { |
| 48 | t.Helper() |
| 49 | var hits int |
| 50 | var mu sync.Mutex |
| 51 | rxSeqBySession := map[[frame.SessionIDLen]byte]uint64{} |
| 52 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 53 | mu.Lock() |
| 54 | hits++ |
| 55 | mu.Unlock() |
| 56 | body, _ := io.ReadAll(r.Body) |
| 57 | clientID, in, err := frame.DecodeBatch(aead, body) |
| 58 | if err != nil { |
| 59 | t.Errorf("server decode: %v", err) |
| 60 | w.WriteHeader(500) |
| 61 | return |
| 62 | } |
| 63 | var out []*frame.Frame |
| 64 | mu.Lock() |
| 65 | for _, f := range in { |
| 66 | seq := rxSeqBySession[f.SessionID] |
| 67 | rxSeqBySession[f.SessionID] = seq + 1 |
| 68 | out = append(out, &frame.Frame{ |
| 69 | SessionID: f.SessionID, |
| 70 | Seq: seq, |
| 71 | Payload: f.Payload, |
| 72 | }) |
| 73 | } |
| 74 | mu.Unlock() |
| 75 | respBody, _ := frame.EncodeBatch(aead, clientID, out) |
| 76 | w.Header().Set("Content-Type", "text/plain") |
| 77 | _, _ = w.Write(respBody) |
| 78 | })) |
| 79 | return srv, &hits |
| 80 | } |
| 81 | |
| 82 | func TestCarrier_RoundTripEcho(t *testing.T) { |
| 83 | aead, err := frame.NewCryptoFromHexKey(testKeyHex) |
no test coverage detected