Example #1
0
def main():
	(opts, _) = optionParser.parseCommandLine()

	runner.loadSqlScript(opts, runner.getRepoDir() + '/sql/test_scenario2_inserts.sql')

	runner.startBackend(opts, processes)
	runner.startHttpd(opts, processes)

	runner.setBackendTime(processes, "2014-03-21 16:00:00")
	issueRequest(opts, "http://www.facebook.com", runner.CATEGORY_BLOCKED_CUSTOM)
	issueRequest(opts, "http://www.battle.net", runner.ACCESS_OK)

	runner.setBackendTime(processes, "2014-03-21 18:01:00")
	issueRequest(opts, "http://www.battle.net", runner.INTERNET_ACCESS_FORBIDDEN)

	runner.loadSqlScript(opts, runner.getRepoDir() + '/sql/test_scenario2_parent_inserts.sql')

	runner.setBackendTime(processes, "2014-03-21 18:02:00")
	issueRequest(opts, "http://www.facebook.com", runner.INTERNET_ACCESS_FORBIDDEN)

	runner.setBackendTime(processes, "2014-03-22 08:00:00")
	issueRequest(opts, "http://www.facebook.com", runner.ACCESS_OK)
	issueRequest(opts, "http://www.battle.net", runner.CATEGORY_BLOCKED_CUSTOM)

	print "All tests OK"
Example #2
0
def main():
	(opts, _) = optionParser.parseCommandLine()

	tunnelTargetPort = opts.dbPort
	tunnelTargetHost = opts.dbHost
	opts.dbPort = opts.tunnelPort
	opts.dbHost = 'localhost'

	runner.startTunnel(opts, processes, tunnelTargetHost, tunnelTargetPort)
	runner.startBackend(opts, processes)
	runner.startHttpd(opts, processes)

	runner.setBackendTime(processes, "2014-03-21 18:12:00")

	# we have to create connection here to be reused
	print 'connecting to db on localhost:%s' % opts.httpdPort
	connection = runner.createConnection('localhost:'+opts.httpdPort)
	issueRequest(connection, opts.userId, "http://www.onet.pl", runner.INTERNET_ACCESS_FORBIDDEN)

	runner.stopTunnel(processes)
	issueRequest(connection, opts.userId, "http://www.onet.pl", runner.ACCESS_OK)

	runner.startTunnel(opts, processes, tunnelTargetHost, tunnelTargetPort)
	issueRequest(connection, opts.userId, "http://www.onet.pl", runner.INTERNET_ACCESS_FORBIDDEN)

	print "All tests OK"
def main():
    (opts, _) = optionParser.parseCommandLine()
    opts.timeout = 1000  # 1sec timeout

    runner.startHttpd(opts, processes)

    urls = file(os.path.abspath(
        __file__ + '/../../alexa100k.urls.txt')).readlines()[:NUM_REQUESTS]

    startTime = time.time()
    issueSequentialRequests(opts, urls, timeoutExpected=True)
    duration = time.time() - startTime
    expectedDuration = NUM_REQUESTS * float(opts.timeout) / 1000
    assert almostEqual(duration, expectedDuration,
                       TOLERANCE), "expected timed out requests: %f vs %f" % (
                           duration, expectedDuration)

    runner.startBackend(opts, processes)

    startTime = time.time()
    issueSequentialRequests(opts, urls)
    duration = time.time() - startTime

    if opts.dbHost == "" or opts.dbHost == "localhost":
        print "db is local"
        expectedDuration = expectedDuration / 10
    else:
        print "db is remote"
        expectedDuration = expectedDuration / 2
    assert duration < expectedDuration, "requests duration: %f, expected less than %f" % (
        duration, expectedDuration)
def main():
    (opts, _) = optionParser.parseCommandLine()

    realWebrootPort = 80
    realWebrootHost = 'service2.brightcloud.com'

    webrootCfgPath = 'scripts/tests/webrootUnavailableTest/webroot.cfg'
    fp = open(webrootCfgPath)
    for i, line in enumerate(fp):
        if 'BcapPort' in line:
            opts.tunnelPort = getValueFromConfigLine(line)
        if 'BcapServer' in line:
            tunnelHost = getValueFromConfigLine(line)
    fp.close()

    connectionMax = str(NUM_OF_THREADS * 3 + 100)
    httpdArgs = [
        "--connectionPerIpLimitAbsolute",
        connectionMax,
        "--connectionPerIpLimitSlowDown",
        connectionMax,
    ]

    opts.webrootConfigFilePath = os.path.join(runner.getRepoDir(),
                                              webrootCfgPath)
    runner.startTunnel(opts, processes, tunnelHost, realWebrootPort,
                       realWebrootHost)
    runner.startBackend(opts, processes)
    runner.startHttpd(opts, processes, httpdArgs)

    runner.setBackendTime(processes, "2014-03-21 16:12:00")

    # we have to create connection here to be reused
    #print 'connecting to db on localhost:%s' % opts.httpdPort
    connection = runner.createConnection('localhost:' + opts.httpdPort)
    issueRequest(connection, opts.userId, "http://www.takatamstronahehe.pl",
                 runner.ACCESS_OK, False)
    issueRequest(connection, opts.userId, "http://pornhub.com",
                 runner.CATEGORY_BLOCKED_CUSTOM, False)
    issueRequest(connection, opts.userId, "http://www.onet.pl",
                 runner.ACCESS_OK, False)

    runner.stopTunnel(processes)
    server_address = (tunnelHost, int(opts.tunnelPort))
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(server_address)
    sock.listen(1)

    for i in range(0, NUM_OF_THREADS):
        newThread = multiprocessing.Process(target=sendFewRequest,
                                            args=(opts, ))
        newThread.start()
        threads.append(newThread)

    time.sleep(10)
def main():
	(opts, _) = optionParser.parseCommandLine()

	runner.startBackend(opts, processes)
	runner.startHttpd(opts, processes)

	runner.setBackendTime(processes, "2014-03-24 12:00:01")
	issueRequest(opts, "http://onet.pl", runner.ACCESS_OK, '70514')
	issueRequest(opts, "http://onet.pl", runner.UNKNOWN_USER, '12345123123123')

	print "All tests OK"
def main():
    (opts, args) = optionParser.parseCommandLine()

    if opts.dbHost == "" or opts.dbHost == "localhost":
        print "db is local"
        opts.timeout = 10000
        expectedDuration = 2
    else:
        print "db is remote"
        opts.timeout = 20000
        expectedDuration = 4

    runner.startHttpd(opts, processes)

    urls = file(os.path.abspath(
        __file__ + '/../../alexa100k.urls.txt')).readlines()[:NUM_REQUESTS]

    pool = startConcurrentRequests(opts, urls)

    sleepTime = opts.timeout / 2 / 1000  # sleep half of timeout
    print 'waiting for %.2fs' % sleepTime

    class Timeout:
        pass

    def handler(signum, frame):
        raise Timeout()

    signal.signal(signal.SIGALRM, handler)
    signal.alarm(sleepTime)
    try:
        checkResponses(pool)
    except Timeout:
        pass
    else:
        assert False, 'requests finished too fast'
    signal.alarm(0)

    # When cloud is used:
    # Requests are not dropped until backend starts and on start it has to process them all when db is
    # not yet loaded, so they are sent to the cloud. Loading of full db takes more than 5s.
    # Therefore we disable cloud.
    backendArgs = ['--disableWebrootCloud', '--disableWebrootLocalDb']
    runner.startBackend(opts, processes, backendArgs)

    startTime = time.time()

    checkResponses(pool)

    duration = time.time() - startTime
    print 'handling duration: %.2fs' % duration
    assert duration < expectedDuration, "all requests should have finished within %.2fs after backend start; instead got: %.2fs" % \
     (expectedDuration, duration)
Example #7
0
def main():
    (opts, _) = optionParser.parseCommandLine()

    numUrls = 15000

    print "Overriding logLevel to INFO"
    opts.logLevel = 'INFO'

    # too many urls from alexa list go to cloud
    backendArgs = [
        '--setCurrentTime', '"2014-03-21 15:31:00"', '--disableWebrootCloud'
    ]
    runner.startBackend(opts, processes, backendArgs)
    httpdArgs = []
    if opts.ssl:
        httpdArgs = [
            "-s",  # use SSL
            "--keyFile",
            "~/ssl/httpd.key",  # key file
            "--certFile",
            "~/ssl/httpd.pem"  # certificate file
        ]
    httpdArgs += [
        "--connectionPerIpLimitAbsolute",
        str(numUrls),
        "--connectionPerIpLimitSlowDown",
        str(numUrls),
    ]
    runner.startHttpd(opts, processes, httpdArgs)

    urls = file(
        os.path.abspath(__file__ +
                        '/../../alexa100k.urls.txt')).readlines()[:numUrls]

    duration = issueRequests(opts, urls, opts.ssl)
    performance = len(urls) / duration
    print 'reqs/s: %.1f (duration %.2f, urls %d)' % (performance, duration,
                                                     len(urls))

    assert performance > 400, "Performance is lower than expected"

    output = None
    if opts.ssl:
        output = open("performance-ssl.txt", "w")
    else:
        output = open("performance.txt", "w")

    output.write('%.1f\n' % performance)
    output.close()
Example #8
0
def main():
	(opts, _) = optionParser.parseCommandLine()

	runner.startBackend(opts, processes)
	runner.startHttpd(opts, processes)

	runner.setBackendTime(processes, "2014-03-21 15:31:00")
	issueRequest(opts, "http://onet.pl", runner.ACCESS_OK)

	runner.setBackendTime(processes, "2014-03-21 18:12:00")
	issueRequest(opts, "http://www.onet.pl", runner.INTERNET_ACCESS_FORBIDDEN)

	runner.setBackendTime(processes, "2014-03-21 08:03:00")
	issueRequest(opts, "http://pornhub.com", runner.CATEGORY_BLOCKED_CUSTOM)

	runner.setBackendTime(processes, "2014-03-21 07:59:00")
	issueRequest(opts, "http://pornhub.com", runner.INTERNET_ACCESS_FORBIDDEN)

	print "All tests OK"
Example #9
0
def main():
    (opts, _) = optionParser.parseCommandLine()

    runner.startBackend(opts, processes)
    runner.startHttpd(opts, processes)

    currentTime = "2014-03-21 20:11:00"
    runner.setBackendTime(processes, currentTime)
    issueRequest(opts, "http://onet.pl", runner.INTERNET_ACCESS_FORBIDDEN)
    accessStartTime = "2014-03-21 20:00:00"
    sqlStatement = 'insert into tmp_access_control (id, code_child_id, start_date, duration, parent_accepted)'\
    " values (1, 37, '%s', '02:12:00', false)" % accessStartTime
    runner.issueSqlCommand(opts, sqlStatement)
    issueRequest(opts, "http://onet.pl", runner.INTERNET_ACCESS_FORBIDDEN)
    sqlStatement = 'update tmp_access_control set parent_accepted=true where id=1'
    runner.issueSqlCommand(opts, sqlStatement)
    issueRequest(opts, "http://onet.pl", runner.ACCESS_OK)

    print "All tests OK"
def main():
    (opts, _) = optionParser.parseCommandLine()

    runner.startBackend(opts, processes)
    runner.startHttpd(opts, processes)

    runner.setBackendTime(processes, "2014-03-24 08:00:01")
    issueRequest(opts, "http://onet.pl", runner.INTERNET_ACCESS_FORBIDDEN)

    runner.setBackendTime(processes, "2014-03-24 10:00:01")
    issueRequest(opts, "http://pornhub.com", runner.ACCESS_OK)

    runner.setBackendTime(processes, "2014-03-23 08:00:01")
    issueRequest(opts, "http://zabijzajaca.pl", runner.ACCESS_OK)

    runner.setBackendTime(processes, "2014-03-23 20:00:01")
    issueRequest(opts, "http://fasdfasdfasdfaqwe.com.pl",
                 runner.INTERNET_ACCESS_FORBIDDEN)

    print "All tests OK"
def main():
    (opts, _) = optionParser.parseCommandLine()

    runner.loadSqlScript(
        opts,
        runner.getRepoDir() + '/sql/test_scenario3_inserts.sql')

    runner.startBackend(opts, processes)
    runner.startHttpd(opts, processes)

    runner.setBackendTime(processes, "2014-04-10 18:01:00")
    issueRequest(opts, "http://www.onet.pl", runner.INTERNET_ACCESS_FORBIDDEN)

    runner.issueSqlCommand(
        opts, "update access_control set end_time='19:00' where id=84")

    issueRequest(opts, "http://www.onet.pl", runner.ACCESS_OK)

    runner.issueSqlCommand(
        opts, "update access_control set end_time='18:00' where id=84")

    issueRequest(opts, "http://www.onet.pl", runner.INTERNET_ACCESS_FORBIDDEN)

    print "All tests OK"
Example #12
0
def main():
    (opts, _) = optionParser.parseCommandLine()
    defaultAdmissionControlCustomSettingsFile = opts.admissionControlCustomSettingsFile
    opts.admissionControlCustomSettingsFile = TEST_FILE

    backendArgs = ['--disableWebrootCloud']
    runner.startBackend(opts, processes, backendArgs)
    runner.setBackendTime(processes, "2014-09-09 16:42:00")

    #BEGIN OF: custom setting file tests
    #First case - not exiting file [ERROR]
    httpdArgs = [
        "--connectionPerIpLimitAbsolute",
        "600",
        "--connectionPerIpLimitSlowDown",
        "600",
        "--slowDownDuration",
        "300",
    ]
    runner.startHttpd(opts,
                      processes,
                      httpdArgs,
                      True,
                      createAdmissionControlFileIfNotExists=False)

    #Second case - exiting and empty file [ok]
    admissionControlCustomSettingsFile = open(TEST_FILE, 'w')
    admissionControlCustomSettingsFile.close()
    httpdArgs = [
        "--connectionPerIpLimitAbsolute",
        "600",
        "--connectionPerIpLimitSlowDown",
        "600",
        "--slowDownDuration",
        "300",
    ]
    runner.startHttpd(opts,
                      processes,
                      httpdArgs,
                      createAdmissionControlFileIfNotExists=False)
    runner.stopHttpd(processes)

    #Working file
    runHttpd(
        opts, processes, "168.192.0.1 123 543\n" + "127.3.0.1 123 543\n" +
        "91.32.111.15 123 543\n" + "168.192.0.4 86 153\n", False)

    #Third case - file parsing errors
    #2x same ip
    runHttpd(
        opts, processes, "127.0.0.1 123 543\n" + "127.3.0.1 123 543\n" +
        "127.1.0.5 123 543\n" + "127.0.0.1 86 153\n", True)

    #2x same ip
    runHttpd(
        opts, processes, "2001:0db8:0000:0000:0000:ff00:0042:8329 123 543\n" +
        "2001:db8:0:0:0:ff00:42:8329 123 543\n" + "127.1.0.5 123 543\n" +
        "127.0.0.1 86 153\n", True)

    #2x same ip
    runHttpd(
        opts, processes, "2001:db8:0:0:0:ff00:42:8329\n 100 153" +
        "2001:db8::ff00:42:8329\n 86 153" + "127.1.0.5 123 543\n" +
        "127.0.0.1 86 153\n", True)

    #2x same ip
    runHttpd(
        opts, processes, "2001:db8:0:0:0:ff00:42:8329 86 153\n" +
        "0:0:0:0:0:ffff:630c:224c 86 153\n" + "127.1.0.5 123 543\n" +
        "99.12.34.76 86 153\n", True)

    #Parsing error (first line)
    runHttpd(
        opts, processes, "1 127.0.0.1 543\n" + "127.3.0.1 123 543\n" +
        "127.1.0.5 123 543\n" + "127.0.0.2 86 153\n", True)

    #Parsing error (first line)
    runHttpd(
        opts, processes, "1 127.0.0.1 123 543\n" + "127.3.0.1 123 543\n" +
        "127.1.0.5 123 543\n" + "127.0.0.2 86 153\n", True)

    #Parsing error (third line)
    runHttpd(
        opts, processes, "127.0.0.1 123 543\n" + "127.3.0.1 123 543\n" +
        "127.1.0.5 123 543 \n" + "127.0.0.2 86 153\n", True)

    #Parsing error (third line)
    runHttpd(
        opts, processes, "127.0.0.1 123 543\n" + "127.3.0.1 123 543\n" +
        "127.1.0.5 12 123 543\n" + "127.0.0.2 86 153\n", True)

    #Parsing error (second line)
    runHttpd(
        opts, processes, "127.0.0.1 123 543\n" + "127.0.1 123 543\n" +
        "127.1.0.5 123 543\n" + "127.0.0.2 86 153\n", True)

    #Parsing error (second line)
    runHttpd(
        opts, processes, "127.0.0.1 123 543\n" + "123 127.3.0.1 543\n" +
        "127.1.0.5 123 543\n" + "127.0.0.2 86 153\n", True)

    #Parsing error (second line)
    runHttpd(opts, processes, "127.0.0.1 123 543\n" + "127.3.0.1\n", True)

    #Fourth case - connectionPerIpLimitAbsolute

    runHttpd(
        opts, processes, "127.3.0.1 123 543\n" + "91.32.111.15 123 543\n" +
        "127.0.0.1 10 1000\n" + "168.192.0.4 86 153\n", False, False)

    for i in range(0, 10):
        issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK, 503)

    runner.stopHttpd(processes)

    #Fifth case - connectionPerIpLimitAbsolute

    runHttpd(
        opts, processes, "127.3.0.1 123 543\n" + "91.32.111.15 123 543\n" +
        "127.0.0.1 100 5\n" + "168.192.0.4 86 153\n", False, False)

    startTime = datetime.datetime.now()
    for i in range(0, 50):
        issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK)
    assert (datetime.datetime.now() - startTime >
            datetime.timedelta(seconds=10))

    runner.stopHttpd(processes)

    #Sixth case - file reloading: file is ok

    runHttpd(
        opts, processes, "127.3.0.1 123 543\n" + "91.32.111.15 123 543\n" +
        "127.0.0.1 10 1000\n" + "168.192.0.4 86 153\n", False, False)

    for i in range(0, 10):
        issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK, 503)

    reloadConfigFile(processes, "127.0.0.1 100 1000\n")

    for i in range(0, 30):
        issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK)

    runner.stopHttpd(processes)

    #Seventh case - file reloading: file with error, but httpd don't crash,
    #in the end loading correct file

    runHttpd(
        opts, processes, "127.3.0.1 123 543\n" + "91.32.111.15 123 543\n" +
        "127.0.0.1 10 1000\n" + "168.192.0.4 86 153\n", False, False)

    for i in range(0, 10):
        issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK, 503)

    reloadConfigFile(processes, "asdfasdfq345\n\n2345sadf \n")

    reloadConfigFile(
        processes, "127.0.0.1 123 543\n" + "123 127.3.0.1 543\n" +
        "127.1.0.5 123 543\n" + "127.0.0.2 86 153\n")

    reloadConfigFile(processes, "127.0.0.1 100 1000\n")

    reloadConfigFile(
        processes, "2001:db8:0:0:0:ff00:42:8329 86 153\n" +
        "0:0:0:0:0:ffff:630c:224c 86 153\n" + "127.1.0.5 123 543\n" +
        "99.12.34.76 86 153\n")

    reloadConfigFile(processes, "127.0.0.1 100 1000\n")
    reloadConfigFile(processes, "127.0.0.1 100 1000\n")
    reloadConfigFile(processes, "127.0.0.1 100 1000\n")

    for i in range(0, 30):
        issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK)

    runner.stopHttpd(processes)

    #END OF: custom setting file tests

    # X-Forwarded-For test: don't use the header if not enabled
    assert opts.useForwardedFor == False, "in tests we don't use X-Forwarded-For by default"
    runHttpd(opts,
             processes,
             "127.0.0.42 20 1000\n",
             False,
             False,
             limitAbsolute=10)

    addHeaders = {'X-Forwarded-For': '127.0.0.42'}
    # header is ignored by httpd
    for i in range(10):
        issueRequest(opts,
                     "http://www.google.pl",
                     runner.ACCESS_OK,
                     addHeaders=addHeaders)
    issueRequest(opts,
                 "http://www.google.pl",
                 runner.ACCESS_OK,
                 503,
                 addHeaders=addHeaders)

    runner.stopHttpd(processes)

    # X-Forwarded-For test: use the header if enabled
    opts.useForwardedFor = True
    runHttpd(opts,
             processes,
             "127.0.0.42 20 1000\n",
             False,
             False,
             limitAbsolute=10)

    addHeaders = {'X-Forwarded-For': '127.0.0.42'}
    # more requests possible for this IP
    for i in range(20):
        issueRequest(opts,
                     "http://www.google.pl",
                     runner.ACCESS_OK,
                     addHeaders=addHeaders)
    issueRequest(opts,
                 "http://www.google.pl",
                 runner.ACCESS_OK,
                 503,
                 addHeaders=addHeaders)

    # X-Forwarded-For test: whitelisted IP is itself a proxy
    # connection goes like this: 200.50.10.5 -> 127.0.0.42 -> load balancer -> httpd
    addHeaders = {'X-Forwarded-For': '200.50.10.5, 127.0.0.42'}
    # renew limits
    reloadConfigFile(processes, "127.0.0.42 20 1000\n")
    for i in range(20):
        issueRequest(opts,
                     "http://www.google.pl",
                     runner.ACCESS_OK,
                     addHeaders=addHeaders)
    issueRequest(opts,
                 "http://www.google.pl",
                 runner.ACCESS_OK,
                 503,
                 addHeaders=addHeaders)

    # X-Forwarded-For test: unauthorized user added the header for whitelisted IP, load balancer added real attacker IP
    addHeaders = {'X-Forwarded-For': '127.0.0.42, 100.100.100.100'}
    # don't renew limits - there where only requests from whitelisted IP
    # only 10 requests possible
    for i in range(10):
        issueRequest(opts,
                     "http://www.google.pl",
                     runner.ACCESS_OK,
                     addHeaders=addHeaders)
    issueRequest(opts,
                 "http://www.google.pl",
                 runner.ACCESS_OK,
                 503,
                 addHeaders=addHeaders)

    runner.stopHttpd(processes)
    opts.useForwardedFor = False

    #END OF: X-Forwarded-For tests

    opts.admissionControlCustomSettingsFile = defaultAdmissionControlCustomSettingsFile

    httpdArgs = [
        "--connectionPerIpLimitAbsolute",
        "600",
        "--connectionPerIpLimitSlowDown",
        "600",
        "--slowDownDuration",
        "300",
    ]
    runner.startHttpd(opts, processes, httpdArgs)

    startTime = datetime.datetime.now()
    for i in range(0, 500):
        issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK)
    assert (datetime.datetime.now() - startTime <
            datetime.timedelta(seconds=10))

    runner.stopHttpd(processes)
    time.sleep(5)
    httpdArgs = [
        "--connectionPerIpLimitAbsolute", "600",
        "--connectionPerIpLimitSlowDown", "100", "--slowDownDuration", "300"
    ]
    runner.startHttpd(opts, processes, httpdArgs)

    startTime = datetime.datetime.now()
    for i in range(0, 150):
        issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK)
    assert (datetime.datetime.now() - startTime >
            datetime.timedelta(seconds=10))

    runner.stopHttpd(processes)
    time.sleep(5)
    httpdArgs = [
        "--connectionPerIpLimitAbsolute", "105",
        "--connectionPerIpLimitSlowDown", "103", "--slowDownDuration", "300"
    ]
    runner.startHttpd(opts, processes, httpdArgs)

    startTime = datetime.datetime.now()
    for i in range(0, 105):
        issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK, 503)

    runner.stopHttpd(processes)
    time.sleep(5)
    httpdArgs = [
        "--connectionPerIpLimitAbsolute", "1000",
        "--connectionPerIpLimitSlowDown", "900", "--slowDownDuration", "300"
    ]
    runner.startHttpd(opts, processes, httpdArgs)

    startTime = datetime.datetime.now()
    for i in range(0, 20):
        issueRequest(opts,
                     "http://www.google.pl",
                     runner.ACCESS_OK,
                     userId="this444User333Definitly222Not111Exist12345",
                     headerResult=runner.UNKNOWN_USER)
    issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK, 503)

    print "All tests OK"
def main():
    (opts, _) = optionParser.parseCommandLine()

    runner.startHttpd(opts, processes)

    runner.startBackend(opts, processes)
    runner.setBackendTime(processes, "2014-09-12 10:05:59")

    issueRequest(opts, "http://www.onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "subdomain.some-obscene-site.com", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.abcD.com", runner.ACCESS_OK)  # SAF-449

    runner.stopBackend(processes)

    #insert some unvalid URLs
    insertIntoWebrootOverrides(opts, 101, 'asd87asd9asdj@#$%^&*().pl', 11)
    insertIntoWebrootOverrides(opts, 102, '\/\/\/\/\/\/\////\///\\\\////\/\/',
                               11)
    insertIntoWebrootOverrides(opts, 104, 'onet', 11)

    insertIntoWebrootOverrides(opts, 105, 'www.Abcd.com', 11)  # SAF-449
    #SAF-478 TEST
    insertIntoWebrootOverrides(opts, 106, "www.onet.pl/" + ('A' * 5060), 11)

    # verify backend loads overrides on startup
    insertIntoWebrootOverrides(opts, 1, 'onet.pl', 11)
    insertIntoWebrootOverrides(opts, 2, 'http://some-obscene-site.com', 11)

    runner.startBackend(opts, processes)
    runner.setBackendTime(processes, "2014-09-12 10:05:59")

    issueRequest(opts, "http://www.onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "subdomain.some-obscene-site.com",
                 runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "www.abcD.com",
                 runner.CATEGORY_BLOCKED_CUSTOM)  # SAF-449

    runner.stopBackend(processes)

    # verify backend periodically reloads overrides from db
    overridesUpdateInterval = 10
    backendArgs = ['--overridesUpdateInterval', str(overridesUpdateInterval)]
    runner.startBackend(opts, processes, backendArgs)
    runner.setBackendTime(processes, "2014-09-12 10:05:59")

    issueRequest(opts, "http://www.onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "subdomain.some-obscene-site.com",
                 runner.CATEGORY_BLOCKED_CUSTOM)

    # site deleted from overrides
    runner.issueSqlCommand(opts, "delete from webroot_overrides where id=2")

    time.sleep(overridesUpdateInterval + 3)

    issueRequest(opts, "http://www.onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "subdomain.some-obscene-site.com", runner.ACCESS_OK)

    # site added again to overrides
    insertIntoWebrootOverrides(opts, 3, 'http://some-obscene-site.com', 11)

    time.sleep(overridesUpdateInterval + 3)

    issueRequest(opts, "http://www.onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "subdomain.some-obscene-site.com",
                 runner.CATEGORY_BLOCKED_CUSTOM)

    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)

    insertIntoWebrootOverrides(opts, 4, 'http://bing.pl', 11)
    runner.reloadBackendOverrides(processes)

    issueRequest(opts, "onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://www.bing.pl", runner.CATEGORY_BLOCKED_CUSTOM)

    runner.issueSqlCommand(opts, "delete from webroot_overrides")
    insertIntoWebrootOverrides(opts, 5, 'wp.pl', 11)
    runner.reloadBackendOverrides(processes)

    issueRequest(opts, "onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "www.sport.wp.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "www.wp.pl/?subpage", runner.CATEGORY_BLOCKED_CUSTOM)

    runner.issueSqlCommand(opts, "delete from webroot_overrides")
    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.ACCESS_OK)

    print "All tests OK"
def main():
    (opts, _) = optionParser.parseCommandLine()

    if opts.dbHost == "" or opts.dbHost == "localhost":
        print "db is local"
        NUM_REQUESTS = 1000
    else:
        print "db is remote"
        NUM_REQUESTS = 20

    requestLimit = str(NUM_REQUESTS + 200)
    httpdArgs = [
        '--connectionLimit', requestLimit, "--connectionPerIpLimitAbsolute",
        requestLimit, "--connectionPerIpLimitSlowDown", requestLimit
    ]
    runner.startHttpd(opts, processes, httpdArgs)
    runner.startBackend(opts, processes)

    allRequests = []

    # start backend at 16:00
    timestamp = datetime.datetime.combine(datetime.date.today(),
                                          datetime.time(16, 0))
    runner.setBackendTime(processes, timestamp)

    curRequests = []
    curRequests.append(
        Request("http://www.facebook.com", runner.CATEGORY_BLOCKED_CUSTOM,
                timestamp, opts.userId, '3'))
    curRequests.append(
        Request("http://www.battle.net", runner.ACCESS_OK, timestamp,
                opts.userId, '2'))
    # The same second time. This time let's don't verify the categoryGroupId in response. It is however verified that whatever was the
    # category-group-id header in response, the same goes to request log.
    curRequests.append(
        Request("http://www.facebook.com", runner.CATEGORY_BLOCKED_CUSTOM,
                timestamp, opts.userId, None))
    curRequests.append(
        Request("http://www.battle.net", runner.ACCESS_OK, timestamp,
                opts.userId, None))

    # verify special characters are properly logged
    curRequests.append(
        Request("http://www.battle.net/abc\ndef", runner.ACCESS_OK, timestamp,
                opts.userId, None))
    curRequests.append(
        Request("http://www.battle.net/abc\tdef", runner.ACCESS_OK, timestamp,
                opts.userId, None))
    curRequests.append(
        Request("http://www.battle.net/abc\\def", runner.ACCESS_OK, timestamp,
                opts.userId, None))

    # verify not-empty UserAction header
    curRequests.append(
        Request("http://www.battle.net",
                runner.ACCESS_OK,
                timestamp,
                opts.userId,
                None,
                userAction=UserActionCode.NON_INTENTIONAL_REQUEST))
    curRequests.append(
        Request("http://www.battle.net",
                runner.ACCESS_OK,
                timestamp,
                opts.userId,
                None,
                userAction=UserActionCode.INTENTIONAL_REQUEST))
    curRequests.append(
        Request("http://www.battle.net",
                runner.ACCESS_OK,
                timestamp,
                opts.userId,
                None,
                userAction=UserActionCode.CATEGORY_GROUP_QUERY))
    # malformed header
    curRequests.append(
        Request("www.google.com",
                runner.ACCESS_OK,
                timestamp,
                opts.userId,
                None,
                userAction='abc'))
    curRequests.append(
        Request("www.google.com",
                runner.ACCESS_OK,
                timestamp,
                opts.userId,
                None,
                userAction=3))
    curRequests.append(
        Request("www.google.com",
                runner.ACCESS_OK,
                timestamp,
                opts.userId,
                None,
                userAction=-1))

    # verify category group query works
    curRequests.append(
        Request("www.sklep-intymny.pl",
                runner.CATEGORY_BLOCKED_CUSTOM,
                timestamp,
                opts.userId,
                '1',
                userAction=UserActionCode.CATEGORY_GROUP_QUERY))
    # verifies that category group is returned even for not blocked url
    curRequests.append(
        Request("www.onet.pl",
                runner.ACCESS_OK,
                timestamp,
                opts.userId,
                '6',
                userAction=UserActionCode.CATEGORY_GROUP_QUERY))
    # test for SAF-351
    curRequests.append(
        Request("http://www.some-uncategorized-site.com",
                runner.ACCESS_OK,
                timestamp,
                opts.userId,
                None,
                userAction=UserActionCode.CATEGORY_GROUP_QUERY))
    # test for SAF-222
    curRequests.append(
        Request("http://www.some-uncategorized-site.com", runner.ACCESS_OK,
                timestamp, opts.userId, '27'))
    curRequests.append(
        Request("http://192.168.1.1", runner.ACCESS_OK, timestamp, opts.userId,
                '27'))

    # verify characters outside of ascii are properly handled
    request = Request("www.google.com/\x80abc\xaadef\xffghi",
                      runner.ACCESS_OK,
                      timestamp,
                      opts.userId,
                      None,
                      quote=False)
    request.urlInDb = "www.google.com/?abc?def?ghi"
    curRequests.append(request)

    allRequests.extend(curRequests)
    issueConcurrentRequests(opts, curRequests)

    # start backend at 18:01
    timestamp = datetime.datetime.combine(datetime.date.today(),
                                          datetime.time(18, 1))
    runner.setBackendTime(processes, timestamp)

    curRequests = []
    curRequests.append(
        Request("http://www.battle.net", runner.INTERNET_ACCESS_FORBIDDEN,
                timestamp, opts.userId, '2'))

    allRequests.extend(curRequests)
    issueConcurrentRequests(opts, curRequests)

    # start backend at 12:00
    timestamp = datetime.datetime.combine(datetime.date.today(),
                                          datetime.time(12, 0))
    runner.setBackendTime(processes, timestamp)

    curRequests = []
    curRequests.append(
        Request("http://www.battle.net", runner.UNKNOWN_USER, timestamp,
                '12345123123123', None))
    curRequests.append(
        Request("http://www.some-uncategorized-site.com", runner.ACCESS_OK,
                timestamp, opts.userId, None))

    allRequests.extend(curRequests)
    issueConcurrentRequests(opts, curRequests)

    # start backend at 13:00
    timestamp = datetime.datetime.combine(datetime.date.today(),
                                          datetime.time(13, 0))
    runner.setBackendTime(processes, timestamp)

    urls = map(
        str.strip,
        file(os.path.abspath(__file__ +
                             '/../../alexa100k.urls.txt')).readlines()
        [:NUM_REQUESTS])
    curRequests = []
    for url in urls:
        curRequests.append(Request(url, None, timestamp, opts.userId, None))

    allRequests.extend(curRequests)
    issueConcurrentRequests(opts, curRequests)

    # wait for backend to flush request log and check the log
    runner.stopBackend(processes)
    checkLogs(opts, allRequests)

    print "All tests OK"
def main():
    (opts, _) = optionParser.parseCommandLine()

    runner.startHttpd(opts, processes)

    runner.startBackend(opts, processes)
    runner.setBackendTime(processes, "2014-09-12 10:05:59")

    issueRequest(opts, "http://www.onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.ACCESS_OK)

    # FirstScenario - "simple scenario test":
    # 1. Set category Violence to onet.pl [CATEGORY_BLOCKED_CUSTOM] and category Violence to bing.pl [CATEGORY_BLOCKED_CUSTOM]
    # 2. Update onet.pl category for Internet Portals [ACCESS_OK]
    # 3. Update onet.pl category for Adult and Pornography [CATEGORY_BLOCKED_CUSTOM]
    # 4. Delete onet.pl from webroot overrides [ACCESS_OK]
    # 5. Delete bing.pl from webroot overrides [ACCESS_OK]

    runner.startWebrootOverridesScript(processes)
    runner.sendCommandToWebrootOverridesScript(
        processes,
        commandAddCategory("http://www.onet.pl", VIOLENCE) +
        commandAddCategory("bing.pl", VIOLENCE) + commandQuit())
    runner.stopWebrootOverridesScript(processes)

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://bing.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "wp.pl", runner.ACCESS_OK)

    runner.startWebrootOverridesScript(processes)
    runner.sendCommandToWebrootOverridesScript(
        processes,
        commandUpdateCategory("onet.pl", INTERNET_PORTALS) + commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://bing.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://www.wp.pl", runner.ACCESS_OK)

    runner.startWebrootOverridesScript(processes)
    runner.sendCommandToWebrootOverridesScript(
        processes,
        commandUpdateCategory("onet.pl", ADULT_AND_PORNOGRAPHY) +
        commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://bing.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "wp.pl", runner.ACCESS_OK)

    runner.startWebrootOverridesScript(processes)
    runner.sendCommandToWebrootOverridesScript(
        processes,
        commandDeleteUrl("onet.pl") + commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://bing.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "wp.pl", runner.ACCESS_OK)

    runner.startWebrootOverridesScript(processes)
    runner.sendCommandToWebrootOverridesScript(
        processes,
        commandDeleteUrl("bing.pl") + commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.ACCESS_OK)

    # SecondScenario - "lists test":
    # 1. Check categories list
    # 2. Check if overrides list is empty
    # 3. Set category Violence to onet.pl [CATEGORY_BLOCKED_CUSTOM]
    # 4. Set category Adult and Pornography to wp.pl [CATEGORY_BLOCKED_CUSTOM]
    # 5. Check if overrides list have correct values
    # 6. Delete wp.pl from webroot overrides
    # 7. Check if overrides have correct values
    # 8. Delete onet.pl from webroot overrides
    # 9. Checi if overrides have correct values

    runner.startWebrootOverridesScript(processes)
    response = runner.sendCommandToWebrootOverridesScript(
        processes,
        commandPrintCategories() + commandQuit())
    assert "CATEGORIES LIST:\nAdult and pornography\nAbused drugs\nViolence\nIllegal\nInternet Portals\n" in response[
        0]

    runner.startWebrootOverridesScript(processes)
    response = runner.sendCommandToWebrootOverridesScript(
        processes,
        commandPrintOverrides() + commandQuit())
    assert "APPLIED WEBROOT OVERRIDES LIST:\n0 Webroot Overrides Found\n" in response[
        0]

    runner.startWebrootOverridesScript(processes)
    runner.sendCommandToWebrootOverridesScript(
        processes,
        commandAddCategory("http://www.onet.pl", VIOLENCE) +
        commandAddCategory("wp.pl", ADULT_AND_PORNOGRAPHY) + commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.CATEGORY_BLOCKED_CUSTOM)

    runner.startWebrootOverridesScript(processes)
    response = runner.sendCommandToWebrootOverridesScript(
        processes,
        commandPrintOverrides() + commandQuit())
    runner.stopWebrootOverridesScript(processes)
    assert "APPLIED WEBROOT OVERRIDES LIST:\nonet.pl - Violence\nwp.pl - Adult and pornography\n" in response[
        0]

    runner.startWebrootOverridesScript(processes)
    response = runner.sendCommandToWebrootOverridesScript(
        processes,
        commandDeleteUrl("wp.pl") + commandPrintOverrides() + commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.ACCESS_OK)

    assert "APPLIED WEBROOT OVERRIDES LIST:\nonet.pl - Violence\n" in response[
        0]

    runner.startWebrootOverridesScript(processes)
    response = runner.sendCommandToWebrootOverridesScript(
        processes,
        commandDeleteUrl("onet.pl") + commandPrintOverrides() + commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.ACCESS_OK)

    assert "APPLIED WEBROOT OVERRIDES LIST:\n0 Webroot Overrides Found\n" in response[
        0]

    # ThirdScenario - "uncorrect values":
    # 1. Try to add incorrect urls to overrides table
    # 2. Try to set incorrect category for correct url
    # 3. Set category Violence to onet.pl [CATEGORY_BLOCKED_CUSTOM]
    # 4. Try to set category for onet.pl again
    # 5. Try to delete unexisting url
    # 6. Try to delete onet.pl, but reject in last step
    # 7. Try to update category of unexisting url
    # 8. Try to update onet.pl category for unexisting one
    # 9. Update onet.pl category for Internet Portals [ACCESS_OK]
    # 10.Update onet.pl category for Violence [CATEGORY_BLOCKED_CUSTOM]
    # 11. Send textbomb to script
    # 12. Delete onet.pl from webroot overrides [ACCESS_OK]

    runner.startWebrootOverridesScript(processes)
    response = runner.sendCommandToWebrootOverridesScript(
        processes, "a\n%s\n%s\n%s\n%s\n%s\n%s\n" %
        ("asda//", "aaasd::s*&*/", "onet.pl", "ASDAdasd", "Categ#$#SDF",
         VIOLENCE) + commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.ACCESS_OK)

    runner.startWebrootOverridesScript(processes)
    runner.sendCommandToWebrootOverridesScript(
        processes,
        commandAddCategory("http://www.onet.pl", INTERNET_PORTALS) +
        commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "wp.pl", runner.ACCESS_OK)

    runner.startWebrootOverridesScript(processes)
    response = runner.sendCommandToWebrootOverridesScript(
        processes, "d\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" %
        ("bing.pl", "()()()(", "VVWWV.ASDASD.!@#", "onet.pl", "a", "v", "n",
         "y") + commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.ACCESS_OK)

    runner.startWebrootOverridesScript(processes)
    response = runner.sendCommandToWebrootOverridesScript(
        processes, "u\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" %
        ("bing.pl", "()()()(", "VVWWV.ASDASD.!@#", "onet.pl", "ASDAS", "Cat",
         "y", INTERNET_PORTALS) + commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.ACCESS_OK)

    runner.startWebrootOverridesScript(processes)
    response = runner.sendCommandToWebrootOverridesScript(
        processes, "u\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" %
        ("bing.pl", "()()()(", "VVWWV.ASDASD.!@#", "onet.pl", "ASDAS", "Cat",
         "y", VIOLENCE) + commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.ACCESS_OK)

    runner.startWebrootOverridesScript(processes)
    response = runner.sendCommandToWebrootOverridesScript(
        processes,
        "%s\nd\n%s\n%s\n" % (TEXT_BOMB, "onet.pl", "y") + commandQuit())

    runner.reloadBackendOverrides(processes)
    issueRequest(opts, "http://www.onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.bing.pl", runner.ACCESS_OK)
    issueRequest(opts, "www.wp.pl", runner.ACCESS_OK)

    print "All tests OK"
Example #16
0
def main():
    (opts, _) = optionParser.parseCommandLine()

    runner.startBackend(opts, processes)
    runner.startHttpd(opts, processes)
    runner.setBackendTime(processes, "2014-04-10 08:00:00")

    #SAF-478 TEST
    issueRequest(opts, "www.onet.pl/" + ('A' * 5060), runner.ACCESS_OK)

    issueRequest(opts, "onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "www.onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "wp.pl/", runner.ACCESS_OK)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeAllSubdomainsInsert(1, "onet.pl"),
            makeProfileUrlDecision(1, True)
        ]))

    # SAF-440 (error with google.pl?safesearch=on) test
    issueRequest(opts, "onet.pl?", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl?/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl/?", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl?/?", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl?safesearch", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl/?safesearch", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl?/safesearch", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl?/?safesearch", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl?safesearch", runner.URL_BLOCKED_CUSTOM)
    # similar bug but with handling of '#' character
    issueRequest(opts, "onet.pl#top", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl#/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl/#bottom", runner.URL_BLOCKED_CUSTOM)

    issueRequest(opts, "onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "wp.pl/", runner.ACCESS_OK)
    issueRequest(opts, "m.onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "xxxxxxxxxxxxonet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "net.pl/", runner.ACCESS_OK)
    issueRequest(opts, ".pl/", runner.ACCESS_OK)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeAllSubdomainsInsert(1, "onet.pl"),
            makeAllSubdomainsInsert(2, "www.onet.pl"),
            makeProfileUrlDecision(1, True),
            makeProfileUrlDecision(2, False)
        ]))

    issueRequest(opts, "onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/",
                 runner.ACCESS_OK)  # matched by both, longer is more specific

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeAllSubdomainsInsert(1, "www.onet.pl"),
            makeAllSubdomainsInsert(2, "onet.pl"),
            makeProfileUrlDecision(1, False),
            makeProfileUrlDecision(2, True)
        ]))

    issueRequest(opts, "onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/",
                 runner.ACCESS_OK)  # matched by both, longer is more specific

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeDomainWithPathInsert(1, "www.onet.pl", "/"),
            makeProfileUrlDecision(1, True)
        ]))

    issueRequest(opts, "onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "www.onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/xxx", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "x.www.onet.pl/xxx", runner.ACCESS_OK)
    issueRequest(opts, ".www.onet.pl/xxx", runner.ACCESS_OK)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeDomainWithPathInsert(1, "www.onet.pl", "/abc/def"),
            makeProfileUrlDecision(1, True)
        ]))

    issueRequest(opts, "www.onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "www.onet.pl/xxx", runner.ACCESS_OK)
    issueRequest(opts, "www.onet.pl/abc/def", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/abc/defghi", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/abc/def/ghi", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "x.www.onet.pl/abc/def/ghi", runner.ACCESS_OK)
    issueRequest(opts, "xwww.onet.pl/abc/def/ghi", runner.ACCESS_OK)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeAllSubdomainsInsert(1, "www.onet.pl"),
            makeDomainWithPathInsert(2, "www.onet.pl", "/"),
            makeProfileUrlDecision(1, True),
            makeProfileUrlDecision(2, False)
        ]))

    issueRequest(opts, "onet.pl/", runner.ACCESS_OK)
    issueRequest(
        opts, "www.onet.pl/",
        runner.ACCESS_OK)  # matched by both, domain with path is more specific
    issueRequest(opts, "abcwww.onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "xyz.www.onet.pl/", runner.URL_BLOCKED_CUSTOM)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeDomainWithPathInsert(1, "www.onet.pl", "/"),
            makeAllSubdomainsInsert(2, "www.onet.pl"),
            makeProfileUrlDecision(1, False),
            makeProfileUrlDecision(2, True)
        ]))

    issueRequest(opts, "onet.pl/", runner.ACCESS_OK)
    issueRequest(
        opts, "www.onet.pl/",
        runner.ACCESS_OK)  # matched by both, domain with path is more specific
    issueRequest(opts, "abcwww.onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "xyz.www.onet.pl/", runner.URL_BLOCKED_CUSTOM)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeAllSubdomainsInsert(1, "www.onet.pl"),
            makeDomainWithPathInsert(2, "www.onet.pl", "/some/path"),
            makeProfileUrlDecision(1, True),
            makeProfileUrlDecision(2, False)
        ]))

    issueRequest(opts, "onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "www.onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "xyz.www.onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/some/path", runner.ACCESS_OK)
    issueRequest(opts, "xyz.www.onet.pl/some/path", runner.URL_BLOCKED_CUSTOM)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeDomainWithPathInsert(1, "www.onet.pl", "/some/p"),
            makeDomainWithPathInsert(2, "www.onet.pl", "/some/path"),
            makeProfileUrlDecision(1, True),
            makeProfileUrlDecision(2, False)
        ]))

    issueRequest(opts, "www.onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "www.onet.pl/some/p", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/some/pa", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/some/pat", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/some/path", runner.ACCESS_OK)
    issueRequest(opts, "www.onet.pl/some/pathx", runner.ACCESS_OK)
    issueRequest(opts, "www.onet.pl/some/path/", runner.ACCESS_OK)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeDomainWithPathInsert(1, "www.onet.pl", "/some/path"),
            makeDomainWithPathInsert(2, "www.onet.pl", "/some/p"),
            makeProfileUrlDecision(1, False),
            makeProfileUrlDecision(2, True)
        ]))

    issueRequest(opts, "www.onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "www.onet.pl/some/p", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/some/pa", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/some/pat", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/some/path", runner.ACCESS_OK)
    issueRequest(opts, "www.onet.pl/some/pathx", runner.ACCESS_OK)
    issueRequest(opts, "www.onet.pl/some/path/", runner.ACCESS_OK)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeAllSubdomainsInsert(1, "onet.pl"),
            makeDomainWithPathInsert(2, "www.onet.pl", "/some/"),
            makeDomainWithPathInsert(3, "www.onet.pl", "/"),
            makeProfileUrlDecision(1, True),
            makeProfileUrlDecision(2, False),
            makeProfileUrlDecision(3, True)
        ]))

    issueRequest(opts, "onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/some/", runner.ACCESS_OK)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeDomainWithPathInsert(1, "www.onet.pl", "/"),
            makeDomainWithPathInsert(2, "onet.pl", "/"),
            makeProfileUrlDecision(1, False),
            makeProfileUrlDecision(2, True)
        ]))

    issueRequest(opts, "onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "asdasdadasasdasddas.www.onet.pl/", runner.ACCESS_OK)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeDomainWithPathInsert(1, "www.onet.pl", "/"),
            makeDomainWithPathInsert(2, "onet.pl", "/"),
            makeAllSubdomainsInsert(3, "www.onet.pl"),
            makeProfileUrlDecision(1, False),
            makeProfileUrlDecision(2, True),
            makeProfileUrlDecision(3, True)
        ]))

    issueRequest(opts, "onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "www.onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "asdasdadasasdasddas.www.onet.pl/",
                 runner.URL_BLOCKED_CUSTOM)

    runner.issueSqlCommand(
        opts,
        ';'.join([
            makeDeletes(),
            "insert into profile_categories_groups_list (id, code_profile_id, code_categories_groups_id, blocked) values \
(2, 20, 46, true)"
            # (46,'Portale');
        ]))
    issueRequest(opts, "onet.pl/", runner.CATEGORY_BLOCKED_CUSTOM)
    runner.issueSqlCommand(
        opts, ';'.join([
            makeAllSubdomainsInsert(1, "onet.pl"),
            makeProfileUrlDecision(1, False)
        ]))
    issueRequest(
        opts, "onet.pl/", runner.ACCESS_OK
    )  # url pattern allow decision overrides category group deny decision
    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            "update profile_categories_groups_list set blocked=false where id=2"
        ]))
    issueRequest(opts, "onet.pl/", runner.ACCESS_OK)
    runner.issueSqlCommand(
        opts, ';'.join([
            makeAllSubdomainsInsert(1, "onet.pl"),
            makeProfileUrlDecision(1, True)
        ]))
    issueRequest(
        opts, "onet.pl/", runner.URL_BLOCKED_CUSTOM
    )  # url pattern deny decision overrides category group allow decision
    runner.issueSqlCommand(
        opts, ';'.join([
            "update profile_categories_groups_list set blocked=true where id=2"
        ]))
    issueRequest(
        opts, "onet.pl/", runner.URL_BLOCKED_CUSTOM
    )  # still URL_BLOCKED_CUSTOM although also blocked by category group

    print "All tests OK"
def main():
    (opts, _) = optionParser.parseCommandLine()

    runner.loadSqlScript(
        opts,
        runner.getRepoDir() + '/sql/test_scenario1_inserts.sql')

    runner.startBackend(opts, processes)
    runner.startHttpd(opts, processes)

    runner.setBackendTime(processes, "2014-04-10 08:00:00")
    issueRequest(opts, "wp.pl", runner.ACCESS_OK)
    issueRequest(opts, "onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "http://onet.pl/abc", runner.ACCESS_OK)
    issueRequest(opts, "http://onet.pl/abc:123", runner.ACCESS_OK)
    issueRequest(opts, "http://onet.pl/abc:123?../../..", runner.ACCESS_OK)
    issueRequest(opts, "onet.pl/..", runner.ACCESS_OK)
    issueRequest(opts, "onet.pl/../wp.pl", runner.ACCESS_OK)
    issueRequest(opts, "onet.pl/../wp.pl:123", runner.ACCESS_OK)
    issueRequest(opts, "onet.pl:123/../wp.pl", runner.ACCESS_OK)
    issueRequest(opts, "onet.pl:123/?../wp.pl", runner.ACCESS_OK)
    issueRequest(opts, "onet.pl:123/?../?wp.pl", runner.ACCESS_OK)
    issueRequest(opts, "ONET.PL/", runner.ACCESS_OK)
    issueRequest(opts, "ONET.PL", runner.ACCESS_OK)
    issueRequest(opts, "HTTP://ONET.PL", runner.ACCESS_OK)
    issueRequest(opts, "HTTP://ONET.PL/", runner.ACCESS_OK)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeAllSubdomainsInsert(1, "onet.pl"),
            makeProfileUrlDecision(1, True)
        ]))

    issueRequest(opts, "wp.pl", runner.ACCESS_OK)
    issueRequest(opts, "onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/abc", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/abc:123", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/abc:123?../../..",
                 runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl/..", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl/../wp.pl", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl/../wp.pl:123", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl:123/../wp.pl", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl:123/?../wp.pl", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl:123/?../?wp.pl", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "ONET.PL/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "ONET.PL", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "HTTP://ONET.PL", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "HTTP://ONET.PL/", runner.URL_BLOCKED_CUSTOM)

    runner.issueSqlCommand(
        opts,
        ';'.join([
            makeDeletes(),
            "insert into profile_categories_groups_list (id, code_profile_id, code_categories_groups_id, blocked) values \
(2, 20, 46, true)"
            # (46,'Portale');
        ]))

    issueRequest(opts, "onet.pl/", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/abc", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/abc:123",
                 runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/abc:123?../../..",
                 runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl/..", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl/../wp.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl/../wp.pl:123", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl:123/../wp.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl:123/?../wp.pl", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl:123/?../?wp.pl",
                 runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "ONET.PL/", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "ONET.PL", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "HTTP://ONET.PL", runner.CATEGORY_BLOCKED_CUSTOM)
    issueRequest(opts, "HTTP://ONET.PL/", runner.CATEGORY_BLOCKED_CUSTOM)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            "delete from profile_categories_groups_list where id=2",
            makeDomainWithPathInsert(1, "onet.pl", "/"),
            makeProfileUrlDecision(1, True)
        ]))

    issueRequest(opts, "wp.pl", runner.ACCESS_OK)
    issueRequest(opts, "onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/abc", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/abc:123", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/abc:123?../../..",
                 runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl/..", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl/../wp.pl", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl/../wp.pl:123", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl:123/../wp.pl", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl:123/?../wp.pl", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "onet.pl:123/?../?wp.pl", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "ONET.PL/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "ONET.PL", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "HTTP://ONET.PL", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "HTTP://ONET.PL/", runner.URL_BLOCKED_CUSTOM)

    issueRequest(opts, "HtTP://OnET.PL/", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/%2E%2E/wp.pl",
                 runner.URL_BLOCKED_CUSTOM)

    # malformed urls
    issueRequest(opts, "http://", runner.ACCESS_OK)
    issueRequest(opts, "http:/", runner.ACCESS_OK)
    issueRequest(opts, ":/", runner.ACCESS_OK)
    issueRequest(opts, "://", runner.ACCESS_OK)
    issueRequest(opts, "/", runner.ACCESS_OK)
    issueRequest(opts, ":", runner.ACCESS_OK)
    issueRequest(opts, "?", runner.ACCESS_OK)
    issueRequest(opts, "abc/", runner.ACCESS_OK)
    issueRequest(opts, "abc/%00/aaa", runner.ACCESS_OK)
    issueRequest(opts, "abc/%0/", runner.ACCESS_OK)
    issueRequest(opts, "abc/%0", runner.ACCESS_OK)
    issueRequest(opts, "abc/%", runner.ACCESS_OK)
    issueRequest(opts, "abc/%ff",
                 runner.ACCESS_OK)  # not malformed but corner-case
    issueRequest(opts, "abc/%g0", runner.ACCESS_OK)
    issueRequest(opts, "abc/%0g", runner.ACCESS_OK)
    issueRequest(opts, "abc/%G0", runner.ACCESS_OK)
    issueRequest(opts, "abc/%0G", runner.ACCESS_OK)

    runner.issueSqlCommand(
        opts, ';'.join([
            makeDeletes(),
            makeDomainWithPathInsert(1, "onet.pl", "/abc"),
            makeProfileUrlDecision(1, True)
        ]))

    issueRequest(opts, "http://onet.pl/", runner.ACCESS_OK)
    issueRequest(opts, "http://onet.pl/abc", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/xyz/../abc", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl//abc", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/./abc", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/../abc", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/Abc", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/%41bc", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/%61bc", runner.URL_BLOCKED_CUSTOM)
    issueRequest(opts, "http://onet.pl/%42bc", runner.ACCESS_OK)
    issueRequest(opts, "http://onet.pl/%62bc", runner.ACCESS_OK)

    print "All tests OK"
Example #18
0
def main():
    (opts, _) = optionParser.parseCommandLine()

    try:
        os.remove(STATS_FILE)
    except OSError:
        pass

    runner.issueSqlCommand(
        opts,
        "insert into webroot_overrides (id, address, code_categories_ext_id) values (1, 'onet.pl', 10)"
    )

    opts.statisticsPrintingInterval = 10  # seconds
    opts.statisticsFilePath = STATS_FILE

    runner.startBackend(opts, processes)
    runner.startHttpd(opts, processes)

    runner.setBackendTime(processes, "2014-03-21 15:31:00")

    issueRequest(opts, "192.168.0.1", runner.ACCESS_OK)
    issueRequest(opts, "127.0.0.1", runner.ACCESS_OK)
    issueRequest(opts, "10.1.2.3", runner.ACCESS_OK)
    issueRequest(opts, "http://onet.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.google.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.safekiddo.pl", runner.ACCESS_OK)
    issueRequest(opts, "http://www.safekiddo.com", runner.ACCESS_OK)
    issueRequest(opts, "http://www.safekiddo.com", runner.ACCESS_OK)
    issueRequest(opts, "http://www.safekiddo.com", runner.ACCESS_OK)
    issueRequest(opts, "http://www.safekiddo.com", runner.ACCESS_OK)
    time.sleep(opts.statisticsPrintingInterval + 1)

    runner.stopBackend(processes)

    regexList = [
        LAST_VALUE_PATTERN % ("bcBcapMap", 2),
        MAXIMUM_VALUE_PATTERN % ("bcBcapMap", 2),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("bcBcapMap", 2),
        LAST_VALUE_PATTERN % ("bcRtuMap", ""),
        MAXIMUM_VALUE_PATTERN % ("bcRtuMap", ""),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("bcRtuMap", ""),
        LAST_VALUE_PATTERN % ("unset", ""),
        MAXIMUM_VALUE_PATTERN % ("unset", ""),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("unset", ""),
        LAST_VALUE_PATTERN % ("localIp", 3),
        MAXIMUM_VALUE_PATTERN % ("localIp", 3),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("localIp", 3),
        LAST_VALUE_PATTERN % ("rtuCache", ""),
        MAXIMUM_VALUE_PATTERN % ("rtuCache", ""),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("rtuCache", ""),
        LAST_VALUE_PATTERN % ("localDb", 1),
        MAXIMUM_VALUE_PATTERN % ("localDb", 1),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("localDb", 1),
        LAST_VALUE_PATTERN % ("bcap", 2),
        MAXIMUM_VALUE_PATTERN % ("bcap", 2),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("bcap", 2),
        LAST_VALUE_PATTERN % ("bcapCache", 3),
        MAXIMUM_VALUE_PATTERN % ("bcapCache", 3),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("bcapCache", 3),

        #Testing newValueSinceLastPrinting
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("bcBcapMap", 0),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("bcRtuMap", 0),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("unset", 0),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("localIp", 0),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("rtuCache", 0),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("localDb", 0),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("bcap", 0),
        DIFFERENCE_SINCE_LAST_PRINTING_PATTERN % ("bcapCache", 0),
    ]

    checkRegexList(regexList)

    print "All tests OK"
def main():
    (opts, _) = optionParser.parseCommandLine()

    requestLoggerMaxQueueLength = 10000

    if opts.dbHost == "" or opts.dbHost == "localhost":
        print "db is local"
        numUrls = 15000
    else:
        print "db is remote"
        numUrls = 1000

    print "Overriding logLevel to INFO"
    opts.logLevel = 'INFO'

    optsOriginal = copy.copy(opts)
    opts.dbHostLogs = 'localhost'
    opts.dbPortLogs = optsOriginal.tunnelPort

    tunnelHost = optsOriginal.dbHostLogs
    if tunnelHost is None:
        tunnelHost = optsOriginal.dbHost

    tunnelPort = optsOriginal.dbPortLogs
    if tunnelPort is None:
        tunnelPort = optsOriginal.dbPort

    runner.startTunnel(opts, processes, tunnelHost, tunnelPort)

    # The test verifies that backend performance is not degraded in case logs db becomes sluggish.
    # Cloud access is too slow for this test.
    backendArgs = [
        '--requestLoggerMaxQueueLength',
        str(requestLoggerMaxQueueLength), '--setCurrentTime',
        '"2014-03-21 15:31:00"', '--disableWebrootCloud'
    ]
    runner.startBackend(opts, processes, backendArgs)

    # Backend should now have loaded webroot local db, but a connection pool to logs db is created
    # after webroot gets initialized.
    time.sleep(3)
    # Suspend connections to logs db.
    runner.suspendProcess('tunnel', processes)

    httpdArgs = [
        "--connectionPerIpLimitAbsolute",
        str(numUrls),
        "--connectionPerIpLimitSlowDown",
        str(numUrls),
    ]
    runner.startHttpd(opts, processes, httpdArgs)

    urls = file(
        os.path.abspath(__file__ +
                        '/../../alexa100k.urls.txt')).readlines()[:numUrls]

    duration = issueRequests(opts, urls)
    performance = len(urls) / duration
    print 'reqs/s: %.1f (duration %.2f, urls %d)' % (performance, duration,
                                                     len(urls))

    assert performance > 400, "Performance is lower than expected"

    output = open("performance.txt", "w")
    output.write(str(performance))
    output.close()