1 #include <attn/attention.hpp>
2 #include <attn/attn_common.hpp>
3 #include <attn/attn_config.hpp>
4 #include <attn/attn_handler.hpp>
5 #include <util/pdbg.hpp>
6 #include <util/trace.hpp>
7 
8 #include "gtest/gtest.h"
9 
10 namespace attn
11 {
12 // these are in the attn_lib but not all exposed via headers
13 int handleSpecial(Attention* i_attention);
14 } // namespace attn
15 
16 using namespace attn;
17 using namespace util::pdbg;
18 
19 /** @brief global function to be called back. */
20 int handleAttention(Attention* attention)
21 {
22     int rc = RC_SUCCESS;
23     if (attention != nullptr)
24     {
25         return rc;
26     }
27     else
28     {
29         return RC_NOT_HANDLED;
30     }
31 }
32 
33 // Global variables for UT #1 and UT#2.
34 // Attention type
35 Attention::AttentionType gType = Attention::AttentionType::Special;
36 // pointer to handler callback function
37 int (*gHandler)(Attention*)   = &(handleSpecial);
38 const AttentionFlag gAttnFlag = AttentionFlag::enBreakpoints;
39 
40 // Start preparation for UT case #1.
41 
42 // Global variables for UT #1
43 const char* gPosPath = "/proc0/pib/perv12";
44 const uint32_t gPos  = 12;
45 
46 /** @brief Fixture class for TEST_F(). */
47 class AttentionTestPos : public testing::Test
48 {
49   public:
50     AttentionTestPos() {}
51 
52     void SetUp()
53     {
54         pdbg_targets_init(nullptr);
55 
56         target = pdbg_target_from_path(nullptr, gPosPath);
57         EXPECT_NE(nullptr, target);
58 
59         config = new Config;
60         EXPECT_EQ(true, config->getFlag(gAttnFlag));
61 
62         pAttn = std::make_unique<Attention>(
63             Attention(gType, gHandler, target, config));
64     }
65 
66     void TearDown()
67     {
68         delete config;
69     }
70 
71     std::unique_ptr<Attention> pAttn;
72     Config* config      = nullptr;
73     pdbg_target* target = nullptr;
74 };
75 
76 TEST_F(AttentionTestPos, TestAttnTargetPos)
77 {
78     EXPECT_EQ(0, pAttn->getPriority());
79     EXPECT_EQ(RC_SUCCESS, pAttn->handle());
80 
81     // Verify the global target_tmp.
82     EXPECT_NE(nullptr, target);
83     uint32_t attr = std::numeric_limits<uint32_t>::max();
84     pdbg_target_get_attribute(target, "ATTR_FAPI_POS", 4, 1, &attr);
85     EXPECT_EQ(gPos, attr);
86 
87     // Verify the target in Attention object.
88     attr                    = std::numeric_limits<uint32_t>::max();
89     pdbg_target* target_tmp = pAttn->getTarget();
90     EXPECT_NE(nullptr, target_tmp);
91     pdbg_target_get_attribute(target_tmp, "ATTR_FAPI_POS", 4, 1, &attr);
92     EXPECT_EQ(gPos, attr);
93 
94     // Verify the config in Attention object.
95     Config* config_tmp = pAttn->getConfig();
96     EXPECT_EQ(true, config_tmp->getFlag(gAttnFlag));
97     config_tmp->clearFlag(gAttnFlag);
98     EXPECT_EQ(false, config_tmp->getFlag(gAttnFlag));
99     config_tmp->setFlag(gAttnFlag);
100     EXPECT_EQ(true, config_tmp->getFlag(gAttnFlag));
101 }
102 
103 // Start preparation for UT case #2.
104 
105 // Global variables for UT #2
106 const uint32_t gChipId = 0x20da; // Chip ID for proc0.
107 
108 /** @brief Fixture class for TEST_F(). */
109 class AttentionTestProc : public testing::Test
110 {
111   public:
112     AttentionTestProc() {}
113 
114     void SetUp()
115     {
116         pdbg_targets_init(nullptr);
117         target = getPrimaryProcessor();
118 
119         EXPECT_NE(nullptr, target);
120 
121         attr = getTrgtType(target);
122         EXPECT_EQ(TYPE_PROC, attr);
123 
124         attr = std::numeric_limits<uint32_t>::max();
125         pdbg_target_get_attribute(target, "ATTR_CHIP_ID", 4, 1, &attr);
126         EXPECT_EQ(attr, gChipId);
127 
128         config = new Config;
129         EXPECT_EQ(true, config->getFlag(gAttnFlag));
130 
131         pAttn = std::make_unique<Attention>(
132             Attention(gType, gHandler, target, config));
133     }
134 
135     void TearDown()
136     {
137         delete config;
138     }
139 
140     std::unique_ptr<Attention> pAttn;
141     Config* config      = nullptr;
142     pdbg_target* target = nullptr;
143     uint32_t attr       = std::numeric_limits<uint32_t>::max();
144 };
145 
146 TEST_F(AttentionTestProc, TestAttentionProc)
147 {
148     EXPECT_EQ(0, pAttn->getPriority());
149     EXPECT_EQ(RC_SUCCESS, pAttn->handle());
150 
151     // Verify the target in Attention object.
152     attr                    = std::numeric_limits<uint32_t>::max();
153     pdbg_target* target_tmp = pAttn->getTarget();
154     EXPECT_NE(nullptr, target_tmp);
155     attr = getTrgtType(target_tmp);
156     EXPECT_EQ(TYPE_PROC, attr);
157 
158     attr = std::numeric_limits<uint32_t>::max();
159     pdbg_target_get_attribute(target_tmp, "ATTR_CHIP_ID", 4, 1, &attr);
160     EXPECT_EQ(attr, gChipId);
161 
162     // Verify the config in Attention object.
163     Config* config_tmp = pAttn->getConfig();
164     EXPECT_EQ(true, config_tmp->getFlag(gAttnFlag));
165     config_tmp->clearFlag(gAttnFlag);
166     EXPECT_EQ(false, config_tmp->getFlag(gAttnFlag));
167     config_tmp->setFlag(gAttnFlag);
168     EXPECT_EQ(true, config_tmp->getFlag(gAttnFlag));
169 }
170 
171 TEST(AttnConfig, TestAttnConfig)
172 {
173     Config* config = new Config();
174 
175     // Test clearFlagAll() function.
176     config->clearFlagAll();
177     EXPECT_EQ(false, config->getFlag(AttentionFlag::enVital));
178     EXPECT_EQ(false, config->getFlag(AttentionFlag::enCheckstop));
179     EXPECT_EQ(false, config->getFlag(AttentionFlag::enTerminate));
180     EXPECT_EQ(false, config->getFlag(AttentionFlag::enBreakpoints));
181     // The dfltTi flag is not impacted.
182     EXPECT_EQ(false, config->getFlag(AttentionFlag::dfltTi));
183     EXPECT_EQ(false, config->getFlag(AttentionFlag::enClrAttnIntr));
184 
185     // Test setFlagAll() function.
186     config->setFlagAll();
187     EXPECT_EQ(true, config->getFlag(AttentionFlag::enVital));
188     EXPECT_EQ(true, config->getFlag(AttentionFlag::enCheckstop));
189     EXPECT_EQ(true, config->getFlag(AttentionFlag::enTerminate));
190     EXPECT_EQ(true, config->getFlag(AttentionFlag::enBreakpoints));
191     // The dfltTi flag is not impacted.
192     EXPECT_EQ(false, config->getFlag(AttentionFlag::dfltTi));
193     EXPECT_EQ(true, config->getFlag(AttentionFlag::enClrAttnIntr));
194 
195     // Test setFlag() and getFlag() functions.
196     // Only test one flag.
197     config->clearFlagAll();
198     config->setFlag(enVital);
199     EXPECT_EQ(true, config->getFlag(enVital));
200 
201     delete config;
202 }