11cb0ef41Sopenharmony_ci#include "node_sockaddr-inl.h" 21cb0ef41Sopenharmony_ci#include "gtest/gtest.h" 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciusing node::SocketAddress; 51cb0ef41Sopenharmony_ciusing node::SocketAddressBlockList; 61cb0ef41Sopenharmony_ciusing node::SocketAddressLRU; 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciTEST(SocketAddress, SocketAddress) { 91cb0ef41Sopenharmony_ci CHECK(SocketAddress::is_numeric_host("123.123.123.123")); 101cb0ef41Sopenharmony_ci CHECK(!SocketAddress::is_numeric_host("localhost")); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci sockaddr_storage storage; 131cb0ef41Sopenharmony_ci sockaddr_storage storage2; 141cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET, "123.123.123.123", 443, &storage); 151cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET, "1.1.1.1", 80, &storage2); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci SocketAddress addr(reinterpret_cast<const sockaddr*>(&storage)); 181cb0ef41Sopenharmony_ci SocketAddress addr2(reinterpret_cast<const sockaddr*>(&storage2)); 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci CHECK_EQ(addr.length(), sizeof(sockaddr_in)); 211cb0ef41Sopenharmony_ci CHECK_EQ(addr.family(), AF_INET); 221cb0ef41Sopenharmony_ci CHECK_EQ(addr.address(), "123.123.123.123"); 231cb0ef41Sopenharmony_ci CHECK_EQ(addr.port(), 443); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci addr.set_flow_label(12345); 261cb0ef41Sopenharmony_ci CHECK_EQ(addr.flow_label(), 0); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci CHECK_NE(addr, addr2); 291cb0ef41Sopenharmony_ci CHECK_EQ(addr, addr); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci CHECK_EQ(SocketAddress::Hash()(addr), SocketAddress::Hash()(addr)); 321cb0ef41Sopenharmony_ci CHECK_NE(SocketAddress::Hash()(addr), SocketAddress::Hash()(addr2)); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci addr.Update(reinterpret_cast<uint8_t*>(&storage2), sizeof(sockaddr_in)); 351cb0ef41Sopenharmony_ci CHECK_EQ(addr.length(), sizeof(sockaddr_in)); 361cb0ef41Sopenharmony_ci CHECK_EQ(addr.family(), AF_INET); 371cb0ef41Sopenharmony_ci CHECK_EQ(addr.address(), "1.1.1.1"); 381cb0ef41Sopenharmony_ci CHECK_EQ(addr.port(), 80); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci SocketAddress::Map<size_t> map; 411cb0ef41Sopenharmony_ci map[addr]++; 421cb0ef41Sopenharmony_ci map[addr]++; 431cb0ef41Sopenharmony_ci CHECK_EQ(map[addr], 2); 441cb0ef41Sopenharmony_ci} 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ciTEST(SocketAddress, SocketAddressIPv6) { 471cb0ef41Sopenharmony_ci sockaddr_storage storage; 481cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET6, "::1", 443, &storage); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci SocketAddress addr(reinterpret_cast<const sockaddr*>(&storage)); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci CHECK_EQ(addr.length(), sizeof(sockaddr_in6)); 531cb0ef41Sopenharmony_ci CHECK_EQ(addr.family(), AF_INET6); 541cb0ef41Sopenharmony_ci CHECK_EQ(addr.address(), "::1"); 551cb0ef41Sopenharmony_ci CHECK_EQ(addr.port(), 443); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci addr.set_flow_label(12345); 581cb0ef41Sopenharmony_ci CHECK_EQ(addr.flow_label(), 12345); 591cb0ef41Sopenharmony_ci} 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ciTEST(SocketAddressLRU, SocketAddressLRU) { 621cb0ef41Sopenharmony_ci struct Foo { 631cb0ef41Sopenharmony_ci int c; 641cb0ef41Sopenharmony_ci bool expired; 651cb0ef41Sopenharmony_ci }; 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci struct FooLRUTraits { 681cb0ef41Sopenharmony_ci using Type = Foo; 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci static bool CheckExpired(const SocketAddress& address, const Type& type) { 711cb0ef41Sopenharmony_ci return type.expired; 721cb0ef41Sopenharmony_ci } 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci static void Touch(const SocketAddress& address, Type* type) { 751cb0ef41Sopenharmony_ci type->expired = false; 761cb0ef41Sopenharmony_ci } 771cb0ef41Sopenharmony_ci }; 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci SocketAddressLRU<FooLRUTraits> lru(2); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci sockaddr_storage storage[4]; 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET, "123.123.123.123", 443, &storage[0]); 841cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET, "123.123.123.124", 443, &storage[1]); 851cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET, "123.123.123.125", 443, &storage[2]); 861cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET, "123.123.123.123", 443, &storage[3]); 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci SocketAddress addr1(reinterpret_cast<const sockaddr*>(&storage[0])); 901cb0ef41Sopenharmony_ci SocketAddress addr2(reinterpret_cast<const sockaddr*>(&storage[1])); 911cb0ef41Sopenharmony_ci SocketAddress addr3(reinterpret_cast<const sockaddr*>(&storage[2])); 921cb0ef41Sopenharmony_ci SocketAddress addr4(reinterpret_cast<const sockaddr*>(&storage[3])); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci Foo* foo = lru.Upsert(addr1); 951cb0ef41Sopenharmony_ci CHECK_NOT_NULL(foo); 961cb0ef41Sopenharmony_ci CHECK_EQ(foo->c, 0); 971cb0ef41Sopenharmony_ci CHECK_EQ(foo->expired, false); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci foo->c = 1; 1001cb0ef41Sopenharmony_ci foo->expired = true; 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci foo = lru.Upsert(addr1); 1031cb0ef41Sopenharmony_ci CHECK_NOT_NULL(lru.Peek(addr1)); 1041cb0ef41Sopenharmony_ci CHECK_EQ(lru.Peek(addr1), lru.Peek(addr4)); 1051cb0ef41Sopenharmony_ci CHECK_EQ(lru.Peek(addr1)->c, 1); 1061cb0ef41Sopenharmony_ci CHECK_EQ(lru.Peek(addr1)->expired, false); 1071cb0ef41Sopenharmony_ci CHECK_EQ(lru.size(), 1); 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci foo = lru.Upsert(addr2); 1101cb0ef41Sopenharmony_ci foo->c = 2; 1111cb0ef41Sopenharmony_ci foo->expired = true; 1121cb0ef41Sopenharmony_ci CHECK_NOT_NULL(lru.Peek(addr2)); 1131cb0ef41Sopenharmony_ci CHECK_EQ(lru.Peek(addr2)->c, 2); 1141cb0ef41Sopenharmony_ci CHECK_EQ(lru.size(), 2); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci foo->expired = true; 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci foo = lru.Upsert(addr3); 1191cb0ef41Sopenharmony_ci foo->c = 3; 1201cb0ef41Sopenharmony_ci foo->expired = false; 1211cb0ef41Sopenharmony_ci CHECK_NOT_NULL(lru.Peek(addr3)); 1221cb0ef41Sopenharmony_ci CHECK_EQ(lru.Peek(addr3)->c, 3); 1231cb0ef41Sopenharmony_ci CHECK_EQ(lru.size(), 1); 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci // addr1 was removed because we exceeded size. 1261cb0ef41Sopenharmony_ci // addr2 was removed because it was expired. 1271cb0ef41Sopenharmony_ci CHECK_NULL(lru.Peek(addr1)); 1281cb0ef41Sopenharmony_ci CHECK_NULL(lru.Peek(addr2)); 1291cb0ef41Sopenharmony_ci} 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ciTEST(SocketAddress, Comparison) { 1321cb0ef41Sopenharmony_ci sockaddr_storage storage[6]; 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET, "10.0.0.1", 0, &storage[0]); 1351cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET, "10.0.0.2", 0, &storage[1]); 1361cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET6, "::1", 0, &storage[2]); 1371cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET6, "::2", 0, &storage[3]); 1381cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET6, "::ffff:10.0.0.1", 0, &storage[4]); 1391cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET6, "::ffff:10.0.0.2", 0, &storage[5]); 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci SocketAddress addr1(reinterpret_cast<const sockaddr*>(&storage[0])); 1421cb0ef41Sopenharmony_ci SocketAddress addr2(reinterpret_cast<const sockaddr*>(&storage[1])); 1431cb0ef41Sopenharmony_ci SocketAddress addr3(reinterpret_cast<const sockaddr*>(&storage[2])); 1441cb0ef41Sopenharmony_ci SocketAddress addr4(reinterpret_cast<const sockaddr*>(&storage[3])); 1451cb0ef41Sopenharmony_ci SocketAddress addr5(reinterpret_cast<const sockaddr*>(&storage[4])); 1461cb0ef41Sopenharmony_ci SocketAddress addr6(reinterpret_cast<const sockaddr*>(&storage[5])); 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ci CHECK_EQ(addr1.compare(addr1), SocketAddress::CompareResult::SAME); 1491cb0ef41Sopenharmony_ci CHECK_EQ(addr1.compare(addr2), SocketAddress::CompareResult::LESS_THAN); 1501cb0ef41Sopenharmony_ci CHECK_EQ(addr2.compare(addr1), SocketAddress::CompareResult::GREATER_THAN); 1511cb0ef41Sopenharmony_ci CHECK(addr1 <= addr1); 1521cb0ef41Sopenharmony_ci CHECK(addr1 < addr2); 1531cb0ef41Sopenharmony_ci CHECK(addr1 <= addr2); 1541cb0ef41Sopenharmony_ci CHECK(addr2 >= addr2); 1551cb0ef41Sopenharmony_ci CHECK(addr2 > addr1); 1561cb0ef41Sopenharmony_ci CHECK(addr2 >= addr1); 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci CHECK_EQ(addr3.compare(addr3), SocketAddress::CompareResult::SAME); 1591cb0ef41Sopenharmony_ci CHECK_EQ(addr3.compare(addr4), SocketAddress::CompareResult::LESS_THAN); 1601cb0ef41Sopenharmony_ci CHECK_EQ(addr4.compare(addr3), SocketAddress::CompareResult::GREATER_THAN); 1611cb0ef41Sopenharmony_ci CHECK(addr3 <= addr3); 1621cb0ef41Sopenharmony_ci CHECK(addr3 < addr4); 1631cb0ef41Sopenharmony_ci CHECK(addr3 <= addr4); 1641cb0ef41Sopenharmony_ci CHECK(addr4 >= addr4); 1651cb0ef41Sopenharmony_ci CHECK(addr4 > addr3); 1661cb0ef41Sopenharmony_ci CHECK(addr4 >= addr3); 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ci // Not comparable 1691cb0ef41Sopenharmony_ci CHECK_EQ(addr1.compare(addr3), SocketAddress::CompareResult::NOT_COMPARABLE); 1701cb0ef41Sopenharmony_ci CHECK_EQ(addr3.compare(addr1), SocketAddress::CompareResult::NOT_COMPARABLE); 1711cb0ef41Sopenharmony_ci CHECK(!(addr1 < addr3)); 1721cb0ef41Sopenharmony_ci CHECK(!(addr1 > addr3)); 1731cb0ef41Sopenharmony_ci CHECK(!(addr1 >= addr3)); 1741cb0ef41Sopenharmony_ci CHECK(!(addr1 <= addr3)); 1751cb0ef41Sopenharmony_ci CHECK(!(addr3 < addr1)); 1761cb0ef41Sopenharmony_ci CHECK(!(addr3 > addr1)); 1771cb0ef41Sopenharmony_ci CHECK(!(addr3 >= addr1)); 1781cb0ef41Sopenharmony_ci CHECK(!(addr3 <= addr1)); 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci // Comparable 1811cb0ef41Sopenharmony_ci CHECK_EQ(addr1.compare(addr5), SocketAddress::CompareResult::SAME); 1821cb0ef41Sopenharmony_ci CHECK_EQ(addr2.compare(addr6), SocketAddress::CompareResult::SAME); 1831cb0ef41Sopenharmony_ci CHECK_EQ(addr1.compare(addr6), SocketAddress::CompareResult::LESS_THAN); 1841cb0ef41Sopenharmony_ci CHECK_EQ(addr6.compare(addr1), SocketAddress::CompareResult::GREATER_THAN); 1851cb0ef41Sopenharmony_ci CHECK(addr1 <= addr5); 1861cb0ef41Sopenharmony_ci CHECK(addr1 <= addr6); 1871cb0ef41Sopenharmony_ci CHECK(addr1 < addr6); 1881cb0ef41Sopenharmony_ci CHECK(addr6 > addr1); 1891cb0ef41Sopenharmony_ci CHECK(addr6 >= addr1); 1901cb0ef41Sopenharmony_ci CHECK(addr2 >= addr6); 1911cb0ef41Sopenharmony_ci CHECK(addr2 >= addr5); 1921cb0ef41Sopenharmony_ci} 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ciTEST(SocketAddressBlockList, Simple) { 1951cb0ef41Sopenharmony_ci SocketAddressBlockList bl; 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci sockaddr_storage storage[2]; 1981cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET, "10.0.0.1", 0, &storage[0]); 1991cb0ef41Sopenharmony_ci SocketAddress::ToSockAddr(AF_INET, "10.0.0.2", 0, &storage[1]); 2001cb0ef41Sopenharmony_ci std::shared_ptr<SocketAddress> addr1 = 2011cb0ef41Sopenharmony_ci std::make_shared<SocketAddress>( 2021cb0ef41Sopenharmony_ci reinterpret_cast<const sockaddr*>(&storage[0])); 2031cb0ef41Sopenharmony_ci std::shared_ptr<SocketAddress> addr2 = 2041cb0ef41Sopenharmony_ci std::make_shared<SocketAddress>( 2051cb0ef41Sopenharmony_ci reinterpret_cast<const sockaddr*>(&storage[1])); 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ci bl.AddSocketAddress(addr1); 2081cb0ef41Sopenharmony_ci bl.AddSocketAddress(addr2); 2091cb0ef41Sopenharmony_ci 2101cb0ef41Sopenharmony_ci CHECK(bl.Apply(addr1)); 2111cb0ef41Sopenharmony_ci CHECK(bl.Apply(addr2)); 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ci bl.RemoveSocketAddress(addr1); 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ci CHECK(!bl.Apply(addr1)); 2161cb0ef41Sopenharmony_ci CHECK(bl.Apply(addr2)); 2171cb0ef41Sopenharmony_ci} 218