runner.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #
  2. # Copyright 2012 Square Inc.
  3. # Portions Copyright (c) 2016-present, Facebook, Inc.
  4. # All rights reserved.
  5. #
  6. # This source code is licensed under the license found in the
  7. # LICENSE-examples file in the root directory of this source tree.
  8. #
  9. import argparse
  10. import json
  11. import sys
  12. import subprocess
  13. import urlparse
  14. from twisted.python import log
  15. from twisted.internet import reactor
  16. from twisted.web.server import Site
  17. from twisted.web.static import File
  18. from autobahntestsuite.fuzzing import FuzzingServerFactory
  19. from autobahn.twisted.websocket import listenWS
  20. class jsondict(dict):
  21. def __init__(self, json_value):
  22. if isinstance(json_value, dict):
  23. dict.__init__(self, json_value)
  24. else:
  25. dict.__init__(self, json.loads(json_value))
  26. def append(self, other):
  27. self.update(jsondict(other))
  28. def __repr__(self):
  29. return "'%s'" % json.dumps(self)
  30. def parse_opts(s):
  31. return dict(s.split('='))
  32. def main():
  33. parser = argparse.ArgumentParser()
  34. parser.add_argument('-u', '--url', default='ws://localhost:9001', help='Listen URL [default: %(default)s]')
  35. parser.add_argument('-O', '--options', default=jsondict({'failByDrop':False}), type=jsondict, action='append', help='extra options (overwrites existing) [default: %(default)s]')
  36. parser.add_argument('-p', '--webport', default=9090, type=int)
  37. parser.add_argument('-i', '--listen-interface', default='localhost', help='interface to listen on')
  38. parser.add_argument('-w', '--webdir', default='.')
  39. parser.add_argument('-d', '--debug', default=False, action='store_true', help='Debug Mode [default: %(default)s]')
  40. parser.add_argument('-o', '--outdir', default='./pages/results', metavar='DIR', help='Output Directory [default: %(default)s]')
  41. parser.add_argument('-c', '--cases', default=['*'], nargs='+', help='test cases [default: %(default)s]')
  42. parser.add_argument('-x', '--exclude-cases', default=[], nargs='+', help='test cases to exclude [default: %(default)s]')
  43. parser.add_argument('-l', '--logfile', type=argparse.FileType('w'), default=sys.stdout, help='logging file [default: stdout]')
  44. parser.add_argument('-t', '--exit-timeout', metavar='SECONDS', default=None, type=float, help='Will automatically exit after %(metavar)s seconds [default: %(default)s]')
  45. args = parser.parse_args()
  46. spec = args.__dict__
  47. log.startLogging(args.logfile)
  48. ## fuzzing server
  49. fuzzer = FuzzingServerFactory(spec)
  50. listenWS(fuzzer, interface=args.listen_interface)
  51. ## web server
  52. webdir = File(args.webdir)
  53. web = Site(webdir)
  54. reactor.listenTCP(args.webport, web, interface=args.listen_interface)
  55. log.msg("Using Twisted reactor class %s" % str(reactor.__class__))
  56. if args.exit_timeout:
  57. def exit_callback():
  58. log.msg("Exiting due to timeout (--exit-timeout/-t)")
  59. reactor.fireSystemEvent('shutdown')
  60. #reactor.stop()
  61. sys.exit(12)
  62. reactor.callLater(args.exit_timeout, exit_callback)
  63. reactor.run()