File indexing completed on 2024-11-17 04:55:15

0001 /*
0002     SPDX-License-Identifier: MPL-2.0
0003 */
0004 
0005 /* Copyright (c) 2015 Brian R. Bondy. Distributed under the MPL2 license.
0006  * This Source Code Form is subject to the terms of the Mozilla Public
0007  * License, v. 2.0. If a copy of the MPL was not distributed with this
0008  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
0009 
0010 #include <string.h>
0011 #include <fstream>
0012 #include <sstream>
0013 #include <string>
0014 #include <cerrno>
0015 #include <algorithm>
0016 #include <iostream>
0017 #include <set>
0018 #include "./protocol.h"
0019 #include "./CppUnitLite/TestHarness.h"
0020 #include "./CppUnitLite/Test.h"
0021 #include "./util.h"
0022 
0023 // Testing isBlockableProtocol
0024 TEST(isBlockableProtocol, basic) {
0025   // Each of the below should be valid, since they're urls of protocols
0026   // we block against
0027   CHECK(isBlockableProtocol("http://example.com", 19));
0028   CHECK(isBlockableProtocol("https://example.com", 20));
0029   CHECK(isBlockableProtocol("ws://example.com/path", 22));
0030   CHECK(isBlockableProtocol("wss://example.com/path", 23));
0031 
0032   // Each of the following should also be valid, since we ignore "blob"
0033   // pre-prefixes on URLs.
0034   CHECK(isBlockableProtocol("blob:http://example.com", 24));
0035   CHECK(isBlockableProtocol("blob:https://example.com", 25));
0036   CHECK(isBlockableProtocol("blob:ws://example.com/path", 27));
0037   CHECK(isBlockableProtocol("blob:wss://example.com/path", 28));
0038 
0039   // We don't care about case either, so each of the following should also
0040   // pass.
0041   CHECK(isBlockableProtocol("hTtp://example.com", 19));
0042   CHECK(isBlockableProtocol("htTPs://example.com", 20));
0043   CHECK(isBlockableProtocol("WS://example.com/path", 22));
0044   CHECK(isBlockableProtocol("WSS://example.com/path", 23));
0045 
0046   // Each of the following should fail, since they are each non supported /
0047   // blockable protocols.
0048 
0049   // To short example
0050   CHECK(isBlockableProtocol("htt", 3) == false);
0051 
0052   // Bad protocol example.
0053   CHECK(isBlockableProtocol("htttp://example.com", 25) == false);
0054 
0055   // No protocol example
0056   CHECK(isBlockableProtocol("example.com", 12) == false);
0057 
0058   // PNG data url example (not a valid image)[
0059   const char *pngPixel = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEA==";
0060   int pngPixelLen = strlen(pngPixel);
0061   CHECK(isBlockableProtocol(pngPixel, pngPixelLen) == false);
0062 
0063   // SVG data url example
0064   const char *svgUrl = "data:image/svg+xml;utf8,<svg "
0065                        "xmlns='http://www.w3.org/2000/svg'></svg>";
0066   int svgUrlLen = strlen(svgUrl);
0067   CHECK(isBlockableProtocol(svgUrl, svgUrlLen) == false);
0068 }