| 9 | ) |
| 10 | |
| 11 | func TestRustInfoFile(t *testing.T) { |
| 12 | t.Parallel() |
| 13 | |
| 14 | file, err := os.Open("testdata/rust_info_test.bin") |
| 15 | if err != nil { |
| 16 | panic(err) |
| 17 | } |
| 18 | defer func() { |
| 19 | _ = file.Close() |
| 20 | }() |
| 21 | first := true |
| 22 | for { |
| 23 | info, err := RecvInfo(file) |
| 24 | // First info should be with invalid size. |
| 25 | // This tests if invalid info data is handled properly. |
| 26 | if first { |
| 27 | if !errors.Is(err, ErrUnexpectedInfoSize) { |
| 28 | t.Errorf("unexpected error: %s\n", err) |
| 29 | } |
| 30 | first = false |
| 31 | continue |
| 32 | } |
| 33 | if err != nil { |
| 34 | if errors.Is(err, ErrUnexpectedReadError) { |
| 35 | t.Errorf("unexpected error: %s\n", err) |
| 36 | } |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | switch { |
| 41 | case info.LogLine != nil: |
| 42 | if info.LogLine.Severity != 1 { |
| 43 | t.Errorf("unexpected Log severity: %d\n", info.LogLine.Severity) |
| 44 | } |
| 45 | if info.LogLine.Line != "prefix: test log" { |
| 46 | t.Errorf("unexpected Log line: %s\n", info.LogLine.Line) |
| 47 | } |
| 48 | |
| 49 | case info.ConnectionV4 != nil: |
| 50 | conn := info.ConnectionV4 |
| 51 | expected := connectionV4Internal{ |
| 52 | ID: 1, |
| 53 | ProcessID: 2, |
| 54 | Direction: 3, |
| 55 | Protocol: 4, |
| 56 | LocalIP: [4]byte{1, 2, 3, 4}, |
| 57 | RemoteIP: [4]byte{2, 3, 4, 5}, |
| 58 | LocalPort: 5, |
| 59 | RemotePort: 6, |
| 60 | PayloadLayer: 7, |
| 61 | } |
| 62 | if conn.connectionV4Internal != expected { |
| 63 | t.Errorf("unexpected ConnectionV4: %+v\n", conn) |
| 64 | } |
| 65 | if !bytes.Equal(conn.Payload, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) { |
| 66 | t.Errorf("unexpected ConnectionV4 payload: %+v\n", conn.Payload) |
| 67 | } |
| 68 | |