From d5e022a38ed4133e74b9c54279e38d421962b6e6 Mon Sep 17 00:00:00 2001 From: Makoto Kato Date: Tue, 8 Nov 2022 09:19:09 +0000 Subject: [PATCH] Bug 1275849 - Part 2. Merge ProxyUtils for Windows and macOS. r=necko-reviewers,valentin Actually, both diff is the following. ```diff @@ -22,14 +19,17 @@ static void NormalizeAddr(const nsACString& aAddr, nsCString& aNormalized) { nsTArray addr; ParseString(aAddr, '.', addr); - aNormalized = - StringJoin("."_ns, IntegerRange(4), [&addr](nsACString& dst, size_t i) { - if (i < addr.Length()) { - dst.Append(addr[i]); - } else { - dst.Append('0'); - } - }); + aNormalized = ""; + for (uint32_t i = 0; i < 4; ++i) { + if (i != 0) { + aNormalized.AppendLiteral("."); + } + if (i < addr.Length()) { + aNormalized.Append(addr[i]); + } else { + aNormalized.AppendLiteral("0"); + } + } } ``` So we should share this on Windows and macOS. Also, since gtest is same, we should merge it. Differential Revision: https://phabricator.services.mozilla.com/D161232 --- toolkit/moz.build | 6 +- .../ProxyUtils.cpp | 0 .../{osxproxy => commonproxy}/ProxyUtils.h | 6 +- .../tests/gtest => commonproxy}/moz.build | 20 +-- .../tests/gtest/TestProxyBypassRules.cpp | 6 +- .../tests/gtest/moz.build | 2 +- toolkit/system/osxproxy/ProxyUtils.cpp | 162 ------------------ toolkit/system/osxproxy/moz.build | 4 +- toolkit/system/windowsproxy/ProxyUtils.h | 21 --- toolkit/system/windowsproxy/moz.build | 5 +- .../tests/gtest/TestProxyBypassRules.cpp | 47 ----- 11 files changed, 25 insertions(+), 254 deletions(-) rename toolkit/system/{windowsproxy => commonproxy}/ProxyUtils.cpp (100%) rename toolkit/system/{osxproxy => commonproxy}/ProxyUtils.h (75%) rename toolkit/system/{windowsproxy/tests/gtest => commonproxy}/moz.build (50%) rename toolkit/system/{osxproxy => commonproxy}/tests/gtest/TestProxyBypassRules.cpp (94%) rename toolkit/system/{osxproxy => commonproxy}/tests/gtest/moz.build (93%) delete mode 100644 toolkit/system/osxproxy/ProxyUtils.cpp delete mode 100644 toolkit/system/windowsproxy/ProxyUtils.h delete mode 100644 toolkit/system/windowsproxy/tests/gtest/TestProxyBypassRules.cpp diff --git a/toolkit/moz.build b/toolkit/moz.build index 94f0edfe31b9..4ba2a61ef911 100644 --- a/toolkit/moz.build +++ b/toolkit/moz.build @@ -49,9 +49,13 @@ if CONFIG["MOZ_WIDGET_TOOLKIT"] != "android": if CONFIG["MOZ_WIDGET_TOOLKIT"] == "gtk": DIRS += ["system/unixproxy"] elif CONFIG["MOZ_WIDGET_TOOLKIT"] == "cocoa": - DIRS += ["system/osxproxy"] + DIRS += [ + "system/commonproxy", + "system/osxproxy", + ] elif CONFIG["MOZ_WIDGET_TOOLKIT"] == "windows": DIRS += [ + "system/commonproxy", "system/windowsproxy", "system/windowsDHCPClient", "system/windowsPackageManager", diff --git a/toolkit/system/windowsproxy/ProxyUtils.cpp b/toolkit/system/commonproxy/ProxyUtils.cpp similarity index 100% rename from toolkit/system/windowsproxy/ProxyUtils.cpp rename to toolkit/system/commonproxy/ProxyUtils.cpp diff --git a/toolkit/system/osxproxy/ProxyUtils.h b/toolkit/system/commonproxy/ProxyUtils.h similarity index 75% rename from toolkit/system/osxproxy/ProxyUtils.h rename to toolkit/system/commonproxy/ProxyUtils.h index 8a449f10a81c..16c451c54793 100644 --- a/toolkit/system/osxproxy/ProxyUtils.h +++ b/toolkit/system/commonproxy/ProxyUtils.h @@ -3,8 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef mozilla_toolkit_system_osxproxy_ProxyUtils_h -#define mozilla_toolkit_system_osxproxy_ProxyUtils_h +#ifndef mozilla_toolkit_system_commonproxy_ProxyUtils_h +#define mozilla_toolkit_system_commonproxy_ProxyUtils_h #include "nsString.h" @@ -18,4 +18,4 @@ bool IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride); } // namespace toolkit } // namespace mozilla -#endif // mozilla_toolkit_system_osxproxy_ProxyUtils_h +#endif // mozilla_toolkit_system_commonproxy_ProxyUtils_h diff --git a/toolkit/system/windowsproxy/tests/gtest/moz.build b/toolkit/system/commonproxy/moz.build similarity index 50% rename from toolkit/system/windowsproxy/tests/gtest/moz.build rename to toolkit/system/commonproxy/moz.build index 46501cdf865d..6f93b46e9a5d 100644 --- a/toolkit/system/windowsproxy/tests/gtest/moz.build +++ b/toolkit/system/commonproxy/moz.build @@ -2,17 +2,17 @@ # vim: set filetype=python: # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this -# file, you can obtain one at http://mozilla.org/MPL/2.0/. +# file, You can obtain one at http://mozilla.org/MPL/2.0/. -UNIFIED_SOURCES += [ - "TestProxyBypassRules.cpp", -] +with Files("**"): + BUG_COMPONENT = ("Core", "Networking") + +TEST_DIRS += ["tests/gtest"] + +SOURCES += ["ProxyUtils.cpp"] + +FINAL_LIBRARY = "xul" LOCAL_INCLUDES += [ - "/toolkit/system/windowsproxy", + "/netwerk/base", ] - -FINAL_LIBRARY = "xul-gtest" - -if CONFIG["CC_TYPE"] in ("clang", "gcc"): - CXXFLAGS += ["-Wshadow"] diff --git a/toolkit/system/osxproxy/tests/gtest/TestProxyBypassRules.cpp b/toolkit/system/commonproxy/tests/gtest/TestProxyBypassRules.cpp similarity index 94% rename from toolkit/system/osxproxy/tests/gtest/TestProxyBypassRules.cpp rename to toolkit/system/commonproxy/tests/gtest/TestProxyBypassRules.cpp index 826011f13ecf..3047088d6271 100644 --- a/toolkit/system/osxproxy/tests/gtest/TestProxyBypassRules.cpp +++ b/toolkit/system/commonproxy/tests/gtest/TestProxyBypassRules.cpp @@ -9,7 +9,7 @@ using namespace mozilla::toolkit::system; -TEST(OSXProxy, TestProxyBypassRules) +TEST(CommonProxy, TestProxyBypassRules) { EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "mozilla.org"_ns)); EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "*mozilla.org"_ns)); @@ -20,7 +20,7 @@ TEST(OSXProxy, TestProxyBypassRules) EXPECT_TRUE(IsHostProxyEntry("www.mozilla.com"_ns, "*.mozilla.*"_ns)); } -TEST(OSXProxy, TestProxyBypassRulesIPv4) +TEST(CommonProxy, TestProxyBypassRulesIPv4) { EXPECT_TRUE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.1.*"_ns)); EXPECT_FALSE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.2.*"_ns)); @@ -32,7 +32,7 @@ TEST(OSXProxy, TestProxyBypassRulesIPv4) EXPECT_TRUE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.1.1/32"_ns)); } -TEST(OSXProxy, TestProxyBypassRulesIPv6) +TEST(CommonProxy, TestProxyBypassRulesIPv6) { EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, "2001:db8:abcd:0012::0/64"_ns)); diff --git a/toolkit/system/osxproxy/tests/gtest/moz.build b/toolkit/system/commonproxy/tests/gtest/moz.build similarity index 93% rename from toolkit/system/osxproxy/tests/gtest/moz.build rename to toolkit/system/commonproxy/tests/gtest/moz.build index 976c83a07e50..3df0793605c9 100644 --- a/toolkit/system/osxproxy/tests/gtest/moz.build +++ b/toolkit/system/commonproxy/tests/gtest/moz.build @@ -9,7 +9,7 @@ UNIFIED_SOURCES += [ ] LOCAL_INCLUDES += [ - "/toolkit/system/osxproxy", + "../..", ] FINAL_LIBRARY = "xul-gtest" diff --git a/toolkit/system/osxproxy/ProxyUtils.cpp b/toolkit/system/osxproxy/ProxyUtils.cpp deleted file mode 100644 index 9c10b35e8582..000000000000 --- a/toolkit/system/osxproxy/ProxyUtils.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "ProxyUtils.h" -#include "nsTArray.h" -#include "prnetdb.h" -#include "prtypes.h" - -namespace mozilla { -namespace toolkit { -namespace system { - -/** - * Normalize the short IP form into the complete form. - * For example, it converts "192.168" into "192.168.0.0" - */ -static void NormalizeAddr(const nsACString& aAddr, nsCString& aNormalized) { - nsTArray addr; - ParseString(aAddr, '.', addr); - aNormalized = ""; - for (uint32_t i = 0; i < 4; ++i) { - if (i != 0) { - aNormalized.AppendLiteral("."); - } - if (i < addr.Length()) { - aNormalized.Append(addr[i]); - } else { - aNormalized.AppendLiteral("0"); - } - } -} - -static PRUint32 MaskIPv4Addr(PRUint32 aAddr, uint16_t aMaskLen) { - if (aMaskLen == 32) { - return aAddr; - } - return PR_htonl(PR_ntohl(aAddr) & (~0L << (32 - aMaskLen))); -} - -static void MaskIPv6Addr(PRIPv6Addr& aAddr, uint16_t aMaskLen) { - if (aMaskLen == 128) { - return; - } - - if (aMaskLen > 96) { - aAddr.pr_s6_addr32[3] = - PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[3]) & (~0L << (128 - aMaskLen))); - } else if (aMaskLen > 64) { - aAddr.pr_s6_addr32[3] = 0; - aAddr.pr_s6_addr32[2] = - PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[2]) & (~0L << (96 - aMaskLen))); - } else if (aMaskLen > 32) { - aAddr.pr_s6_addr32[3] = 0; - aAddr.pr_s6_addr32[2] = 0; - aAddr.pr_s6_addr32[1] = - PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[1]) & (~0L << (64 - aMaskLen))); - } else { - aAddr.pr_s6_addr32[3] = 0; - aAddr.pr_s6_addr32[2] = 0; - aAddr.pr_s6_addr32[1] = 0; - aAddr.pr_s6_addr32[0] = - PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[0]) & (~0L << (32 - aMaskLen))); - } - - return; -} - -static bool IsMatchMask(const nsACString& aHost, const nsACString& aOverride) { - nsresult rv; - - auto tokenEnd = aOverride.FindChar('/'); - if (tokenEnd == -1) { - return false; - } - - nsAutoCString prefixStr( - Substring(aOverride, tokenEnd + 1, aOverride.Length() - tokenEnd - 1)); - auto maskLen = prefixStr.ToInteger(&rv); - if (NS_WARN_IF(NS_FAILED(rv))) { - return false; - } - - nsAutoCString override(aOverride); - NormalizeAddr(Substring(aOverride, 0, tokenEnd), override); - - PRNetAddr prAddrHost; - PRNetAddr prAddrOverride; - if (PR_SUCCESS != - PR_StringToNetAddr(PromiseFlatCString(aHost).get(), &prAddrHost) || - PR_SUCCESS != PR_StringToNetAddr(override.get(), &prAddrOverride)) { - return false; - } - - if (prAddrHost.raw.family == PR_AF_INET && - prAddrOverride.raw.family == PR_AF_INET) { - return MaskIPv4Addr(prAddrHost.inet.ip, maskLen) == - MaskIPv4Addr(prAddrOverride.inet.ip, maskLen); - } else if (prAddrHost.raw.family == PR_AF_INET6 && - prAddrOverride.raw.family == PR_AF_INET6) { - MaskIPv6Addr(prAddrHost.ipv6.ip, maskLen); - MaskIPv6Addr(prAddrOverride.ipv6.ip, maskLen); - - return memcmp(&prAddrHost.ipv6.ip, &prAddrOverride.ipv6.ip, - sizeof(PRIPv6Addr)) == 0; - } - - return false; -} - -static bool IsMatchWildcard(const nsACString& aHost, - const nsACString& aOverride) { - nsAutoCString host(aHost); - nsAutoCString override(aOverride); - - int32_t overrideLength = override.Length(); - int32_t tokenStart = 0; - int32_t offset = 0; - bool star = false; - - while (tokenStart < overrideLength) { - int32_t tokenEnd = override.FindChar('*', tokenStart); - if (tokenEnd == tokenStart) { - // Star is the first character in the token. - star = true; - tokenStart++; - // If the character following the '*' is a '.' character then skip - // it so that "*.foo.com" allows "foo.com". - if (override.FindChar('.', tokenStart) == tokenStart) { - nsAutoCString token(Substring(override, tokenStart + 1, - overrideLength - tokenStart - 1)); - if (host.Equals(token)) { - return true; - } - } - } else { - if (tokenEnd == -1) { - tokenEnd = overrideLength; // no '*' char, match rest of string - } - nsAutoCString token( - Substring(override, tokenStart, tokenEnd - tokenStart)); - offset = host.Find(token, offset); - if (offset == -1 || (!star && offset)) { - return false; - } - star = false; - tokenStart = tokenEnd; - offset += token.Length(); - } - } - - return (star || (offset == static_cast(host.Length()))); -} - -bool IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride) { - return IsMatchMask(aHost, aOverride) || IsMatchWildcard(aHost, aOverride); -} - -} // namespace system -} // namespace toolkit -} // namespace mozilla diff --git a/toolkit/system/osxproxy/moz.build b/toolkit/system/osxproxy/moz.build index a9aff744bc24..a28c9ce35ce4 100644 --- a/toolkit/system/osxproxy/moz.build +++ b/toolkit/system/osxproxy/moz.build @@ -7,11 +7,8 @@ with Files("**"): BUG_COMPONENT = ("Core", "Networking") -TEST_DIRS += ["tests/gtest"] - SOURCES += [ "nsOSXSystemProxySettings.mm", - "ProxyUtils.cpp", ] XPCOM_MANIFESTS += [ @@ -21,5 +18,6 @@ XPCOM_MANIFESTS += [ FINAL_LIBRARY = "xul" LOCAL_INCLUDES += [ + "../commonproxy", "/netwerk/base", ] diff --git a/toolkit/system/windowsproxy/ProxyUtils.h b/toolkit/system/windowsproxy/ProxyUtils.h deleted file mode 100644 index 547ef643b5da..000000000000 --- a/toolkit/system/windowsproxy/ProxyUtils.h +++ /dev/null @@ -1,21 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef mozilla_toolkit_system_windowsproxy_ProxyUtils_h -#define mozilla_toolkit_system_windowsproxy_ProxyUtils_h - -#include "nsString.h" - -namespace mozilla { -namespace toolkit { -namespace system { - -bool IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride); - -} // namespace system -} // namespace toolkit -} // namespace mozilla - -#endif // mozilla_toolkit_system_windowsproxy_ProxyUtils_h diff --git a/toolkit/system/windowsproxy/moz.build b/toolkit/system/windowsproxy/moz.build index a96ea544ec63..cb0e98cd3d57 100644 --- a/toolkit/system/windowsproxy/moz.build +++ b/toolkit/system/windowsproxy/moz.build @@ -7,9 +7,7 @@ with Files("**"): BUG_COMPONENT = ("Core", "Networking") -TEST_DIRS += ["tests/gtest"] - -SOURCES += ["nsWindowsSystemProxySettings.cpp", "ProxyUtils.cpp"] +SOURCES += ["nsWindowsSystemProxySettings.cpp"] XPCOM_MANIFESTS += [ "components.conf", @@ -18,5 +16,6 @@ XPCOM_MANIFESTS += [ FINAL_LIBRARY = "xul" LOCAL_INCLUDES += [ + "../commonproxy", "/netwerk/base", ] diff --git a/toolkit/system/windowsproxy/tests/gtest/TestProxyBypassRules.cpp b/toolkit/system/windowsproxy/tests/gtest/TestProxyBypassRules.cpp deleted file mode 100644 index 9ccb21638863..000000000000 --- a/toolkit/system/windowsproxy/tests/gtest/TestProxyBypassRules.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=8 sts=2 et sw=2 tw=80: */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "gtest/gtest.h" -#include "ProxyUtils.h" - -using namespace mozilla::toolkit::system; - -TEST(WindowsProxy, TestProxyBypassRules) -{ - EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "mozilla.org"_ns)); - EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "*mozilla.org"_ns)); - EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "*.mozilla.org"_ns)); - EXPECT_FALSE(IsHostProxyEntry("notmozilla.org"_ns, "*.mozilla.org"_ns)); - EXPECT_TRUE(IsHostProxyEntry("www.mozilla.org"_ns, "*mozilla.org"_ns)); - EXPECT_TRUE(IsHostProxyEntry("www.mozilla.org"_ns, "*.mozilla.org"_ns)); - EXPECT_TRUE(IsHostProxyEntry("www.mozilla.com"_ns, "*.mozilla.*"_ns)); -} - -TEST(WindowsProxy, TestProxyBypassRulesIPv4) -{ - EXPECT_TRUE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.1.*"_ns)); - EXPECT_FALSE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.2.*"_ns)); - - EXPECT_TRUE(IsHostProxyEntry("10.1.2.3"_ns, "10.0.0.0/8"_ns)); - EXPECT_TRUE(IsHostProxyEntry("192.168.192.1"_ns, "192.168/16"_ns)); - EXPECT_FALSE(IsHostProxyEntry("192.168.192.1"_ns, "192.168/17"_ns)); - EXPECT_TRUE(IsHostProxyEntry("192.168.192.1"_ns, "192.168.128/17"_ns)); - EXPECT_TRUE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.1.1/32"_ns)); -} - -TEST(WindowsProxy, TestProxyBypassRulesIPv6) -{ - EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, - "2001:db8:abcd:0012::0/64"_ns)); - EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0000:4567:89AB:CDEF"_ns, - "2001:db8:abcd:0012::0/80"_ns)); - EXPECT_FALSE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, - "2001:db8:abcd:0012::0/80"_ns)); - EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0000:0000:89AB:CDEF"_ns, - "2001:db8:abcd:0012::0/96"_ns)); - EXPECT_FALSE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, - "2001:db8:abcd:0012::0/96"_ns)); -}