Skip to content

Commit

Permalink
Fix randomly failing unittest like https://circleci.com/gh/andresrian…
Browse files Browse the repository at this point in the history
  • Loading branch information
andresriancho committed Mar 17, 2015
1 parent 0dfb894 commit 058c3eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 13 additions & 1 deletion w3af/core/data/url/tests/helpers/ssl_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import SocketServer
import threading
import socket
import time
import ssl
import os

Expand Down Expand Up @@ -70,6 +71,7 @@ def __init__(self, listen, port, certfile, proto=ssl.PROTOCOL_SSLv3,
self.cert = certfile
self.proto = proto
self.http_response = http_response

self.sock = socket.socket()
self.sock.bind((listen, port))
self.sock.listen(5)
Expand Down Expand Up @@ -116,4 +118,14 @@ def stop(self):
s.connect((self.listen, self.port))
s.close()
except:
pass
pass

def wait_for_start(self):
while self.get_port() is None:
time.sleep(0.5)

def get_port(self):
try:
return self.sock.getsockname()[1]
except:
return None
13 changes: 7 additions & 6 deletions w3af/plugins/tests/audit/test_ssl_certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@
from w3af import ROOT_PATH
from w3af.plugins.tests.helper import PluginTest, PluginConfig
from w3af.core.data.url.tests.helpers.ssl_daemon import SSLServer
from w3af.core.controllers.misc.get_unused_port import get_unused_port


class TestSSLCertificate(PluginTest):

PORT = get_unused_port()
local_target_url = 'https://localhost:%s/' % PORT
local_target_url = 'https://localhost:%s/'

remote_url = 'https://www.yandex.com/'
EXPECTED_STRINGS = ('yandex.ru', 'Moscow', 'RU', 'yandex')
Expand All @@ -49,11 +47,14 @@ def test_ssl_certificate_local(self):
# Start the HTTPS server
certfile = os.path.join(ROOT_PATH, 'plugins', 'tests', 'audit',
'certs', 'invalid_cert.pem')
s = SSLServer('localhost', self.PORT, certfile)
s = SSLServer('localhost', 0, certfile)
s.start()
s.wait_for_start()

port = s.get_port()

cfg = self._run_configs['cfg']
self._scan(self.local_target_url, cfg['plugins'])
self._scan(self.local_target_url % port, cfg['plugins'])

s.stop()

Expand All @@ -67,7 +68,7 @@ def test_ssl_certificate_local(self):
# Now some tests around specific details of the found vuln
vuln = vulns[0]
self.assertEquals('Invalid SSL certificate', vuln.get_name())
self.assertEquals(self.local_target_url, str(vuln.get_url()))
self.assertEquals(self.local_target_url % port, str(vuln.get_url()))

@attr('internet')
def test_ssl_certificate_yandex(self):
Expand Down

0 comments on commit 058c3eb

Please sign in to comment.