Files
CPP_Core/Core/Base/Check.cpp
T
2026-06-25 15:44:25 +08:00

169 lines
5.2 KiB
C++

#include "Check.h"
#include <algorithm>
#include <iostream>
#include <sstream>
bool check_port(long long port) { return port >= 0 && port <= 65535; }
bool check_ipv4(const std::string &ip) {
std::istringstream ss(ip);
std::string segment;
int count = 0;
// 检查是否为空字符串或以点号结尾
if (ip.empty() || ip.back() == '.') {
return false;
}
while (std::getline(ss, segment, '.')) {
++count;
// 检查是否为数字且在 0-255 范围内
if (segment.empty() || segment.size() > 3 ||
!std::all_of(segment.begin(), segment.end(), ::isdigit) ||
std::stoi(segment) < 0 || std::stoi(segment) > 255) {
return false;
}
// 防止前导零,例如 "01"
if (segment.size() > 1 && segment[0] == '0') {
return false;
}
}
// IPv4 应该有 4 个段
return count == 4;
}
// bool check_ipv4(const std::string& ip) {
// std::istringstream ss(ip);
// std::string segment;
// int count = 0;
//
// while (std::getline(ss, segment, '.')) {
// ++count;
// // 检查是否为数字且在 0-255 范围内
// if (segment.empty() || segment.size() > 3 ||
// !std::all_of(segment.begin(), segment.end(), ::isdigit) ||
// std::stoi(segment) < 0 || std::stoi(segment) > 255) {
// return false;
// }
// // 防止前导零,例如 "01"
// if (segment.size() > 1 && segment[0] == '0') {
// return false;
// }
// }
// return count == 4; // IPv4 应该有 4 个段
// }
unsigned int ip_to_int(const std::string &ip) {
unsigned int result = 0;
std::stringstream ss(ip);
std::string byte;
for (int i = 0; i < 4; ++i) {
std::getline(ss, byte, '.');
result |= (std::stoi(byte) << (24 - i * 8));
}
return result;
}
// 判断 IP 地址是否在子网内
bool is_in_subnet(const std::string &ip, const std::string &netmask,
const std::string &gateway) {
unsigned int ipInt = ip_to_int(ip);
unsigned int netmaskInt = ip_to_int(netmask);
unsigned int gatewayInt = ip_to_int(gateway);
// 计算网络地址
unsigned int networkAddress = ipInt & netmaskInt;
return (networkAddress == (gatewayInt & netmaskInt));
}
// check 函数,验证 IP、子网掩码和网关
bool check_ip_netmask_gateway(const std::string &ip, const std::string &netmask,
const std::string &gateway) {
// 验证 IP 地址、子网掩码和网关是否合法
if (!check_ipv4(ip)) {
std::cerr << "Invalid IP address: " << ip << std::endl;
return false;
}
if (!check_ipv4(netmask)) {
std::cerr << "Invalid netmask: " << netmask << std::endl;
return false;
}
if (!check_ipv4(gateway)) {
std::cerr << "Invalid gateway: " << gateway << std::endl;
return false;
}
// 验证网关是否在同一个子网内
if (!is_in_subnet(ip, netmask, gateway)) {
std::cerr << "Gateway is not in the same subnet as IP address."
<< std::endl;
return false;
}
return true;
}
#ifdef _USE_GTEST
#include <gtest/gtest.h>
// 测试 IPv4 地址的有效性
TEST(CheckIPv4Test, ValidIPv4Addresses) {
// 合法 IPv4 测试用例
std::vector<std::string> validIPs = {
"192.168.0.1", // 合法
"255.255.255.255", // 合法
"0.0.0.0", // 合法
"127.0.0.1" // 合法
};
for (const auto &ip : validIPs) {
EXPECT_TRUE(check_ipv4(ip)) << "Failed for valid IP: " << ip;
}
}
TEST(CheckIPv4Test, InvalidIPv4Addresses) {
// 不合法的 IPv4 测试用例
std::vector<std::string> invalidIPs = {
"192.168.1", // 不合法,段数不足
"256.256.256.256", // 不合法,数值超出范围
"192.168.01.1", // 不合法,前导零
"192.168..1", // 不合法,空段
"abc.def.ghi.jkl", // 不合法,非数字
"192.168.1.1.", // 不合法,多余的点
".192.168.1.1", // 不合法,多余的点
"192.168.1.1.1", // 不合法,段数超出
"127.0.0.1112" // 不合法,段数超出
};
for (const auto &ip : invalidIPs) {
EXPECT_FALSE(check_ipv4(ip)) << "Failed for invalid IP: " << ip;
}
}
// TEST(Check, ipv4) {
// // ::testing::InitGoogleTest(&argc, argv);
// // return RUN_ALL_TESTS();
// std::vector<std::string> testCases = {
// "192.168.0.1", // 合法 IPv4
// "255.255.255.255", // 合法 IPv4
// "0.0.0.0", // 合法 IPv4
// "127.0.0.1", // 合法 IPv4
// "192.168.1", // 不合法,段数不足
// "256.256.256.256", // 不合法,数值超出范围
// "192.168.01.1", // 不合法,前导零
// "192.168..1", // 不合法,空段
// "abc.def.ghi.jkl", // 不合法,非数字
// "192.168.1.1.", // 不合法,多余的点
// ".192.168.1.1", // 不合法,多余的点
// "192.168.1.1.1" // 不合法,段数超出
// "127.0.0.1112" // 不合法,段数超出
// };
//
// // 测试函数
// for (const auto& testCase : testCases) {
// bool result = check_ipv4(testCase);
// std::cout << "Testing: " << testCase
// << " -> " << (result ? "Valid" : "Invalid") << std::endl;
// }
// }
#endif