Prevent NetworkTraceHandlerTest from touching BPF.

Despite extending the handler with a test-only version, calling
HandlerForTest::Register creates a NetworkTraceHandler instead. This
change adds a class-level toggle to skip certain code in tests. Without
this, it's possible for the test to see system-level packets and fail
(although I haven't seen this yet).

Bug: 246985031
Test: atest libnetworkstats_test w/ logs in the poller Start/Stop
Change-Id: If22d91e5449bc774961c58115ba84ca2a4bcde59
This commit is contained in:
Ryan Zuklie 2023-04-03 16:00:15 -07:00
commit f69f94dd42
3 changed files with 9 additions and 11 deletions

View file

@ -136,10 +136,12 @@ void NetworkTraceHandler::OnSetup(const SetupArgs& args) {
}
void NetworkTraceHandler::OnStart(const StartArgs&) {
if (mIsTest) return; // Don't touch non-hermetic bpf in test.
mStarted = sPoller.Start(mPollMs);
}
void NetworkTraceHandler::OnStop(const StopArgs&) {
if (mIsTest) return; // Don't touch non-hermetic bpf in test.
if (mStarted) sPoller.Stop();
mStarted = false;
}

View file

@ -33,14 +33,6 @@ using ::perfetto::protos::Trace;
using ::perfetto::protos::TracePacket;
using ::perfetto::protos::TrafficDirection;
// This handler makes OnStart and OnStop a no-op so that tracing is not really
// started on the device.
class HandlerForTest : public NetworkTraceHandler {
public:
void OnStart(const StartArgs&) override {}
void OnStop(const StopArgs&) override {}
};
class NetworkTraceHandlerTest : public testing::Test {
protected:
// Starts a tracing session with the handler under test.
@ -52,7 +44,7 @@ class NetworkTraceHandlerTest : public testing::Test {
perfetto::DataSourceDescriptor dsd;
dsd.set_name("test.network_packets");
HandlerForTest::Register(dsd);
NetworkTraceHandler::Register(dsd, /*isTest=*/true);
perfetto::TraceConfig cfg;
cfg.add_buffers()->set_size_kb(1024);
@ -94,7 +86,7 @@ class NetworkTraceHandlerTest : public testing::Test {
std::vector<TracePacket>* output,
NetworkPacketTraceConfig config = {}) {
auto session = StartTracing(config);
HandlerForTest::Trace([&](HandlerForTest::TraceContext ctx) {
NetworkTraceHandler::Trace([&](NetworkTraceHandler::TraceContext ctx) {
ctx.GetDataSourceLocked()->Write(input, ctx);
ctx.Flush();
});
@ -357,7 +349,7 @@ TEST_F(NetworkTraceHandlerTest, Interning) {
auto session = StartTracing(config);
HandlerForTest::Trace([&](HandlerForTest::TraceContext ctx) {
NetworkTraceHandler::Trace([&](NetworkTraceHandler::TraceContext ctx) {
ctx.GetDataSourceLocked()->Write(inputs[0], ctx);
ctx.GetDataSourceLocked()->Write(inputs[1], ctx);
ctx.GetDataSourceLocked()->Write(inputs[2], ctx);

View file

@ -70,6 +70,9 @@ class NetworkTraceHandler
// Connects to the system Perfetto daemon and registers the trace handler.
static void InitPerfettoTracing();
// When isTest is true, skip non-hermetic code.
NetworkTraceHandler(bool isTest = false) : mIsTest(isTest) {}
// perfetto::DataSource overrides:
void OnSetup(const SetupArgs& args) override;
void OnStart(const StartArgs&) override;
@ -92,6 +95,7 @@ class NetworkTraceHandler
static internal::NetworkTracePoller sPoller;
bool mStarted;
bool mIsTest;
// Values from config, see proto for details.
uint32_t mPollMs;