| 
 | 1 | +/*  | 
 | 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates.  | 
 | 3 | + * All rights reserved.  | 
 | 4 | + *  | 
 | 5 | + * This source code is licensed under the BSD-style license found in the  | 
 | 6 | + * LICENSE file in the root directory of this source tree.  | 
 | 7 | + */  | 
 | 8 | + | 
 | 9 | +#include <gtest/gtest.h>  | 
 | 10 | + | 
 | 11 | +#include <executorch/devtools/etdump/etdump_filter.h>  | 
 | 12 | +#include <executorch/runtime/platform/runtime.h>  | 
 | 13 | + | 
 | 14 | +#include <cstring>  | 
 | 15 | + | 
 | 16 | +using ::executorch::etdump::ETDumpFilter;  | 
 | 17 | +using ::executorch::runtime::Error;  | 
 | 18 | +using ::executorch::runtime::kUnsetDelegateDebugIntId;  | 
 | 19 | +using ::executorch::runtime::Result;  | 
 | 20 | + | 
 | 21 | +class ETDumpFilterTest : public ::testing::Test {  | 
 | 22 | + protected:  | 
 | 23 | +  ETDumpFilter filter;  | 
 | 24 | + | 
 | 25 | +  void SetUp() override {  | 
 | 26 | +    torch::executor::runtime_init();  | 
 | 27 | +  }  | 
 | 28 | + | 
 | 29 | +  void TearDown() override {}  | 
 | 30 | +};  | 
 | 31 | + | 
 | 32 | +TEST_F(ETDumpFilterTest, AddRegexPatternSuccess) {  | 
 | 33 | +  Result<bool> result = filter.add_regex("test.*");  | 
 | 34 | +  EXPECT_TRUE(result.ok());  | 
 | 35 | +  EXPECT_TRUE(result.get());  | 
 | 36 | +}  | 
 | 37 | + | 
 | 38 | +TEST_F(ETDumpFilterTest, SetDebugHandleRangeSuccess) {  | 
 | 39 | +  Result<bool> result = filter.set_debug_handle_range(10, 20);  | 
 | 40 | +  EXPECT_TRUE(result.ok());  | 
 | 41 | +  EXPECT_TRUE(result.get());  | 
 | 42 | +}  | 
 | 43 | + | 
 | 44 | +TEST_F(ETDumpFilterTest, SetDebugHandleRangeFailure) {  | 
 | 45 | +  Result<bool> result = filter.set_debug_handle_range(20, 10);  | 
 | 46 | +  EXPECT_EQ(result.error(), Error::InvalidArgument);  | 
 | 47 | +}  | 
 | 48 | + | 
 | 49 | +TEST_F(ETDumpFilterTest, FilterByNameSuccess) {  | 
 | 50 | +  filter.add_regex("event.*");  | 
 | 51 | +  Result<bool> result = filter.filter("event_name", kUnsetDelegateDebugIntId);  | 
 | 52 | +  EXPECT_TRUE(result.ok());  | 
 | 53 | +  EXPECT_TRUE(result.get());  | 
 | 54 | +}  | 
 | 55 | + | 
 | 56 | +TEST_F(ETDumpFilterTest, PartialMatchingFailed) {  | 
 | 57 | +  filter.add_regex("event.*");  | 
 | 58 | +  Result<bool> result =  | 
 | 59 | +      filter.filter("non_matching_event", kUnsetDelegateDebugIntId);  | 
 | 60 | +  EXPECT_TRUE(result.ok());  | 
 | 61 | +  EXPECT_FALSE(result.get());  | 
 | 62 | +}  | 
 | 63 | + | 
 | 64 | +TEST_F(ETDumpFilterTest, FilterByDelegateDebugIndexSuccess) {  | 
 | 65 | +  filter.set_debug_handle_range(10, 20);  | 
 | 66 | +  Result<bool> result = filter.filter(nullptr, 15);  | 
 | 67 | +  EXPECT_TRUE(result.ok());  | 
 | 68 | +  EXPECT_TRUE(result.get());  | 
 | 69 | +}  | 
 | 70 | + | 
 | 71 | +TEST_F(ETDumpFilterTest, FilterByDelegateDebugIndexFailure) {  | 
 | 72 | +  filter.set_debug_handle_range(10, 20);  | 
 | 73 | +  Result<bool> result = filter.filter(nullptr, 25);  | 
 | 74 | +  EXPECT_TRUE(result.ok());  | 
 | 75 | +  EXPECT_FALSE(result.get());  | 
 | 76 | +}  | 
 | 77 | + | 
 | 78 | +TEST_F(ETDumpFilterTest, NaiveFilterNameInputCanSucceed) {  | 
 | 79 | +  Result<bool> result = filter.filter("any_input", kUnsetDelegateDebugIntId);  | 
 | 80 | +  EXPECT_TRUE(result.ok());  | 
 | 81 | +  EXPECT_TRUE(result.get());  | 
 | 82 | +}  | 
 | 83 | + | 
 | 84 | +TEST_F(ETDumpFilterTest, NaiveFilterDebugHandleInputCanSucceed) {  | 
 | 85 | +  Result<bool> result = filter.filter(nullptr, 12345);  | 
 | 86 | +  EXPECT_TRUE(result.ok());  | 
 | 87 | +  EXPECT_TRUE(result.get());  | 
 | 88 | +}  | 
 | 89 | + | 
 | 90 | +TEST_F(ETDumpFilterTest, IllegalInput) {  | 
 | 91 | +  filter.add_regex("pattern");  | 
 | 92 | +  Result<bool> result = filter.filter("matching_event", 1);  | 
 | 93 | +  EXPECT_EQ(result.error(), Error::InvalidArgument);  | 
 | 94 | +}  | 
 | 95 | + | 
 | 96 | +TEST_F(ETDumpFilterTest, NoMatchFirstThenMatch) {  | 
 | 97 | +  filter.add_regex("non_matching_pattern");  | 
 | 98 | +  Result<bool> result_1 =  | 
 | 99 | +      filter.filter("matching_event", kUnsetDelegateDebugIntId);  | 
 | 100 | +  EXPECT_TRUE(result_1.ok());  | 
 | 101 | +  EXPECT_FALSE(result_1.get());  | 
 | 102 | +  filter.add_regex("matching_.*");  | 
 | 103 | +  Result<bool> result_2 =  | 
 | 104 | +      filter.filter("matching_event", kUnsetDelegateDebugIntId);  | 
 | 105 | +  EXPECT_TRUE(result_2.ok());  | 
 | 106 | +  EXPECT_TRUE(result_2.get());  | 
 | 107 | +}  | 
 | 108 | + | 
 | 109 | +TEST_F(ETDumpFilterTest, MatchRegexFirstThen) {  | 
 | 110 | +  filter.add_regex("matching.*");  | 
 | 111 | +  Result<bool> result_1 =  | 
 | 112 | +      filter.filter("matching_event", kUnsetDelegateDebugIntId);  | 
 | 113 | +  EXPECT_TRUE(result_1.ok());  | 
 | 114 | +  EXPECT_TRUE(result_1.get());  | 
 | 115 | +  filter.add_regex("non_matching_pattern");  | 
 | 116 | +  Result<bool> result_2 =  | 
 | 117 | +      filter.filter("matching_event", kUnsetDelegateDebugIntId);  | 
 | 118 | +  EXPECT_TRUE(result_2.ok());  | 
 | 119 | +  EXPECT_TRUE(result_2.get());  | 
 | 120 | +}  | 
0 commit comments