|
19 | 19 |
|
20 | 20 | #include "catch.hpp" |
21 | 21 | #include "Session.h" |
| 22 | +#include "TsBlock.h" |
| 23 | +#include <sstream> |
22 | 24 |
|
23 | 25 | using namespace std; |
24 | 26 |
|
@@ -728,3 +730,68 @@ TEST_CASE("Test executeLastDataQuery ", "[testExecuteLastDataQuery]") { |
728 | 730 | sessionDataSet->setFetchSize(1024); |
729 | 731 | REQUIRE(sessionDataSet->hasNext() == false); |
730 | 732 | } |
| 733 | + |
| 734 | +// Helper function for comparing TEndPoint with detailed error message |
| 735 | +void assertTEndPointEqual(const TEndPoint& actual, |
| 736 | + const std::string& expectedIp, |
| 737 | + int expectedPort, |
| 738 | + const char* file, |
| 739 | + int line) { |
| 740 | + if (actual.ip != expectedIp || actual.port != expectedPort) { |
| 741 | + std::stringstream ss; |
| 742 | + ss << "\nTEndPoint mismatch:\nExpected: " << expectedIp << ":" << expectedPort |
| 743 | + << "\nActual: " << actual.ip << ":" << actual.port; |
| 744 | + Catch::SourceLineInfo location(file, line); |
| 745 | + Catch::AssertionHandler handler("TEndPoint comparison", location, ss.str(), Catch::ResultDisposition::Normal); |
| 746 | + handler.handleMessage(Catch::ResultWas::ExplicitFailure, ss.str()); |
| 747 | + handler.complete(); |
| 748 | + } |
| 749 | +} |
| 750 | + |
| 751 | +// Macro to simplify test assertions |
| 752 | +#define REQUIRE_TENDPOINT(actual, expectedIp, expectedPort) \ |
| 753 | + assertTEndPointEqual(actual, expectedIp, expectedPort, __FILE__, __LINE__) |
| 754 | + |
| 755 | +TEST_CASE("UrlUtils - parseTEndPointIpv4AndIpv6Url", "[UrlUtils]") { |
| 756 | + // Test valid IPv4 addresses |
| 757 | + SECTION("Valid IPv4") { |
| 758 | + REQUIRE_TENDPOINT(UrlUtils::parseTEndPointIpv4AndIpv6Url("192.168.1.1:8080"), "192.168.1.1", 8080); |
| 759 | + REQUIRE_TENDPOINT(UrlUtils::parseTEndPointIpv4AndIpv6Url("10.0.0.1:80"), "10.0.0.1", 80); |
| 760 | + } |
| 761 | + |
| 762 | + // Test valid IPv6 addresses |
| 763 | + SECTION("Valid IPv6") { |
| 764 | + REQUIRE_TENDPOINT(UrlUtils::parseTEndPointIpv4AndIpv6Url("[2001:db8::1]:8080"), "2001:db8::1", 8080); |
| 765 | + REQUIRE_TENDPOINT(UrlUtils::parseTEndPointIpv4AndIpv6Url("[::1]:80"), "::1", 80); |
| 766 | + } |
| 767 | + |
| 768 | + // Test hostnames |
| 769 | + SECTION("Hostnames") { |
| 770 | + REQUIRE_TENDPOINT(UrlUtils::parseTEndPointIpv4AndIpv6Url("localhost:8080"), "localhost", 8080); |
| 771 | + REQUIRE_TENDPOINT(UrlUtils::parseTEndPointIpv4AndIpv6Url("example.com:443"), "example.com", 443); |
| 772 | + } |
| 773 | + |
| 774 | + // Test edge cases |
| 775 | + SECTION("Edge cases") { |
| 776 | + REQUIRE_TENDPOINT(UrlUtils::parseTEndPointIpv4AndIpv6Url(""), "", 0); |
| 777 | + REQUIRE_TENDPOINT(UrlUtils::parseTEndPointIpv4AndIpv6Url("127.0.0.1"), "127.0.0.1", 0); |
| 778 | + } |
| 779 | + |
| 780 | + // Test invalid inputs |
| 781 | + SECTION("Invalid inputs") { |
| 782 | + REQUIRE_TENDPOINT(UrlUtils::parseTEndPointIpv4AndIpv6Url("192.168.1.1:abc"), "192.168.1.1:abc", 0); |
| 783 | + REQUIRE_TENDPOINT(UrlUtils::parseTEndPointIpv4AndIpv6Url("]invalid[:80"), "]invalid[", 80); |
| 784 | + } |
| 785 | + |
| 786 | + // Test port ranges |
| 787 | + SECTION("Port ranges") { |
| 788 | + REQUIRE_TENDPOINT(UrlUtils::parseTEndPointIpv4AndIpv6Url("localhost:0"), "localhost", 0); |
| 789 | + REQUIRE_TENDPOINT(UrlUtils::parseTEndPointIpv4AndIpv6Url("127.0.0.1:65535"), "127.0.0.1", 65535); |
| 790 | + } |
| 791 | +} |
| 792 | + |
| 793 | +TEST_CASE("TsBlock deserialize rejects truncated malicious payload", "[TsBlockDeserialize]") { |
| 794 | + std::string data(18, '\0'); |
| 795 | + data[3] = '\x10'; |
| 796 | + REQUIRE_THROWS_AS(TsBlock::deserialize(data), IoTDBException); |
| 797 | +} |
0 commit comments