home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / arm / test.py < prev    next >
Encoding:
Python Source  |  2012-05-18  |  4.7 KB  |  145 lines

  1. #!/usr/bin/env python
  2.  
  3. """
  4. Handler for arm tests and demos.
  5. """
  6.  
  7. import time
  8. from util import connections, torTools, uiTools
  9.  
  10. MENU = """Arm Test Options:
  11.   1. Resolver Performance Test
  12.   2. Resolver Dump
  13.   3. Glyph Demo
  14.   4. Exit Policy Check
  15.   q. Quit
  16.  
  17. Selection: """
  18.  
  19. def printDivider():
  20.   print("\n" + "-" * 40 + "\n")
  21.  
  22. conn = None
  23. while True:
  24.   userInput = raw_input(MENU)
  25.   
  26.   # initiate the TorCtl connection if the test needs it
  27.   if userInput in ("1", "2", "4") and not conn:
  28.     conn = torTools.getConn()
  29.     conn.init()
  30.     
  31.     # prefetch pid so extra system calls don't effect the timing of tests
  32.     conn.getMyPid()
  33.   
  34.   if userInput == "q":
  35.     break # quit test script
  36.   elif userInput == "1":
  37.     systemResolvers = connections.getSystemResolvers()
  38.     printDivider()
  39.     
  40.     allConnectionResults = []
  41.     for resolver in systemResolvers:
  42.       startTime = time.time()
  43.       connectionResults = connections.getConnections(resolver, "tor", conn.getMyPid())
  44.       connectionResults.sort()
  45.       allConnectionResults.append(connectionResults)
  46.       
  47.       resolverLabel = "%-10s" % resolver
  48.       countLabel = "%4i results" % len(connectionResults)
  49.       timeLabel = "%0.4f seconds" % (time.time() - startTime)
  50.       print "%s %s     %s" % (resolverLabel, countLabel, timeLabel)
  51.     
  52.     allResolversMatch = True
  53.     firstResult = allConnectionResults.pop()
  54.     while allConnectionResults:
  55.       if allConnectionResults.pop() != firstResult:
  56.         allResolversMatch = False
  57.         break
  58.     
  59.     if allResolversMatch:
  60.       print("\nThe results of all the connection resolvers match")
  61.     else:
  62.       print("\nWarning: Connection resolver results differ")
  63.     
  64.     printDivider()
  65.   elif userInput == "2":
  66.     # use the given resolver to fetch tor's connections
  67.     while True:
  68.       # provide the selection options
  69.       printDivider()
  70.       print("Select a resolver:")
  71.       availableResolvers = connections.Resolver.values()
  72.       for i in range(len(availableResolvers)):
  73.         print("  %i. %s" % (i, availableResolvers[i]))
  74.       print("  q. Go back to the main menu")
  75.       
  76.       userSelection = raw_input("\nSelection: ")
  77.       if userSelection == "q":
  78.         printDivider()
  79.         break
  80.       
  81.       if userSelection.isdigit() and int(userSelection) in range(0, 7):
  82.         try:
  83.           resolver = connections.Resolver.values()[int(userSelection)]
  84.           startTime = time.time()
  85.           
  86.           print(connections.getResolverCommand(resolver, "tor", conn.getMyPid()))
  87.           connectionResults = connections.getConnections(resolver, "tor", conn.getMyPid())
  88.           connectionResults.sort()
  89.           
  90.           # prints results
  91.           printDivider()
  92.           for lIp, lPort, fIp, fPort in connectionResults:
  93.             print("  %s:%s -> %s:%s" % (lIp, lPort, fIp, fPort))
  94.           
  95.           print("\n  Runtime: %0.4f seconds" % (time.time() - startTime))
  96.         except (IOError, IndexError), exc:
  97.           print exc
  98.       else:
  99.         print("'%s' isn't a valid selection\n" % userSelection)
  100.   elif userInput == "3":
  101.     uiTools.demoGlyphs()
  102.     
  103.     # Switching to a curses context and back repeatedly seems to screw up the
  104.     # terminal. Just to be safe this ends the process after the demo.
  105.     break
  106.   elif userInput == "4":
  107.     # display the current exit policy and query if destinations are allowed by it
  108.     exitPolicy = conn.getExitPolicy()
  109.     print("Exit Policy: %s" % exitPolicy)
  110.     printDivider()
  111.     
  112.     while True:
  113.       # provide the selection options
  114.       userSelection = raw_input("\nCheck if destination is allowed (q to go back): ")
  115.       userSelection = userSelection.replace(" ", "").strip() # removes all whitespace
  116.       
  117.       isValidQuery, isExitAllowed = True, False
  118.       if userSelection == "q":
  119.         printDivider()
  120.         break
  121.       elif connections.isValidIpAddress(userSelection):
  122.         # just an ip address (use port 80)
  123.         isExitAllowed = exitPolicy.check(userSelection, 80)
  124.       elif userSelection.isdigit():
  125.         # just a port (use a common ip like 4.2.2.2)
  126.         isExitAllowed = exitPolicy.check("4.2.2.2", userSelection)
  127.       elif ":" in userSelection:
  128.         # ip/port combination
  129.         ipAddr, port = userSelection.split(":", 1)
  130.         
  131.         if connections.isValidIpAddress(ipAddr) and port.isdigit():
  132.           isExitAllowed = exitPolicy.check(ipAddr, port)
  133.         else: isValidQuery = False
  134.       else: isValidQuery = False # invalid input
  135.       
  136.       if isValidQuery:
  137.         resultStr = "is" if isExitAllowed else "is *not*"
  138.         print("Exiting %s allowed to that destination" % resultStr)
  139.       else:
  140.         print("'%s' isn't a valid destination (should be an ip, port, or ip:port)\n" % userSelection)
  141.     
  142.   else:
  143.     print("'%s' isn't a valid selection\n" % userInput)
  144.  
  145.