From a433c8a9ca3e5ef484ee6484a9211637701b2a15 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 16 Dec 2022 10:45:21 +0800 Subject: [PATCH] feat: capture pcap file for iOS, including CLI hrp ios pcap --- docs/CHANGELOG.md | 2 +- hrp/cmd/ios/pcap.go | 58 ++++++++++++++++++++++++++++++++++++++ hrp/pkg/uixt/ios_device.go | 3 +- 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 hrp/cmd/ios/pcap.go diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 0d3afe7c..3e364689 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -8,7 +8,7 @@ - feat: run xctest before start ios automation - feat: run step with specified loop times - feat: add options for FindTexts -- feat: capture pcap file for iOS +- feat: capture pcap file for iOS, including CLI `hrp ios pcap` and option `uixt.WithIOSPcapOn(true)` - refactor: move all UI APIs to uixt pkg - docs: add examples for UI APIs diff --git a/hrp/cmd/ios/pcap.go b/hrp/cmd/ios/pcap.go new file mode 100644 index 00000000..b4138ea2 --- /dev/null +++ b/hrp/cmd/ios/pcap.go @@ -0,0 +1,58 @@ +package ios + +import ( + "os" + "os/signal" + "syscall" + "time" + + "github.com/httprunner/httprunner/v4/hrp/internal/builtin" + "github.com/httprunner/httprunner/v4/hrp/internal/env" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" +) + +var pcapCmd = &cobra.Command{ + Use: "pcap", + Short: "capture ios network packets", + RunE: func(cmd *cobra.Command, args []string) error { + device, err := uixt.NewIOSDevice( + uixt.WithUDID(udid), + ) + if err != nil { + log.Fatal().Err(err).Msg("failed to init ios device") + } + + err = builtin.EnsureFolderExists(env.ResultsPath) + if err != nil { + return err + } + + if err = device.StartPcap(); err != nil { + return err + } + defer device.StopPcap() + + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGTERM, syscall.SIGINT) + timer := time.NewTimer(time.Duration(timeDuration) * time.Second) + for { + select { + case <-timer.C: + return nil + case <-c: + log.Warn().Msg("received signal, stop pcap") + return nil + } + } + }, +} + +var timeDuration int + +func init() { + pcapCmd.Flags().StringVarP(&udid, "udid", "u", "", "specify device by udid") + pcapCmd.Flags().IntVarP(&timeDuration, "duration", "t", 10, "specify time duraion in seconds") + iosRootCmd.AddCommand(pcapCmd) +} diff --git a/hrp/pkg/uixt/ios_device.go b/hrp/pkg/uixt/ios_device.go index 347dce7e..a508a777 100644 --- a/hrp/pkg/uixt/ios_device.go +++ b/hrp/pkg/uixt/ios_device.go @@ -388,13 +388,13 @@ func (dev *IOSDevice) StopPerf() string { } func (dev *IOSDevice) StartPcap() error { + log.Info().Msg("start packet capture") packets, err := dev.d.PcapStart() if err != nil { return err } dev.pcapFile = filepath.Join(env.ResultsPath, "dump.pcap") - log.Info().Str("pcapFile", dev.pcapFile).Msg("create pcap file") file, err := os.OpenFile(dev.pcapFile, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o755) if err != nil { @@ -435,6 +435,7 @@ func (dev *IOSDevice) StopPcap() string { return "" } close(dev.pcapStop) + log.Info().Str("pcapFile", dev.pcapFile).Msg("stop packet capture") return dev.pcapFile }