• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "trace_state_change_test.h"
17 #include "unified_collector.h"
18 #include "parameter_ex.h"
19 #include <unistd.h>
20 
21 using namespace testing::ext;
22 using namespace OHOS::HiviewDFX;
23 
24 namespace {
25 class MockHiviewPlatform : public HiviewContext {
26 public:
27     MockHiviewPlatform() = default;
28     ~MockHiviewPlatform() = default;
29 
GetHiViewDirectory(DirectoryType type __UNUSED)30     std::string GetHiViewDirectory(DirectoryType type __UNUSED)
31     {
32         return "/data/log/hiview";
33     }
34 
GetSharedWorkLoop()35     std::shared_ptr<EventLoop> GetSharedWorkLoop()
36     {
37         return nullptr;
38     }
39 };
40 
41 constexpr int8_t STATE_COUNT = 2;
42 constexpr bool DYNAMIC_TRACE_FSM[STATE_COUNT][STATE_COUNT][STATE_COUNT] = {
43     {{true,  false}, {false, false}},
44     {{false, false}, {false, false}},
45 };
46 constexpr bool CHECK_DYNAMIC_TRACE_FSM[STATE_COUNT][STATE_COUNT] = {
47     {true, true}, {false, true}
48 };
49 constexpr useconds_t SET_PROPERTY_MICRO_SECONDS_DELAY = 100 * 1000;
50 constexpr useconds_t STATE_CHANGE_CALLBACK_MICRO_SECONDS_DELAY = 500 * 1000;
51 std::shared_ptr g_unifiedCollector = std::make_shared<UnifiedCollector>();
52 MockHiviewPlatform g_hiviewContext = MockHiviewPlatform();
53 bool g_originalTestAppTraceOn, g_originalUCollectionSwitchOn, g_originalTraceCollectionSwitchOn;
54 } // namespace
55 
ConvertBoolToString(bool value)56 inline const std::string ConvertBoolToString(bool value)
57 {
58     return value ? "true" : "false";
59 }
60 
SetUpTestCase()61 void TraceStateChangeTest::SetUpTestCase()
62 {
63     g_unifiedCollector->SetHiviewContext(&g_hiviewContext);
64 }
65 
TearDownTestCase()66 void TraceStateChangeTest::TearDownTestCase()
67 {
68 }
69 
SetUp()70 void TraceStateChangeTest::SetUp()
71 {
72     ASSERT_NE(g_unifiedCollector, nullptr);
73     g_originalTestAppTraceOn = Parameter::IsTestAppTraceOn();
74     g_originalUCollectionSwitchOn = Parameter::IsUCollectionSwitchOn();
75     g_originalTraceCollectionSwitchOn = Parameter::IsTraceCollectionSwitchOn();
76 }
77 
TearDown()78 void TraceStateChangeTest::TearDown()
79 {
80     Parameter::SetProperty(HIVIEW_UCOLLECTION_TEST_APP_TRACE_STATE, ConvertBoolToString(g_originalTestAppTraceOn));
81     usleep(SET_PROPERTY_MICRO_SECONDS_DELAY);
82     Parameter::SetProperty(HIVIEW_UCOLLECTION_STATE, ConvertBoolToString(g_originalUCollectionSwitchOn));
83     usleep(SET_PROPERTY_MICRO_SECONDS_DELAY);
84     Parameter::SetProperty(DEVELOP_HIVIEW_TRACE_RECORDER, ConvertBoolToString(g_originalTraceCollectionSwitchOn));
85     usleep(SET_PROPERTY_MICRO_SECONDS_DELAY);
86 }
87 
88 /**
89  * @tc.name: TraceStateChangeTest001
90  * @tc.desc: Test UnifiedCollector state change
91  * @tc.type: FUNC
92  * @tc.require: issue#I9S4U8
93  */
94 HWTEST_F(TraceStateChangeTest, TraceStateChangeTest001, TestSize.Level3)
95 {
96     constexpr std::size_t stateConstantCount = 2;
97     constexpr uint64_t constantTestCaseNumber = 1<<stateConstantCount;
98     constexpr std::size_t stateVariableCount = 3;
99     constexpr uint64_t variableTestCaseNumber = 1<<stateVariableCount;
100     for (uint64_t constantBinary = 0; constantBinary < constantTestCaseNumber; constantBinary++)
101     {
102         bool isBetaVersion = (constantBinary & 1<<1) == 0; // due to coupling of watch parameter in unified collector
103         bool isDeveloperMode = (constantBinary & 1<<0) != 0;
104         bool isTestAppTraceOn = 0;
105         bool isUCollectionSwitchOn = 0;
106         bool isTraceCollectionSwitchOn = 0;
107 
108         Parameter::SetBetaVersion(isBetaVersion);
109         Parameter::SetDeveloperMode(isDeveloperMode);
110         Parameter::SetProperty(HIVIEW_UCOLLECTION_TEST_APP_TRACE_STATE, ConvertBoolToString(isTestAppTraceOn));
111         Parameter::SetProperty(HIVIEW_UCOLLECTION_STATE, ConvertBoolToString(isUCollectionSwitchOn));
112         Parameter::SetProperty(DEVELOP_HIVIEW_TRACE_RECORDER, ConvertBoolToString(isTraceCollectionSwitchOn));
113 
114         g_unifiedCollector->OnLoad();
115         for (uint64_t variableBinary = 0; variableBinary < variableTestCaseNumber; variableBinary++)
116         {
117             isTestAppTraceOn = (variableBinary & 1<<0) != 0;
118             isUCollectionSwitchOn = (variableBinary & 1<<1) != 0;
119             if (isDeveloperMode) {
120                 isTraceCollectionSwitchOn = (variableBinary & 1<<2) != 0;
121             }
122 
123             Parameter::SetProperty(HIVIEW_UCOLLECTION_TEST_APP_TRACE_STATE, ConvertBoolToString(isTestAppTraceOn));
124             usleep(SET_PROPERTY_MICRO_SECONDS_DELAY);
125             Parameter::SetProperty(HIVIEW_UCOLLECTION_STATE, ConvertBoolToString(isUCollectionSwitchOn));
126             usleep(SET_PROPERTY_MICRO_SECONDS_DELAY);
127             Parameter::SetProperty(DEVELOP_HIVIEW_TRACE_RECORDER, ConvertBoolToString(isTraceCollectionSwitchOn));
128 
129             usleep(STATE_CHANGE_CALLBACK_MICRO_SECONDS_DELAY);
130             bool targetTraceState = CHECK_DYNAMIC_TRACE_FSM[isDeveloperMode][isTestAppTraceOn] &&
131                 DYNAMIC_TRACE_FSM[isBetaVersion][isUCollectionSwitchOn][isTraceCollectionSwitchOn];
132             EXPECT_EQ(AppCallerEvent::enableDynamicTrace_, targetTraceState);
133         }
134         g_unifiedCollector->OnUnload();
135     }
136 }
137 
138 /**
139  * @tc.name: TraceStateChangeTest002
140  * @tc.desc: Test UnifiedCollector state initialization
141  * @tc.type: FUNC
142  * @tc.require: issue#I9S4U8
143  */
144 HWTEST_F(TraceStateChangeTest, TraceStateChangeTest002, TestSize.Level3)
145 {
146     constexpr std::size_t stateConstantCount = 5;
147     constexpr uint64_t testCaseNumber = 1<<stateConstantCount;
148     for (uint64_t binaryExpression = 0; binaryExpression < testCaseNumber; binaryExpression++)
149     {
150         bool isBetaVersion = (binaryExpression & 1<<0) != 0;
151         bool isDeveloperMode = (binaryExpression & 1<<1) != 0;
152         bool isTestAppTraceOn = (binaryExpression & 1<<2) != 0;
153         bool isUCollectionSwitchOn = (binaryExpression & 1<<3) != 0;
154         bool isTraceCollectionSwitchOn = (binaryExpression & 1<<4) != 0;
155 
156         Parameter::SetDeveloperMode(isDeveloperMode);
157         Parameter::SetBetaVersion(isBetaVersion);
158         Parameter::SetProperty(HIVIEW_UCOLLECTION_TEST_APP_TRACE_STATE, ConvertBoolToString(isTestAppTraceOn));
159         Parameter::SetProperty(HIVIEW_UCOLLECTION_STATE, ConvertBoolToString(isUCollectionSwitchOn));
160         Parameter::SetProperty(DEVELOP_HIVIEW_TRACE_RECORDER, ConvertBoolToString(isTraceCollectionSwitchOn));
161 
162         g_unifiedCollector->OnLoad();
163         bool targetTraceState = CHECK_DYNAMIC_TRACE_FSM[isDeveloperMode][isTestAppTraceOn] &&
164             DYNAMIC_TRACE_FSM[isBetaVersion][isUCollectionSwitchOn][isTraceCollectionSwitchOn];
165         EXPECT_EQ(AppCallerEvent::enableDynamicTrace_, targetTraceState);
166         g_unifiedCollector->OnUnload();
167     }
168 }