blob: 5bde599c16ec4938dad39db5f625ab5b07b47326 [file] [log] [blame]
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/developer/debug/debug_agent/filter.h"
#include <lib/syslog/cpp/macros.h>
#include <gtest/gtest.h>
#include "src/developer/debug/debug_agent/mock_system_interface.h"
#include "src/developer/debug/debug_agent/system_interface.h"
#include "src/developer/debug/ipc/records.h"
namespace debug_agent {
namespace {
TEST(Filter, MatchProcess) {
auto system_interface = MockSystemInterface::CreateWithData();
// j: 1 root
// j: 8 job1 /moniker fuchsia-pkg://devhost/package#meta/component.cm
// j: 17 job12
// j: 18 job121
// p: 19 job121-p1
auto process = system_interface->GetProcess(19);
// Process name.
debug_ipc::Filter filter{.type = debug_ipc::Filter::Type::kProcessName, .pattern = "job121-p1"};
EXPECT_TRUE(Filter(filter).MatchesProcess(*process, *system_interface));
filter = {.type = debug_ipc::Filter::Type::kProcessName, .pattern = "p1"};
EXPECT_FALSE(Filter(filter).MatchesProcess(*process, *system_interface));
filter = {.type = debug_ipc::Filter::Type::kProcessNameSubstr, .pattern = "p1"};
EXPECT_TRUE(Filter(filter).MatchesProcess(*process, *system_interface));
filter = {.type = debug_ipc::Filter::Type::kProcessNameSubstr, .pattern = "p2"};
EXPECT_FALSE(Filter(filter).MatchesProcess(*process, *system_interface));
// Job koid.
filter = {.type = debug_ipc::Filter::Type::kProcessNameSubstr, .pattern = "p1", .job_koid = 1};
EXPECT_TRUE(Filter(filter).MatchesProcess(*process, *system_interface));
filter = {.type = debug_ipc::Filter::Type::kProcessNameSubstr, .job_koid = 8};
EXPECT_TRUE(Filter(filter).MatchesProcess(*process, *system_interface));
filter = {.type = debug_ipc::Filter::Type::kProcessNameSubstr, .pattern = "p1", .job_koid = 2};
EXPECT_FALSE(Filter(filter).MatchesProcess(*process, *system_interface));
filter = {.type = debug_ipc::Filter::Type::kProcessName, .pattern = "p1", .job_koid = 1};
EXPECT_FALSE(Filter(filter).MatchesProcess(*process, *system_interface));
// Component info.
filter = {.type = debug_ipc::Filter::Type::kComponentName, .pattern = "component.cm"};
EXPECT_TRUE(Filter(filter).MatchesProcess(*process, *system_interface));
filter = {.type = debug_ipc::Filter::Type::kComponentMoniker, .pattern = "/moniker"};
EXPECT_TRUE(Filter(filter).MatchesProcess(*process, *system_interface));
filter = {.type = debug_ipc::Filter::Type::kComponentUrl,
.pattern = "fuchsia-pkg://devhost/package#meta/component.cm"};
EXPECT_TRUE(Filter(filter).MatchesProcess(*process, *system_interface));
// j: 1 root
// j: 25 /a/long/generated_to_here/fixed/moniker
// fuchsia-pkg://devhost/test_package#meta/component2.cm
// j: 26 job2-p1
process = system_interface->GetProcess(26);
filter = {.type = debug_ipc::Filter::Type::kComponentMonikerSuffix, .pattern = "fixed/moniker"};
EXPECT_TRUE(Filter(filter).MatchesProcess(*process, *system_interface));
}
TEST(Filter, ApplyToJob) {
auto system_interface = MockSystemInterface::CreateWithData();
auto root = system_interface->GetRootJob();
debug_ipc::Filter filter{.type = debug_ipc::Filter::Type::kProcessName, .pattern = "root-p1"};
EXPECT_EQ(1ull, Filter(filter).ApplyToJob(*root, *system_interface).size());
filter = {.type = debug_ipc::Filter::Type::kProcessNameSubstr, .pattern = "p1"};
EXPECT_EQ(5ull, Filter(filter).ApplyToJob(*root, *system_interface).size());
filter = {.type = debug_ipc::Filter::Type::kProcessNameSubstr, .pattern = "p1", .job_koid = 8};
EXPECT_EQ(3ull, Filter(filter).ApplyToJob(*root, *system_interface).size());
filter = {.type = debug_ipc::Filter::Type::kComponentName, .pattern = "component.cm"};
EXPECT_EQ(5ull, Filter(filter).ApplyToJob(*root, *system_interface).size());
}
} // namespace
} // namespace debug_agent