-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
809 additions
and
413 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2023 iLogtail Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "common/CapabilityUtil.h" | ||
#include "unittest/Unittest.h" | ||
|
||
namespace logtail { | ||
|
||
class CapabilityUtilUnittest : public ::testing::Test { | ||
public: | ||
void TestGetCapability(); | ||
void TestGetCapabilities(); | ||
void TestInvalidCapability(); | ||
}; | ||
|
||
void CapabilityUtilUnittest::TestGetCapability() { | ||
// Test some common capabilities | ||
APSARA_TEST_STREQ_DESC(GetCapability(0).c_str(), "CAP_CHOWN", "CAP_CHOWN should match"); | ||
APSARA_TEST_STREQ_DESC(GetCapability(1).c_str(), "DAC_OVERRIDE", "DAC_OVERRIDE should match"); | ||
APSARA_TEST_STREQ_DESC(GetCapability(2).c_str(), "CAP_DAC_READ_SEARCH", "CAP_DAC_READ_SEARCH should match"); | ||
APSARA_TEST_STREQ_DESC(GetCapability(7).c_str(), "CAP_SETUID", "CAP_SETUID should match"); | ||
APSARA_TEST_STREQ_DESC(GetCapability(21).c_str(), "CAP_SYS_ADMIN", "CAP_SYS_ADMIN should match"); | ||
} | ||
|
||
void CapabilityUtilUnittest::TestGetCapabilities() { | ||
// Test single capability | ||
uint64_t singleCap = 1ULL << 0; // CAP_CHOWN | ||
APSARA_TEST_STREQ_DESC(GetCapabilities(singleCap).c_str(), "CAP_CHOWN", "Single capability should match"); | ||
|
||
// Test multiple capabilities | ||
uint64_t multipleCaps = (1ULL << 0) | (1ULL << 1) | (1ULL << 7); // CAP_CHOWN, DAC_OVERRIDE, CAP_SETUID | ||
std::string result = GetCapabilities(multipleCaps); | ||
APSARA_TEST_TRUE(result.find("CAP_CHOWN") != std::string::npos); | ||
APSARA_TEST_TRUE(result.find("DAC_OVERRIDE") != std::string::npos); | ||
APSARA_TEST_TRUE(result.find("CAP_SETUID") != std::string::npos); | ||
|
||
// Test no capabilities | ||
APSARA_TEST_STREQ_DESC(GetCapabilities(0).c_str(), "", "No capabilities should return empty string"); | ||
|
||
// Test all capabilities | ||
uint64_t allCaps = ~0ULL; | ||
result = GetCapabilities(allCaps); | ||
APSARA_TEST_TRUE(result.find("CAP_CHOWN") != std::string::npos); | ||
APSARA_TEST_TRUE(result.find("CAP_SYS_ADMIN") != std::string::npos); | ||
APSARA_TEST_TRUE(result.find("CAP_CHECKPOINT_RESTORE") != std::string::npos); | ||
} | ||
|
||
void CapabilityUtilUnittest::TestInvalidCapability() { | ||
// Test invalid capability value | ||
try { | ||
GetCapability(-1); | ||
APSARA_TEST_TRUE(false); // Should not reach here | ||
} catch (const std::invalid_argument& e) { | ||
APSARA_TEST_TRUE(true); | ||
} | ||
|
||
try { | ||
GetCapability(100); | ||
APSARA_TEST_TRUE(false); // Should not reach here | ||
} catch (const std::invalid_argument& e) { | ||
APSARA_TEST_TRUE(true); | ||
} | ||
} | ||
|
||
UNIT_TEST_CASE(CapabilityUtilUnittest, TestGetCapability); | ||
UNIT_TEST_CASE(CapabilityUtilUnittest, TestGetCapabilities); | ||
UNIT_TEST_CASE(CapabilityUtilUnittest, TestInvalidCapability); | ||
|
||
} // namespace logtail | ||
|
||
UNIT_TEST_MAIN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2023 iLogtail Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "common/NetworkUtil.h" | ||
#include "unittest/Unittest.h" | ||
|
||
#if defined(__linux__) | ||
#include <arpa/inet.h> | ||
#endif | ||
|
||
namespace logtail { | ||
|
||
class NetworkUtilUnittest : public ::testing::Test { | ||
public: | ||
void TestGetAddrString(); | ||
void TestGetFamilyString(); | ||
void TestGetProtocolString(); | ||
void TestGetStateString(); | ||
}; | ||
|
||
void NetworkUtilUnittest::TestGetAddrString() { | ||
#if defined(__linux__) | ||
// Test IP address conversion | ||
// 127.0.0.1 in network byte order | ||
uint32_t localhost = 0x0100007F; | ||
APSARA_TEST_STREQ_DESC(GetAddrString(localhost).c_str(), "127.0.0.1", "Localhost IP should match"); | ||
|
||
// 192.168.1.1 in network byte order | ||
uint32_t localIP = 0x0101A8C0; | ||
APSARA_TEST_STREQ_DESC(GetAddrString(localIP).c_str(), "192.168.1.1", "Local IP should match"); | ||
|
||
// 8.8.8.8 in network byte order | ||
uint32_t googleDNS = 0x08080808; | ||
APSARA_TEST_STREQ_DESC(GetAddrString(googleDNS).c_str(), "8.8.8.8", "Google DNS IP should match"); | ||
#endif | ||
} | ||
|
||
void NetworkUtilUnittest::TestGetFamilyString() { | ||
#if defined(__linux__) | ||
// Test common address families | ||
APSARA_TEST_STREQ_DESC(GetFamilyString(AF_INET).c_str(), "AF_INET", "IPv4 family should match"); | ||
APSARA_TEST_STREQ_DESC(GetFamilyString(AF_INET6).c_str(), "AF_INET6", "IPv6 family should match"); | ||
APSARA_TEST_STREQ_DESC(GetFamilyString(AF_UNIX).c_str(), "AF_UNIX", "Unix domain socket family should match"); | ||
|
||
// Test unknown family | ||
APSARA_TEST_TRUE(GetFamilyString(9999).find_first_not_of("0123456789") == std::string::npos); | ||
#endif | ||
} | ||
|
||
void NetworkUtilUnittest::TestGetProtocolString() { | ||
// Test common protocols | ||
APSARA_TEST_STREQ_DESC(GetProtocolString(1).c_str(), "ICMP", "ICMP protocol should match"); | ||
APSARA_TEST_STREQ_DESC(GetProtocolString(6).c_str(), "TCP", "TCP protocol should match"); | ||
APSARA_TEST_STREQ_DESC(GetProtocolString(17).c_str(), "UDP", "UDP protocol should match"); | ||
APSARA_TEST_STREQ_DESC(GetProtocolString(89).c_str(), "OSPF", "OSPF protocol should match"); | ||
|
||
// Test unknown protocol | ||
APSARA_TEST_STREQ_DESC(GetProtocolString(999).c_str(), "Unknown", "Unknown protocol should return 'Unknown'"); | ||
} | ||
|
||
void NetworkUtilUnittest::TestGetStateString() { | ||
// Test TCP states | ||
APSARA_TEST_STREQ_DESC(GetStateString(1).c_str(), "TCP_ESTABLISHED", "TCP ESTABLISHED state should match"); | ||
APSARA_TEST_STREQ_DESC(GetStateString(2).c_str(), "TCP_SYN_SENT", "TCP SYN_SENT state should match"); | ||
APSARA_TEST_STREQ_DESC(GetStateString(10).c_str(), "TCP_LISTEN", "TCP LISTEN state should match"); | ||
|
||
// Test invalid state | ||
APSARA_TEST_STREQ_DESC(GetStateString(999).c_str(), "INVALID_STATE", "Invalid state should return 'INVALID_STATE'"); | ||
} | ||
|
||
UNIT_TEST_CASE(NetworkUtilUnittest, TestGetAddrString); | ||
UNIT_TEST_CASE(NetworkUtilUnittest, TestGetFamilyString); | ||
UNIT_TEST_CASE(NetworkUtilUnittest, TestGetProtocolString); | ||
UNIT_TEST_CASE(NetworkUtilUnittest, TestGetStateString); | ||
|
||
} // namespace logtail | ||
|
||
UNIT_TEST_MAIN |
Oops, something went wrong.