Skip to content

Commit

Permalink
fix(output/stdout): fix -c flag being ignored when combined with -r
Browse files Browse the repository at this point in the history
closes #136
  • Loading branch information
mozillazg committed Nov 3, 2024
1 parent 8f7728a commit 1cefb98
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 45 deletions.
9 changes: 6 additions & 3 deletions cmd/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package cmd
import (
"context"
"fmt"
"github.com/mozillazg/ptcpdump/internal/utils"
"io"
"os"
"path/filepath"

"github.com/mozillazg/ptcpdump/internal/log"
"github.com/mozillazg/ptcpdump/internal/metadata"
"github.com/mozillazg/ptcpdump/internal/parser"
"github.com/mozillazg/ptcpdump/internal/writer"
)

func read(ctx context.Context, opts *Options) error {
fpath := opts.ReadPath()
utils.OutStderr("reading from file %s\n", fpath)
log.Warnf("reading from file %s", fpath)

f, err := os.Open(fpath)
if err != nil {
Expand Down Expand Up @@ -45,7 +45,7 @@ func read(ctx context.Context, opts *Options) error {
}
}

var n int64
var n uint
for {
e, err := p.Parse()
if err != nil {
Expand All @@ -58,6 +58,9 @@ func read(ctx context.Context, opts *Options) error {
return err
}
n++
if opts.maxPacketCount > 0 && opts.maxPacketCount <= n {
break
}
}
if opts.onlyPrintCount {
fmt.Printf("%d packets\n", n)
Expand Down
8 changes: 8 additions & 0 deletions cmd/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ func TestFormat(t *testing.T) {
},
expectedOutFile: "../testdata/format/tcp.pcapng.out.txt",
},
{
name: "tcp -c",
opts: &Options{
readFilePath: "../testdata/format/tcp.pcapng",
maxPacketCount: 2,
},
expectedOutFile: "../testdata/format/tcp.pcapng.-c.out.txt",
},
{
name: "tcp -v",
opts: &Options{
Expand Down
2 changes: 2 additions & 0 deletions testdata/format/tcp.pcapng.-c.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
13:50:35.524360 lo curl.345650 Out IP 127.0.0.1.58694 > 127.0.0.1.8000: Flags [S], seq 217911223, win 65495, options [mss 65495,sackOK,TS val 2363239505 ecr 0,nop,wscale 7], length 0, ParentProc [ptcpdump.345626]
13:50:35.524995 lo curl.345650 In IP 127.0.0.1.58694 > 127.0.0.1.8000: Flags [S], seq 217911223, win 65495, options [mss 65495,sackOK,TS val 2363239505 ecr 0,nop,wscale 7], length 0, ParentProc [ptcpdump.345626]
9 changes: 1 addition & 8 deletions testdata/test_run_with_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,18 @@ function test_ptcpdump() {

cat "${LNAME}"
cat "${LNAME}" | grep '/usr/bin/curl'
cat "${LNAME}" | grep -F 'curl -m 10 1.1.1.1'
cat "${LNAME}" | grep -F ' > 1.1.1.1.80: Flags [S],' # SYN
}

function test_tcpdump_read() {
which tcpdump || (apt update || true && apt install -y tcpdump)
tcpdump -nr "${FNAME}"
tcpdump -nr "${FNAME}" | grep -F ' > 1.1.1.1.80: Flags [S],' # SYN
}

function test_ptcpdump_read() {
EXPECT_NAME="${LNAME}.read.expect"
cat "${LNAME}" |grep -v packets |grep -v WARN > "${EXPECT_NAME}"
timeout 30s ${CMD} -v -r "${FNAME}" |tee "${RNAME}"
diff "${EXPECT_NAME}" "${RNAME}"
}

function main() {
test_ptcpdump
test_tcpdump_read
test_ptcpdump_read
}

Expand Down
17 changes: 1 addition & 16 deletions testdata/test_sub_curl_domain_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,11 @@ function test_ptcpdump() {
cat "${LNAME}"
cat "${LNAME}" | grep '/usr/bin/curl'
cat "${LNAME}" | grep -F '.80: Flags [S],'
cat "${LNAME}" | grep -F 'curl -m 10 ubuntu.com'
}

function test_tcpdump_read() {
which tcpdump || (apt update || true && apt install -y tcpdump)
tcpdump -nr "${FNAME}"
tcpdump -nr "${FNAME}" | grep -F '.80: Flags [S],' # SYN
}

function test_ptcpdump_read() {
EXPECT_NAME="${LNAME}.read.expect"
cat "${LNAME}" > "${EXPECT_NAME}"
timeout 30s ${CMD} -v -r "${FNAME}" > "${RNAME}"
diff "${EXPECT_NAME}" "${RNAME}"
}


function main() {
test_ptcpdump
test_tcpdump_read
test_ptcpdump_read
}

main
19 changes: 1 addition & 18 deletions testdata/test_sub_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,14 @@ function test_ptcpdump() {

cat "${LNAME}"
cat "${LNAME}" | grep '/usr/bin/curl'
cat "${LNAME}" | grep -F 'curl -m 10 1.1.1.1'
cat "${LNAME}" | grep -F ' > 1.1.1.1.80: Flags [S],' # SYN
cat "${LNAME}" | grep -P '1.1.1.1.80 > .*: Flags \[S.\],' # SYN-ACK
cat "${LNAME}" | grep -F ' > 1.1.1.1.80: Flags [.],' # ACK
}

function test_tcpdump_read() {
which tcpdump || (apt update || true && apt install -y tcpdump)
tcpdump -nr "${FNAME}"
tcpdump -nr "${FNAME}" | grep -F ' > 1.1.1.1.80: Flags [S],' # SYN
tcpdump -nr "${FNAME}" | grep -P '1.1.1.1.80 > .*: Flags \[S.\],' # SYN-ACK
tcpdump -nr "${FNAME}" | grep -F ' > 1.1.1.1.80: Flags [.],' # ACK
}

function test_ptcpdump_read() {
EXPECT_NAME="${LNAME}.read.expect"
cat "${LNAME}" > "${EXPECT_NAME}"
timeout 30s ${CMD} -v -r "${FNAME}" > "${RNAME}"
diff "${EXPECT_NAME}" "${RNAME}"
}


function main() {
test_ptcpdump
test_tcpdump_read
test_ptcpdump_read
}

main

0 comments on commit 1cefb98

Please sign in to comment.