DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (4a108e94d3e2)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

"""
Runs the Mochitest test harness.
"""

from __future__ import with_statement
import os
import sys
SCRIPT_DIR = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))
sys.path.insert(0, SCRIPT_DIR)

from urlparse import urlparse
import ctypes
import glob
import json
import mozcrash
import mozdebug
import mozinfo
import mozprocess
import mozrunner
import optparse
import re
import shutil
import signal
import subprocess
import sys
import tempfile
import time
import traceback
import urllib2
import zipfile
import bisection

from automationutils import (
    environment,
    processLeakLog,
    dumpScreen,
    ShutdownLeaks,
    printstatus,
    LSANLeaks,
    setAutomationLog,
)

from datetime import datetime
from manifestparser import TestManifest
from manifestparser.filters import subsuite
from mochitest_options import MochitestOptions
from mozprofile import Profile, Preferences
from mozprofile.permissions import ServerLocations
from urllib import quote_plus as encodeURIComponent
from mozlog.structured.formatters import TbplFormatter
from mozlog.structured import commandline

# This should use the `which` module already in tree, but it is
# not yet present in the mozharness environment
from mozrunner.utils import findInPath as which

###########################
# Option for NSPR logging #
###########################

# Set the desired log modules you want an NSPR log be produced by a try run for, or leave blank to disable the feature.
# This will be passed to NSPR_LOG_MODULES environment variable. Try run will then put a download link for the log file
# on tbpl.mozilla.org.

NSPR_LOG_MODULES = ""

####################
# LOG HANDLING     #
####################

# output processing


class MochitestFormatter(TbplFormatter):

    """
    The purpose of this class is to maintain compatibility with legacy users.
    Mozharness' summary parser expects the count prefix, and others expect python
    logging to contain a line prefix picked up by TBPL (bug 1043420).
    Those directly logging "TEST-UNEXPECTED" require no prefix to log output
    in order to turn a build orange (bug 1044206).

    Once updates are propagated to Mozharness, this class may be removed.
    """
    log_num = 0

    def __init__(self):
        super(MochitestFormatter, self).__init__()

    def __call__(self, data):
        output = super(MochitestFormatter, self).__call__(data)
        log_level = data.get('level', 'info').upper()

        if 'js_source' in data or log_level == 'ERROR':
            data.pop('js_source', None)
            output = '%d %s %s' % (
                MochitestFormatter.log_num, log_level, output)
            MochitestFormatter.log_num += 1

        return output

# output processing


class MessageLogger(object):

    """File-like object for logging messages (structured logs)"""
    BUFFERING_THRESHOLD = 100
    # This is a delimiter used by the JS side to avoid logs interleaving
    DELIMITER = u'\ue175\uee31\u2c32\uacbf'
    BUFFERED_ACTIONS = set(['test_status', 'log'])
    VALID_ACTIONS = set(['suite_start', 'suite_end', 'test_start', 'test_end',
                         'test_status', 'log',
                         'buffering_on', 'buffering_off'])
    TEST_PATH_PREFIXES = ['/tests/',
                          'chrome://mochitests/content/browser/',
                          'chrome://mochitests/content/chrome/']

    def __init__(self, logger, buffering=True):
        self.logger = logger
        self.buffering = buffering
        self.restore_buffering = False
        self.tests_started = False

        # Message buffering
        self.buffered_messages = []

        # Failures reporting, after the end of the tests execution
        self.errors = []

    def valid_message(self, obj):
        """True if the given object is a valid structured message (only does a superficial validation)"""
        return isinstance(obj, dict) and 'action' in obj and obj[
            'action'] in MessageLogger.VALID_ACTIONS

    def _fix_test_name(self, message):
        """Normalize a logged test path to match the relative path from the sourcedir.
        """
        if 'test' in message:
            test = message['test']
            for prefix in MessageLogger.TEST_PATH_PREFIXES:
                if test.startswith(prefix):
                    message['test'] = test[len(prefix):]
                    break

    def parse_line(self, line):
        """Takes a given line of input (structured or not) and returns a list of structured messages"""
        line = line.rstrip().decode("UTF-8", "replace")

        messages = []
        for fragment in line.split(MessageLogger.DELIMITER):
            if not fragment:
                continue
            try:
                message = json.loads(fragment)
                if not self.valid_message(message):
                    message = dict(
                        action='log',
                        level='info',
                        message=fragment,
                        unstructured=True)
            except ValueError:
                message = dict(
                    action='log',
                    level='info',
                    message=fragment,
                    unstructured=True)
            self._fix_test_name(message)
            messages.append(message)

        return messages

    def process_message(self, message):
        """Processes a structured message. Takes into account buffering, errors, ..."""
        if not self.tests_started and message['action'] == 'test_start':
            self.tests_started = True

        # Activation/deactivating message buffering from the JS side
        if message['action'] == 'buffering_on':
            self.buffering = True
            return
        if message['action'] == 'buffering_off':
            self.buffering = False
            return

        unstructured = False
        if 'unstructured' in message:
            unstructured = True
            message.pop('unstructured')

        # Error detection also supports "raw" errors (in log messages) because some tests
        # manually dump 'TEST-UNEXPECTED-FAIL'.
        if ('expected' in message or (message['action'] == 'log' and message[
                'message'].startswith('TEST-UNEXPECTED'))):
            # Saving errors/failures to be shown at the end of the test run
            self.errors.append(message)
            self.restore_buffering = self.restore_buffering or self.buffering
            self.buffering = False
            if self.buffered_messages:
                snipped = len(
                    self.buffered_messages) - self.BUFFERING_THRESHOLD
                if snipped > 0:
                    self.logger.info(
                        "<snipped {0} output lines - "
                        "if you need more context, please use "
                        "SimpleTest.requestCompleteLog() in your test>" .format(snipped))
                # Dumping previously buffered messages
                self.dump_buffered(limit=True)

            # Logging the error message
            self.logger.log_raw(message)
        # If we don't do any buffering, or the tests haven't started, or the message was
        # unstructured, it is directly logged.
        elif any([not self.buffering,
                  unstructured,
                  not self.tests_started,
                  message['action'] not in self.BUFFERED_ACTIONS]):
            self.logger.log_raw(message)
        else:
            # Buffering the message
            self.buffered_messages.append(message)

        # If a test ended, we clean the buffer
        if message['action'] == 'test_end':
            self.buffered_messages = []
            if self.restore_buffering:
                self.restore_buffering = False
                self.buffering = True

    def write(self, line):
        messages = self.parse_line(line)
        for message in messages:
            self.process_message(message)
        return messages

    def flush(self):
        sys.stdout.flush()

    def dump_buffered(self, limit=False):
        if limit:
            dumped_messages = self.buffered_messages[-
                                                     self.BUFFERING_THRESHOLD:]
        else:
            dumped_messages = self.buffered_messages

        for buf_msg in dumped_messages:
            self.logger.log_raw(buf_msg)
        # Cleaning the list of buffered messages
        self.buffered_messages = []

    def finish(self):
        self.dump_buffered()
        self.buffering = False
        self.logger.suite_end()

####################
# PROCESS HANDLING #
####################


def call(*args, **kwargs):
    """front-end function to mozprocess.ProcessHandler"""
    # TODO: upstream -> mozprocess
    # https://bugzilla.mozilla.org/show_bug.cgi?id=791383
    process = mozprocess.ProcessHandler(*args, **kwargs)
    process.run()
    return process.wait()


def killPid(pid, log):
    # see also https://bugzilla.mozilla.org/show_bug.cgi?id=911249#c58
    try:
        os.kill(pid, getattr(signal, "SIGKILL", signal.SIGTERM))
    except Exception as e:
        log.info("Failed to kill process %d: %s" % (pid, str(e)))

if mozinfo.isWin:
    import ctypes.wintypes

    def isPidAlive(pid):
        STILL_ACTIVE = 259
        PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
        pHandle = ctypes.windll.kernel32.OpenProcess(
            PROCESS_QUERY_LIMITED_INFORMATION,
            0,
            pid)
        if not pHandle:
            return False
        pExitCode = ctypes.wintypes.DWORD()
        ctypes.windll.kernel32.GetExitCodeProcess(
            pHandle,
            ctypes.byref(pExitCode))
        ctypes.windll.kernel32.CloseHandle(pHandle)
        return pExitCode.value == STILL_ACTIVE

else:
    import errno

    def isPidAlive(pid):
        try:
            # kill(pid, 0) checks for a valid PID without actually sending a signal
            # The method throws OSError if the PID is invalid, which we catch
            # below.
            os.kill(pid, 0)

            # Wait on it to see if it's a zombie. This can throw OSError.ECHILD if
            # the process terminates before we get to this point.
            wpid, wstatus = os.waitpid(pid, os.WNOHANG)
            return wpid == 0
        except OSError as err:
            # Catch the errors we might expect from os.kill/os.waitpid,
            # and re-raise any others
            if err.errno == errno.ESRCH or err.errno == errno.ECHILD:
                return False
            raise
# TODO: ^ upstream isPidAlive to mozprocess

#######################
# HTTP SERVER SUPPORT #
#######################


class MochitestServer(object):

    "Web server used to serve Mochitests, for closer fidelity to the real web."

    def __init__(self, options, logger):
        if isinstance(options, optparse.Values):
            options = vars(options)
        self._log = logger
        self._closeWhenDone = options['closeWhenDone']
        self._utilityPath = options['utilityPath']
        self._xrePath = options['xrePath']
        self._profileDir = options['profilePath']
        self.webServer = options['webServer']
        self.httpPort = options['httpPort']
        self.shutdownURL = "http://%(server)s:%(port)s/server/shutdown" % {
            "server": self.webServer,
            "port": self.httpPort}
        self.testPrefix = "'webapprt_'" if options.get(
            'webapprtContent') else "undefined"

        if options.get('httpdPath'):
            self._httpdPath = options['httpdPath']
        else:
            self._httpdPath = SCRIPT_DIR
        self._httpdPath = os.path.abspath(self._httpdPath)

    def start(self):
        "Run the Mochitest server, returning the process ID of the server."

        # get testing environment
        env = environment(xrePath=self._xrePath)
        env["XPCOM_DEBUG_BREAK"] = "warn"
        env["LD_LIBRARY_PATH"] = self._xrePath

        # When running with an ASan build, our xpcshell server will also be ASan-enabled,
        # thus consuming too much resources when running together with the browser on
        # the test slaves. Try to limit the amount of resources by disabling certain
        # features.
        env["ASAN_OPTIONS"] = "quarantine_size=1:redzone=32:malloc_context_size=5"

        if mozinfo.isWin:
            env["PATH"] = env["PATH"] + ";" + str(self._xrePath)

        args = [
            "-g",
            self._xrePath,
            "-v",
            "170",
            "-f",
            os.path.join(
                self._httpdPath,
                "httpd.js"),
            "-e",
            """const _PROFILE_PATH = '%(profile)s'; const _SERVER_PORT = '%(port)s'; const _SERVER_ADDR = '%(server)s'; const _TEST_PREFIX = %(testPrefix)s; const _DISPLAY_RESULTS = %(displayResults)s;""" % {
                "profile": self._profileDir.replace(
                    '\\',
                    '\\\\'),
                "port": self.httpPort,
                "server": self.webServer,
                "testPrefix": self.testPrefix,
                "displayResults": str(
                    not self._closeWhenDone).lower()},
            "-f",
            os.path.join(
                SCRIPT_DIR,
                "server.js")]

        xpcshell = os.path.join(self._utilityPath,
                                "xpcshell" + mozinfo.info['bin_suffix'])
        command = [xpcshell] + args
        self._process = mozprocess.ProcessHandler(
            command,
            cwd=SCRIPT_DIR,
            env=env)
        self._process.run()
        self._log.info(
            "%s : launching %s" %
            (self.__class__.__name__, command))
        pid = self._process.pid
        self._log.info("runtests.py | Server pid: %d" % pid)

    def ensureReady(self, timeout):
        assert timeout >= 0

        aliveFile = os.path.join(self._profileDir, "server_alive.txt")
        i = 0
        while i < timeout:
            if os.path.exists(aliveFile):
                break
            time.sleep(1)
            i += 1
        else:
            self._log.info(
                "TEST-UNEXPECTED-FAIL | runtests.py | Timed out while waiting for server startup.")
            self.stop()
            sys.exit(1)

    def stop(self):
        try:
            with urllib2.urlopen(self.shutdownURL) as c:
                c.read()

            # TODO: need ProcessHandler.poll()
            # https://bugzilla.mozilla.org/show_bug.cgi?id=912285
            #      rtncode = self._process.poll()
            rtncode = self._process.proc.poll()
            if rtncode is None:
                # TODO: need ProcessHandler.terminate() and/or .send_signal()
                # https://bugzilla.mozilla.org/show_bug.cgi?id=912285
                # self._process.terminate()
                self._process.proc.terminate()
        except:
            self._process.kill()


class WebSocketServer(object):

    "Class which encapsulates the mod_pywebsocket server"

    def __init__(self, options, scriptdir, logger, debuggerInfo=None):
        self.port = options.webSocketPort
        self.debuggerInfo = debuggerInfo
        self._log = logger
        self._scriptdir = scriptdir

    def start(self):
        # Invoke pywebsocket through a wrapper which adds special SIGINT handling.
        #
        # If we're in an interactive debugger, the wrapper causes the server to
        # ignore SIGINT so the server doesn't capture a ctrl+c meant for the
        # debugger.
        #
        # If we're not in an interactive debugger, the wrapper causes the server to
        # die silently upon receiving a SIGINT.
        scriptPath = 'pywebsocket_wrapper.py'
        script = os.path.join(self._scriptdir, scriptPath)

        cmd = [sys.executable, script]
        if self.debuggerInfo and self.debuggerInfo.interactive:
            cmd += ['--interactive']
        cmd += ['-p', str(self.port), '-w', self._scriptdir, '-l',
                os.path.join(self._scriptdir, "websock.log"),
                '--log-level=debug', '--allow-handlers-outside-root-dir']
        # start the process
        self._process = mozprocess.ProcessHandler(cmd, cwd=SCRIPT_DIR)
        self._process.run()
        pid = self._process.pid
        self._log.info("runtests.py | Websocket server pid: %d" % pid)

    def stop(self):
        self._process.kill()


class MochitestUtilsMixin(object):

    """
    Class containing some utility functions common to both local and remote
    mochitest runners
    """

    # TODO Utility classes are a code smell. This class is temporary
    #      and should be removed when desktop mochitests are refactored
    #      on top of mozbase. Each of the functions in here should
    #      probably live somewhere in mozbase

    oldcwd = os.getcwd()
    jarDir = 'mochijar'

    # Path to the test script on the server
    TEST_PATH = "tests"
    NESTED_OOP_TEST_PATH = "nested_oop"
    CHROME_PATH = "redirect.html"
    urlOpts = []
    log = None

    def __init__(self, logger_options):
        self.update_mozinfo()
        self.server = None
        self.wsserver = None
        self.sslTunnel = None
        self._locations = None

        if self.log is None:
            commandline.log_formatters["tbpl"] = (
                MochitestFormatter,
                "Mochitest specific tbpl formatter")
            self.log = commandline.setup_logging("mochitest",
                                                 logger_options,
                                                 {
                                                     "tbpl": sys.stdout
                                                 })
            MochitestUtilsMixin.log = self.log
            setAutomationLog(self.log)

        self.message_logger = MessageLogger(logger=self.log)

    def update_mozinfo(self):
        """walk up directories to find mozinfo.json update the info"""
        # TODO: This should go in a more generic place, e.g. mozinfo

        path = SCRIPT_DIR
        dirs = set()
        while path != os.path.expanduser('~'):
            if path in dirs:
                break
            dirs.add(path)
            path = os.path.split(path)[0]

        mozinfo.find_and_update_from_json(*dirs)

    def getFullPath(self, path):
        " Get an absolute path relative to self.oldcwd."
        return os.path.normpath(
            os.path.join(
                self.oldcwd,
                os.path.expanduser(path)))

    def getLogFilePath(self, logFile):
        """ return the log file path relative to the device we are testing on, in most cases
            it will be the full path on the local system
        """
        return self.getFullPath(logFile)

    @property
    def locations(self):
        if self._locations is not None:
            return self._locations
        locations_file = os.path.join(SCRIPT_DIR, 'server-locations.txt')
        self._locations = ServerLocations(locations_file)
        return self._locations

    def buildURLOptions(self, options, env):
        """ Add test control options from the command line to the url

            URL parameters to test URL:

            autorun -- kick off tests automatically
            closeWhenDone -- closes the browser after the tests
            hideResultsTable -- hides the table of individual test results
            logFile -- logs test run to an absolute path
            totalChunks -- how many chunks to split tests into
            thisChunk -- which chunk to run
            startAt -- name of test to start at
            endAt -- name of test to end at
            timeout -- per-test timeout in seconds
            repeat -- How many times to repeat the test, ie: repeat=1 will run the test twice.
        """

        if not hasattr(options, 'logFile'):
            options.logFile = ""
        if not hasattr(options, 'fileLevel'):
            options.fileLevel = 'INFO'

        # allow relative paths for logFile
        if options.logFile:
            options.logFile = self.getLogFilePath(options.logFile)

        if options.browserChrome or options.chrome or \
           options.a11y or options.webapprtChrome or options.jetpackPackage or \
           options.jetpackAddon:
            self.makeTestConfig(options)
        else:
            if options.autorun:
                self.urlOpts.append("autorun=1")
            if options.timeout:
                self.urlOpts.append("timeout=%d" % options.timeout)
            if options.closeWhenDone:
                self.urlOpts.append("closeWhenDone=1")
            if options.webapprtContent:
                self.urlOpts.append("testRoot=webapprtContent")
            if options.logFile:
                self.urlOpts.append(
                    "logFile=" +
                    encodeURIComponent(
                        options.logFile))
                self.urlOpts.append(
                    "fileLevel=" +
                    encodeURIComponent(
                        options.fileLevel))
            if options.consoleLevel:
                self.urlOpts.append(
                    "consoleLevel=" +
                    encodeURIComponent(
                        options.consoleLevel))
            if options.totalChunks:
                self.urlOpts.append("totalChunks=%d" % options.totalChunks)
                self.urlOpts.append("thisChunk=%d" % options.thisChunk)
            if options.chunkByDir:
                self.urlOpts.append("chunkByDir=%d" % options.chunkByDir)
            if options.startAt:
                self.urlOpts.append("startAt=%s" % options.startAt)
            if options.endAt:
                self.urlOpts.append("endAt=%s" % options.endAt)
            if options.shuffle:
                self.urlOpts.append("shuffle=1")
            if "MOZ_HIDE_RESULTS_TABLE" in env and env[
                    "MOZ_HIDE_RESULTS_TABLE"] == "1":
                self.urlOpts.append("hideResultsTable=1")
            if options.runUntilFailure:
                self.urlOpts.append("runUntilFailure=1")
            if options.repeat:
                self.urlOpts.append("repeat=%d" % options.repeat)
            if os.path.isfile(
                os.path.join(
                    self.oldcwd,
                    os.path.dirname(__file__),
                    self.TEST_PATH,
                    options.testPath)) and options.repeat > 0:
                self.urlOpts.append("testname=%s" %
                                    ("/").join([self.TEST_PATH, options.testPath]))
            if options.testManifest:
                self.urlOpts.append("testManifest=%s" % options.testManifest)
                if hasattr(options, 'runOnly') and options.runOnly:
                    self.urlOpts.append("runOnly=true")
                else:
                    self.urlOpts.append("runOnly=false")
            if options.manifestFile:
                self.urlOpts.append("manifestFile=%s" % options.manifestFile)
            if options.failureFile:
                self.urlOpts.append(
                    "failureFile=%s" %
                    self.getFullPath(
                        options.failureFile))
            if options.runSlower:
                self.urlOpts.append("runSlower=true")
            if options.debugOnFailure:
                self.urlOpts.append("debugOnFailure=true")
            if options.dumpOutputDirectory:
                self.urlOpts.append(
                    "dumpOutputDirectory=%s" %
                    encodeURIComponent(
                        options.dumpOutputDirectory))
            if options.dumpAboutMemoryAfterTest:
                self.urlOpts.append("dumpAboutMemoryAfterTest=true")
            if options.dumpDMDAfterTest:
                self.urlOpts.append("dumpDMDAfterTest=true")
            if options.debugger:
                self.urlOpts.append("interactiveDebugger=true")

    def getTestFlavor(self, options):
        if options.browserChrome:
            return "browser-chrome"
        elif options.jetpackPackage:
            return "jetpack-package"
        elif options.jetpackAddon:
            return "jetpack-addon"
        elif options.chrome:
            return "chrome"
        elif options.a11y:
            return "a11y"
        elif options.webapprtChrome:
            return "webapprt-chrome"
        elif options.webapprtContent:
            return "webapprt-content"
        else:
            return "mochitest"

    # This check can be removed when bug 983867 is fixed.
    def isTest(self, options, filename):
        allow_js_css = False
        if options.browserChrome:
            allow_js_css = True
            testPattern = re.compile(r"browser_.+\.js")
        elif options.jetpackPackage:
            allow_js_css = True
            testPattern = re.compile(r"test-.+\.js")
        elif options.jetpackAddon:
            testPattern = re.compile(r".+\.xpi")
        elif options.chrome or options.a11y:
            testPattern = re.compile(r"(browser|test)_.+\.(xul|html|js|xhtml)")
        elif options.webapprtContent:
            testPattern = re.compile(r"webapprt_")
        elif options.webapprtChrome:
            allow_js_css = True
            testPattern = re.compile(r"browser_")
        else:
            testPattern = re.compile(r"test_")

        if not allow_js_css and (".js" in filename or ".css" in filename):
            return False

        pathPieces = filename.split("/")

        return (testPattern.match(pathPieces[-1]) and
                not re.search(r'\^headers\^$', filename))

    def getTestPath(self, options):
        if options.ipcplugins:
            return "dom/plugins/test/mochitest"
        else:
            return options.testPath

    def setTestRoot(self, options):
        if hasattr(self, "testRoot"):
            return self.testRoot, self.testRootAbs
        else:
            if options.browserChrome:
                if options.immersiveMode:
                    self.testRoot = 'metro'
                else:
                    self.testRoot = 'browser'
            elif options.jetpackPackage:
                self.testRoot = 'jetpack-package'
            elif options.jetpackAddon:
                self.testRoot = 'jetpack-addon'
            elif options.a11y:
                self.testRoot = 'a11y'
            elif options.webapprtChrome:
                self.testRoot = 'webapprtChrome'
            elif options.webapprtContent:
                self.testRoot = 'webapprtContent'
            elif options.chrome:
                self.testRoot = 'chrome'
            else:
                self.testRoot = self.TEST_PATH
            self.testRootAbs = os.path.join(SCRIPT_DIR, self.testRoot)

    def buildTestURL(self, options):
        testHost = "http://mochi.test:8888"
        testPath = self.getTestPath(options)
        testURL = "/".join([testHost, self.TEST_PATH, testPath])
        if os.path.isfile(
            os.path.join(
                self.oldcwd,
                os.path.dirname(__file__),
                self.TEST_PATH,
                testPath)) and options.repeat > 0:
            testURL = "/".join([testHost,
                                self.TEST_PATH,
                                os.path.dirname(testPath)])
        if options.chrome or options.a11y:
            testURL = "/".join([testHost, self.CHROME_PATH])
        elif options.browserChrome or options.jetpackPackage or options.jetpackAddon:
            testURL = "about:blank"
        if options.nested_oop:
            testURL = "/".join([testHost, self.NESTED_OOP_TEST_PATH])
        return testURL

    def buildTestPath(self, options, testsToFilter=None, disabled=True):
        """ Build the url path to the specific test harness and test file or directory
            Build a manifest of tests to run and write out a json file for the harness to read
            testsToFilter option is used to filter/keep the tests provided in the list

            disabled -- This allows to add all disabled tests on the build side
                        and then on the run side to only run the enabled ones
        """

        tests = self.getActiveTests(options, disabled)
        paths = []
        for test in tests:
            if testsToFilter and (test['path'] not in testsToFilter):
                continue
            paths.append(test)

        # Bug 883865 - add this functionality into manifestparser
        with open(os.path.join(SCRIPT_DIR, options.testRunManifestFile), 'w') as manifestFile:
            manifestFile.write(json.dumps({'tests': paths}))
        options.manifestFile = options.testRunManifestFile

        return self.buildTestURL(options)

    def startWebSocketServer(self, options, debuggerInfo):
        """ Launch the websocket server """
        self.wsserver = WebSocketServer(
            options,
            SCRIPT_DIR,
            self.log,
            debuggerInfo)
        self.wsserver.start()

    def startWebServer(self, options):
        """Create the webserver and start it up"""

        self.server = MochitestServer(options, self.log)
        self.server.start()

        if options.pidFile != "":
            with open(options.pidFile + ".xpcshell.pid", 'w') as f:
                f.write("%s" % self.server._process.pid)

    def startServers(self, options, debuggerInfo, ignoreSSLTunnelExts=False):
        # start servers and set ports
        # TODO: pass these values, don't set on `self`
        self.webServer = options.webServer
        self.httpPort = options.httpPort
        self.sslPort = options.sslPort
        self.webSocketPort = options.webSocketPort

        # httpd-path is specified by standard makefile targets and may be specified
        # on the command line to select a particular version of httpd.js. If not
        # specified, try to select the one from hostutils.zip, as required in
        # bug 882932.
        if not options.httpdPath:
            options.httpdPath = os.path.join(options.utilityPath, "components")

        self.startWebServer(options)
        self.startWebSocketServer(options, debuggerInfo)

        # start SSL pipe
        self.sslTunnel = SSLTunnel(
            options,
            logger=self.log,
            ignoreSSLTunnelExts=ignoreSSLTunnelExts)
        self.sslTunnel.buildConfig(self.locations)
        self.sslTunnel.start()

        # If we're lucky, the server has fully started by now, and all paths are
        # ready, etc.  However, xpcshell cold start times suck, at least for debug
        # builds.  We'll try to connect to the server for awhile, and if we fail,
        # we'll try to kill the server and exit with an error.
        if self.server is not None:
            self.server.ensureReady(self.SERVER_STARTUP_TIMEOUT)

    def stopServers(self):
        """Servers are no longer needed, and perhaps more importantly, anything they
            might spew to console might confuse things."""
        if self.server is not None:
            try:
                self.log.info('Stopping web server')
                self.server.stop()
            except Exception:
                self.log.critical('Exception when stopping web server')

        if self.wsserver is not None:
            try:
                self.log.info('Stopping web socket server')
                self.wsserver.stop()
            except Exception:
                self.log.critical('Exception when stopping web socket server')

        if self.sslTunnel is not None:
            try:
                self.log.info('Stopping ssltunnel')
                self.sslTunnel.stop()
            except Exception:
                self.log.critical('Exception stopping ssltunnel')

    def copyExtraFilesToProfile(self, options):
        "Copy extra files or dirs specified on the command line to the testing profile."
        for f in options.extraProfileFiles:
            abspath = self.getFullPath(f)
            if os.path.isfile(abspath):
                shutil.copy2(abspath, options.profilePath)
            elif os.path.isdir(abspath):
                dest = os.path.join(
                    options.profilePath,
                    os.path.basename(abspath))
                shutil.copytree(abspath, dest)
            else:
                self.log.warning(
                    "runtests.py | Failed to copy %s to profile" %
                    abspath)

    def installChromeJar(self, chrome, options):
        """
          copy mochijar directory to profile as an extension so we have chrome://mochikit for all harness code
        """
        # Write chrome.manifest.
        with open(os.path.join(options.profilePath, "extensions", "staged", "mochikit@mozilla.org", "chrome.manifest"), "a") as mfile:
            mfile.write(chrome)

    def getChromeTestDir(self, options):
        dir = os.path.join(os.path.abspath("."), SCRIPT_DIR) + "/"
        if mozinfo.isWin:
            dir = "file:///" + dir.replace("\\", "/")
        return dir

    def writeChromeManifest(self, options):
        manifest = os.path.join(options.profilePath, "tests.manifest")
        with open(manifest, "w") as manifestFile:
            # Register chrome directory.
            chrometestDir = self.getChromeTestDir(options)
            manifestFile.write(
                "content mochitests %s contentaccessible=yes\n" %
                chrometestDir)
            manifestFile.write(
                "content mochitests-any %s contentaccessible=yes remoteenabled=yes\n" %
                chrometestDir)
            manifestFile.write(
                "content mochitests-content %s contentaccessible=yes remoterequired=yes\n" %
                chrometestDir)

            if options.testingModulesDir is not None:
                manifestFile.write("resource testing-common file:///%s\n" %
                                   options.testingModulesDir)
        return manifest

    def addChromeToProfile(self, options):
        "Adds MochiKit chrome tests to the profile."

        # Create (empty) chrome directory.
        chromedir = os.path.join(options.profilePath, "chrome")
        os.mkdir(chromedir)

        # Write userChrome.css.
        chrome = """
@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"); /* set default namespace to XUL */
toolbar,
toolbarpalette {
  background-color: rgb(235, 235, 235) !important;
}
toolbar#nav-bar {
  background-image: none !important;
}
"""
        with open(os.path.join(options.profilePath, "userChrome.css"), "a") as chromeFile:
            chromeFile.write(chrome)

        manifest = self.writeChromeManifest(options)

        # Call installChromeJar().
        if not os.path.isdir(os.path.join(SCRIPT_DIR, self.jarDir)):
            self.log.info(
                "TEST-UNEXPECTED-FAIL | invalid setup: missing mochikit extension")
            return None

        # Support Firefox (browser), B2G (shell), SeaMonkey (navigator), and Webapp
        # Runtime (webapp).
        chrome = ""
        if options.browserChrome or options.chrome or options.a11y or options.webapprtChrome:
            chrome += """
overlay chrome://browser/content/browser.xul chrome://mochikit/content/browser-test-overlay.xul
overlay chrome://browser/content/shell.xhtml chrome://mochikit/content/browser-test-overlay.xul
overlay chrome://navigator/content/navigator.xul chrome://mochikit/content/browser-test-overlay.xul
overlay chrome://webapprt/content/webapp.xul chrome://mochikit/content/browser-test-overlay.xul
"""

        if options.jetpackPackage:
            chrome += """
overlay chrome://browser/content/browser.xul chrome://mochikit/content/jetpack-package-overlay.xul
"""

        if options.jetpackAddon:
            chrome += """
overlay chrome://browser/content/browser.xul chrome://mochikit/content/jetpack-addon-overlay.xul
"""

        self.installChromeJar(chrome, options)
        return manifest

    def getExtensionsToInstall(self, options):
        "Return a list of extensions to install in the profile"
        extensions = options.extensionsToInstall or []
        appDir = options.app[
            :options.app.rfind(
                os.sep)] if options.app else options.utilityPath

        extensionDirs = [
            # Extensions distributed with the test harness.
            os.path.normpath(os.path.join(SCRIPT_DIR, "extensions")),
        ]
        if appDir:
            # Extensions distributed with the application.
            extensionDirs.append(
                os.path.join(
                    appDir,
                    "distribution",
                    "extensions"))

        for extensionDir in extensionDirs:
            if os.path.isdir(extensionDir):
                for dirEntry in os.listdir(extensionDir):
                    if dirEntry not in options.extensionsToExclude:
                        path = os.path.join(extensionDir, dirEntry)
                        if os.path.isdir(path) or (
                                os.path.isfile(path) and path.endswith(".xpi")):
                            extensions.append(path)

        # append mochikit
        extensions.append(os.path.join(SCRIPT_DIR, self.jarDir))
        return extensions


class SSLTunnel:

    def __init__(self, options, logger, ignoreSSLTunnelExts=False):
        self.log = logger
        self.process = None
        self.utilityPath = options.utilityPath
        self.xrePath = options.xrePath
        self.certPath = options.certPath
        self.sslPort = options.sslPort
        self.httpPort = options.httpPort
        self.webServer = options.webServer
        self.webSocketPort = options.webSocketPort
        self.useSSLTunnelExts = not ignoreSSLTunnelExts

        self.customCertRE = re.compile("^cert=(?P<nickname>[0-9a-zA-Z_ ]+)")
        self.clientAuthRE = re.compile("^clientauth=(?P<clientauth>[a-z]+)")
        self.redirRE = re.compile("^redir=(?P<redirhost>[0-9a-zA-Z_ .]+)")

    def writeLocation(self, config, loc):
        for option in loc.options:
            match = self.customCertRE.match(option)
            if match:
                customcert = match.group("nickname")
                config.write("listen:%s:%s:%s:%s\n" %
                             (loc.host, loc.port, self.sslPort, customcert))

            match = self.clientAuthRE.match(option)
            if match:
                clientauth = match.group("clientauth")
                config.write("clientauth:%s:%s:%s:%s\n" %
                             (loc.host, loc.port, self.sslPort, clientauth))

            match = self.redirRE.match(option)
            if match:
                redirhost = match.group("redirhost")
                config.write("redirhost:%s:%s:%s:%s\n" %
                             (loc.host, loc.port, self.sslPort, redirhost))

            if self.useSSLTunnelExts and option in (
                    'ssl3',
                    'rc4',
                    'failHandshake'):
                config.write(
                    "%s:%s:%s:%s\n" %
                    (option, loc.host, loc.port, self.sslPort))

    def buildConfig(self, locations):
        """Create the ssltunnel configuration file"""
        configFd, self.configFile = tempfile.mkstemp(
            prefix="ssltunnel", suffix=".cfg")
        with os.fdopen(configFd, "w") as config:
            config.write("httpproxy:1\n")
            config.write("certdbdir:%s\n" % self.certPath)
            config.write("forward:127.0.0.1:%s\n" % self.httpPort)
            config.write(
                "websocketserver:%s:%s\n" %
                (self.webServer, self.webSocketPort))
            config.write("listen:*:%s:pgo server certificate\n" % self.sslPort)

            for loc in locations:
                if loc.scheme == "https" and "nocert" not in loc.options:
                    self.writeLocation(config, loc)

    def start(self):
        """ Starts the SSL Tunnel """

        # start ssltunnel to provide https:// URLs capability
        bin_suffix = mozinfo.info.get('bin_suffix', '')
        ssltunnel = os.path.join(self.utilityPath, "ssltunnel" + bin_suffix)
        if not os.path.exists(ssltunnel):
            self.log.error(
                "INFO | runtests.py | expected to find ssltunnel at %s" %
                ssltunnel)
            exit(1)

        env = environment(xrePath=self.xrePath)
        env["LD_LIBRARY_PATH"] = self.xrePath
        self.process = mozprocess.ProcessHandler([ssltunnel, self.configFile],
                                                 env=env)
        self.process.run()
        self.log.info("runtests.py | SSL tunnel pid: %d" % self.process.pid)

    def stop(self):
        """ Stops the SSL Tunnel and cleans up """
        if self.process is not None:
            self.process.kill()
        if os.path.exists(self.configFile):
            os.remove(self.configFile)


def checkAndConfigureV4l2loopback(device):
    '''
    Determine if a given device path is a v4l2loopback device, and if so
    toggle a few settings on it via fcntl. Very linux-specific.

    Returns (status, device name) where status is a boolean.
    '''
    if not mozinfo.isLinux:
        return False, ''

    libc = ctypes.cdll.LoadLibrary('libc.so.6')
    O_RDWR = 2
    # These are from linux/videodev2.h

    class v4l2_capability(ctypes.Structure):
        _fields_ = [
            ('driver', ctypes.c_char * 16),
            ('card', ctypes.c_char * 32),
            ('bus_info', ctypes.c_char * 32),
            ('version', ctypes.c_uint32),
            ('capabilities', ctypes.c_uint32),
            ('device_caps', ctypes.c_uint32),
            ('reserved', ctypes.c_uint32 * 3)
        ]
    VIDIOC_QUERYCAP = 0x80685600

    fd = libc.open(device, O_RDWR)
    if fd < 0:
        return False, ''

    vcap = v4l2_capability()
    if libc.ioctl(fd, VIDIOC_QUERYCAP, ctypes.byref(vcap)) != 0:
        return False, ''

    if vcap.driver != 'v4l2 loopback':
        return False, ''

    class v4l2_control(ctypes.Structure):
        _fields_ = [
            ('id', ctypes.c_uint32),
            ('value', ctypes.c_int32)
        ]

    # These are private v4l2 control IDs, see:
    # https://github.com/umlaeute/v4l2loopback/blob/fd822cf0faaccdf5f548cddd9a5a3dcebb6d584d/v4l2loopback.c#L131
    KEEP_FORMAT = 0x8000000
    SUSTAIN_FRAMERATE = 0x8000001
    VIDIOC_S_CTRL = 0xc008561c

    control = v4l2_control()
    control.id = KEEP_FORMAT
    control.value = 1
    libc.ioctl(fd, VIDIOC_S_CTRL, ctypes.byref(control))

    control.id = SUSTAIN_FRAMERATE
    control.value = 1
    libc.ioctl(fd, VIDIOC_S_CTRL, ctypes.byref(control))
    libc.close(fd)

    return True, vcap.card


def findTestMediaDevices(log):
    '''
    Find the test media devices configured on this system, and return a dict
    containing information about them. The dict will have keys for 'audio'
    and 'video', each containing the name of the media device to use.

    If audio and video devices could not be found, return None.

    This method is only currently implemented for Linux.
    '''
    if not mozinfo.isLinux:
        return None

    info = {}
    # Look for a v4l2loopback device.
    name = None
    device = None
    for dev in sorted(glob.glob('/dev/video*')):
        result, name_ = checkAndConfigureV4l2loopback(dev)
        if result:
            name = name_
            device = dev
            break

    if not (name and device):
        log.error('Couldn\'t find a v4l2loopback video device')
        return None

    # Feed it a frame of output so it has something to display
    subprocess.check_call(['/usr/bin/gst-launch-0.10', 'videotestsrc',
                           'pattern=green', 'num-buffers=1', '!',
                           'v4l2sink', 'device=%s' % device])
    info['video'] = name

    # Use pactl to see if the PulseAudio module-sine-source module is loaded.
    def sine_source_loaded():
        o = subprocess.check_output(
            ['/usr/bin/pactl', 'list', 'short', 'modules'])
        return filter(lambda x: 'module-sine-source' in x, o.splitlines())

    if not sine_source_loaded():
        # Load module-sine-source
        subprocess.check_call(['/usr/bin/pactl', 'load-module',
                               'module-sine-source'])
    if not sine_source_loaded():
        log.error('Couldn\'t load module-sine-source')
        return None

    # Hardcode the name since it's always the same.
    info['audio'] = 'Sine source at 440 Hz'
    return info


class KeyValueParseError(Exception):

    """error when parsing strings of serialized key-values"""

    def __init__(self, msg, errors=()):
        self.errors = errors
        Exception.__init__(self, msg)


def parseKeyValue(strings, separator='=', context='key, value: '):
    """
    parse string-serialized key-value pairs in the form of
    `key = value`. Returns a list of 2-tuples.
    Note that whitespace is not stripped.
    """

    # syntax check
    missing = [string for string in strings if separator not in string]
    if missing:
        raise KeyValueParseError(
            "Error: syntax error in %s" %
            (context, ','.join(missing)), errors=missing)
    return [string.split(separator, 1) for string in strings]


class Mochitest(MochitestUtilsMixin):
    certdbNew = False
    sslTunnel = None
    vmwareHelper = None
    DEFAULT_TIMEOUT = 60.0
    mediaDevices = None

    # XXX use automation.py for test name to avoid breaking legacy
    # TODO: replace this with 'runtests.py' or 'mochitest' or the like
    test_name = 'automation.py'

    def __init__(self, logger_options):
        super(Mochitest, self).__init__(logger_options)

        # environment function for browserEnv
        self.environment = environment

        # Max time in seconds to wait for server startup before tests will fail -- if
        # this seems big, it's mostly for debug machines where cold startup
        # (particularly after a build) takes forever.
        self.SERVER_STARTUP_TIMEOUT = 180 if mozinfo.info.get('debug') else 90

        # metro browser sub process id
        self.browserProcessId = None

        self.haveDumpedScreen = False
        # Create variables to count the number of passes, fails, todos.
        self.countpass = 0
        self.countfail = 0
        self.counttodo = 0

        self.expectedError = {}
        self.result = {}

    def extraPrefs(self, extraPrefs):
        """interpolate extra preferences from option strings"""

        try:
            return dict(parseKeyValue(extraPrefs, context='--setpref='))
        except KeyValueParseError as e:
            print str(e)
            sys.exit(1)

    def fillCertificateDB(self, options):
        # TODO: move -> mozprofile:
        # https://bugzilla.mozilla.org/show_bug.cgi?id=746243#c35

        pwfilePath = os.path.join(options.profilePath, ".crtdbpw")
        with open(pwfilePath, "w") as pwfile:
            pwfile.write("\n")

        # Pre-create the certification database for the profile
        env = self.environment(xrePath=options.xrePath)
        env["LD_LIBRARY_PATH"] = options.xrePath
        bin_suffix = mozinfo.info.get('bin_suffix', '')
        certutil = os.path.join(options.utilityPath, "certutil" + bin_suffix)
        pk12util = os.path.join(options.utilityPath, "pk12util" + bin_suffix)

        if self.certdbNew:
            # android and b2g use the new DB formats exclusively
            certdbPath = "sql:" + options.profilePath
        else:
            # desktop seems to use the old
            certdbPath = options.profilePath

        status = call(
            [certutil, "-N", "-d", certdbPath, "-f", pwfilePath], env=env)
        if status:
            return status

        # Walk the cert directory and add custom CAs and client certs
        files = os.listdir(options.certPath)
        for item in files:
            root, ext = os.path.splitext(item)
            if ext == ".ca":
                trustBits = "CT,,"
                if root.endswith("-object"):
                    trustBits = "CT,,CT"
                call([certutil,
                      "-A",
                      "-i",
                      os.path.join(options.certPath,
                                   item),
                      "-d",
                      certdbPath,
                      "-f",
                      pwfilePath,
                      "-n",
                      root,
                      "-t",
                      trustBits],
                     env=env)
            elif ext == ".client":
                call([pk12util, "-i", os.path.join(options.certPath, item),
                      "-w", pwfilePath, "-d", certdbPath],
                     env=env)

        os.unlink(pwfilePath)
        return 0

    def buildProfile(self, options):
        """ create the profile and add optional chrome bits and files if requested """
        if options.browserChrome and options.timeout:
            options.extraPrefs.append(
                "testing.browserTestHarness.timeout=%d" %
                options.timeout)
        options.extraPrefs.append(
            "browser.tabs.remote.autostart=%s" %
            ('true' if options.e10s else 'false'))
        if options.strictContentSandbox:
            options.extraPrefs.append(
                "security.sandbox.windows.content.moreStrict=true")
        options.extraPrefs.append(
            "dom.ipc.tabs.nested.enabled=%s" %
            ('true' if options.nested_oop else 'false'))

        # get extensions to install
        extensions = self.getExtensionsToInstall(options)

        # web apps
        appsPath = os.path.join(
            SCRIPT_DIR,
            'profile_data',
            'webapps_mochitest.json')
        if os.path.exists(appsPath):
            with open(appsPath) as apps_file:
                apps = json.load(apps_file)
        else:
            apps = None

        # preferences
        preferences = [
            os.path.join(
                SCRIPT_DIR,
                'profile_data',
                'prefs_general.js')]

        # TODO: Let's include those prefs until bug 1072443 is fixed
        if mozinfo.info.get('buildapp') == 'mulet':
            preferences += [os.path.join(SCRIPT_DIR,
                                         'profile_data',
                                         'prefs_b2g_unittest.js')]

        prefs = {}
        for path in preferences:
            prefs.update(Preferences.read_prefs(path))

        prefs.update(self.extraPrefs(options.extraPrefs))

        # interpolate preferences
        interpolation = {
            "server": "%s:%s" %
            (options.webServer, options.httpPort)}

        # TODO: Remove OOP once bug 1072443 is fixed
        if mozinfo.info.get('buildapp') == 'mulet':
            interpolation["OOP"] = "false"

        prefs = json.loads(json.dumps(prefs) % interpolation)
        for pref in prefs:
            prefs[pref] = Preferences.cast(prefs[pref])
        # TODO: make this less hacky
        # https://bugzilla.mozilla.org/show_bug.cgi?id=913152

        # proxy
        proxy = {'remote': options.webServer,
                 'http': options.httpPort,
                 'https': options.sslPort,
                 # use SSL port for legacy compatibility; see
                 # - https://bugzilla.mozilla.org/show_bug.cgi?id=688667#c66
                 # - https://bugzilla.mozilla.org/show_bug.cgi?id=899221
                 # - https://github.com/mozilla/mozbase/commit/43f9510e3d58bfed32790c82a57edac5f928474d
                 #             'ws': str(self.webSocketPort)
                 'ws': options.sslPort
                 }

        # See if we should use fake media devices.
        if options.useTestMediaDevices:
            prefs['media.audio_loopback_dev'] = self.mediaDevices['audio']
            prefs['media.video_loopback_dev'] = self.mediaDevices['video']

        # create a profile
        self.profile = Profile(profile=options.profilePath,
                               addons=extensions,
                               locations=self.locations,
                               preferences=prefs,
                               apps=apps,
                               proxy=proxy
                               )

        # Fix options.profilePath for legacy consumers.
        options.profilePath = self.profile.profile

        manifest = self.addChromeToProfile(options)
        self.copyExtraFilesToProfile(options)

        # create certificate database for the profile
        # TODO: this should really be upstreamed somewhere, maybe mozprofile
        certificateStatus = self.fillCertificateDB(options)
        if certificateStatus:
            self.log.info(
                "TEST-UNEXPECTED-FAIL | runtests.py | Certificate integration failed")
            return None

        return manifest

    def getGMPPluginPath(self, options):
        if options.gmp_path:
            return options.gmp_path

        gmp_parentdirs = [
            # For local builds, GMP plugins will be under dist/bin.
            options.xrePath,
            # For packaged builds, GMP plugins will get copied under
            # $profile/plugins.
            os.path.join(self.profile.profile, 'plugins'),
        ]

        gmp_subdirs = [
            os.path.join('gmp-fake', '1.0'),
            os.path.join('gmp-fakeopenh264', '1.0'),
            os.path.join('gmp-clearkey', '0.1'),
        ]

        gmp_paths = [os.path.join(parent, sub)
                     for parent in gmp_parentdirs
                     for sub in gmp_subdirs
                     if os.path.isdir(os.path.join(parent, sub))]

        if not gmp_paths:
            # This is fatal for desktop environments.
            raise EnvironmentError('Could not find test gmp plugins')

        return os.pathsep.join(gmp_paths)

    def buildBrowserEnv(self, options, debugger=False, env=None):
        """build the environment variables for the specific test and operating system"""
        if mozinfo.info["asan"]:
            lsanPath = SCRIPT_DIR
        else:
            lsanPath = None

        browserEnv = self.environment(
            xrePath=options.xrePath,
            env=env,
            debugger=debugger,
            dmdPath=options.dmdPath,
            lsanPath=lsanPath)

        # These variables are necessary for correct application startup; change
        # via the commandline at your own risk.
        browserEnv["XPCOM_DEBUG_BREAK"] = "stack"

        # When creating child processes on Windows pre-Vista (e.g. Windows XP) we
        # don't normally inherit stdout/err handles, because you can only do it by
        # inheriting all other inheritable handles as well.
        # We need to inherit them for plain mochitests for test logging purposes, so
        # we do so on the basis of a specific environment variable.
        if self.getTestFlavor(options) == "mochitest":
            browserEnv["MOZ_WIN_INHERIT_STD_HANDLES_PRE_VISTA"] = "1"

        # interpolate environment passed with options
        try:
            browserEnv.update(
                dict(
                    parseKeyValue(
                        options.environment,
                        context='--setenv')))
        except KeyValueParseError as e:
            self.log.error(str(e))
            return None

        browserEnv["XPCOM_MEM_BLOAT_LOG"] = self.leak_report_file

        try:
            gmp_path = self.getGMPPluginPath(options)
            if gmp_path is not None:
                browserEnv["MOZ_GMP_PATH"] = gmp_path
        except EnvironmentError:
            self.log.error('Could not find path to gmp-fake plugin!')
            return None

        if options.fatalAssertions:
            browserEnv["XPCOM_DEBUG_BREAK"] = "stack-and-abort"

        # Produce an NSPR log, is setup (see NSPR_LOG_MODULES global at the top of
        # this script).
        self.nsprLogs = NSPR_LOG_MODULES and "MOZ_UPLOAD_DIR" in os.environ
        if self.nsprLogs:
            browserEnv["NSPR_LOG_MODULES"] = NSPR_LOG_MODULES

            browserEnv["NSPR_LOG_FILE"] = "%s/nspr.log" % tempfile.gettempdir()
            browserEnv["GECKO_SEPARATE_NSPR_LOGS"] = "1"

        if debugger and not options.slowscript:
            browserEnv["JS_DISABLE_SLOW_SCRIPT_SIGNALS"] = "1"

        return browserEnv

    def cleanup(self, options):
        """ remove temporary files and profile """
        if hasattr(self, 'manifest') and self.manifest is not None:
            os.remove(self.manifest)
        if hasattr(self, 'profile'):
            del self.profile
        if options.pidFile != "":
            try:
                os.remove(options.pidFile)
                if os.path.exists(options.pidFile + ".xpcshell.pid"):
                    os.remove(options.pidFile + ".xpcshell.pid")
            except:
                self.log.warning(
                    "cleaning up pidfile '%s' was unsuccessful from the test harness" %
                    options.pidFile)
        options.manifestFile = None

    def dumpScreen(self, utilityPath):
        if self.haveDumpedScreen:
            self.log.info(
                "Not taking screenshot here: see the one that was previously logged")
            return
        self.haveDumpedScreen = True
        dumpScreen(utilityPath)

    def killAndGetStack(
            self,
            processPID,
            utilityPath,
            debuggerInfo,
            dump_screen=False):
        """
        Kill the process, preferrably in a way that gets us a stack trace.
        Also attempts to obtain a screenshot before killing the process
        if specified.
        """

        if dump_screen:
            self.dumpScreen(utilityPath)

        if mozinfo.info.get('crashreporter', True) and not debuggerInfo:
            if mozinfo.isWin:
                # We should have a "crashinject" program in our utility path
                crashinject = os.path.normpath(
                    os.path.join(
                        utilityPath,
                        "crashinject.exe"))
                if os.path.exists(crashinject):
                    status = subprocess.Popen(
                        [crashinject, str(processPID)]).wait()
                    printstatus(status, "crashinject")
                    if status == 0:
                        return
            else:
                try:
                    os.kill(processPID, signal.SIGABRT)
                except OSError:
                    # https://bugzilla.mozilla.org/show_bug.cgi?id=921509
                    self.log.info(
                        "Can't trigger Breakpad, process no longer exists")
                return
        self.log.info("Can't trigger Breakpad, just killing process")
        killPid(processPID, self.log)

    def checkForZombies(self, processLog, utilityPath, debuggerInfo):
        """Look for hung processes"""

        if not os.path.exists(processLog):
            self.log.info(
                'Automation Error: PID log not found: %s' %
                processLog)
            # Whilst no hung process was found, the run should still display as
            # a failure
            return True

        # scan processLog for zombies
        self.log.info('zombiecheck | Reading PID log: %s' % processLog)
        processList = []
        pidRE = re.compile(r'launched child process (\d+)$')
        with open(processLog) as processLogFD:
            for line in processLogFD:
                self.log.info(line.rstrip())
                m = pidRE.search(line)
                if m:
                    processList.append(int(m.group(1)))

        # kill zombies
        foundZombie = False
        for processPID in processList:
            self.log.info(
                "zombiecheck | Checking for orphan process with PID: %d" %
                processPID)
            if isPidAlive(processPID):
                foundZombie = True
                self.log.info(
                    "TEST-UNEXPECTED-FAIL | zombiecheck | child process %d still alive after shutdown" %
                    processPID)
                self.killAndGetStack(
                    processPID,
                    utilityPath,
                    debuggerInfo,
                    dump_screen=not debuggerInfo)

        return foundZombie

    def startVMwareRecording(self, options):
        """ starts recording inside VMware VM using the recording helper dll """
        assert mozinfo.isWin
        from ctypes import cdll
        self.vmwareHelper = cdll.LoadLibrary(self.vmwareHelperPath)
        if self.vmwareHelper is None:
            self.log.warning("runtests.py | Failed to load "
                             "VMware recording helper")
            return
        self.log.info("runtests.py | Starting VMware recording.")
        try:
            self.vmwareHelper.StartRecording()
        except Exception as e:
            self.log.warning("runtests.py | Failed to start "
                             "VMware recording: (%s)" % str(e))
            self.vmwareHelper = None

    def stopVMwareRecording(self):
        """ stops recording inside VMware VM using the recording helper dll """
        try:
            assert mozinfo.isWin
            if self.vmwareHelper is not None:
                self.log.info("runtests.py | Stopping VMware recording.")
                self.vmwareHelper.StopRecording()
        except Exception as e:
            self.log.warning("runtests.py | Failed to stop "
                             "VMware recording: (%s)" % str(e))
            self.log.exception('Error stopping VMWare recording')

        self.vmwareHelper = None

    def runApp(self,
               testUrl,
               env,
               app,
               profile,
               extraArgs,
               utilityPath,
               debuggerInfo=None,
               symbolsPath=None,
               timeout=-1,
               onLaunch=None,
               detectShutdownLeaks=False,
               screenshotOnFail=False,
               testPath=None,
               bisectChunk=None,
               quiet=False):
        """
        Run the app, log the duration it took to execute, return the status code.
        Kills the app if it runs for longer than |maxTime| seconds, or outputs nothing for |timeout| seconds.
        """

        # configure the message logger buffering
        self.message_logger.buffering = quiet

        # debugger information
        interactive = False
        debug_args = None
        if debuggerInfo:
            interactive = debuggerInfo.interactive
            debug_args = [debuggerInfo.path] + debuggerInfo.args

        # fix default timeout
        if timeout == -1:
            timeout = self.DEFAULT_TIMEOUT

        # copy env so we don't munge the caller's environment
        env = env.copy()

        # make sure we clean up after ourselves.
        try:
            # set process log environment variable
            tmpfd, processLog = tempfile.mkstemp(suffix='pidlog')
            os.close(tmpfd)
            env["MOZ_PROCESS_LOG"] = processLog

            if interactive:
                # If an interactive debugger is attached,
                # don't use timeouts, and don't capture ctrl-c.
                timeout = None
                signal.signal(signal.SIGINT, lambda sigid, frame: None)

            # build command line
            cmd = os.path.abspath(app)
            args = list(extraArgs)
            # TODO: mozrunner should use -foreground at least for mac
            # https://bugzilla.mozilla.org/show_bug.cgi?id=916512
            args.append('-foreground')
            if testUrl:
                if debuggerInfo and debuggerInfo.requiresEscapedArgs:
                    testUrl = testUrl.replace("&", "\\&")
                args.append(testUrl)

            if detectShutdownLeaks:
                shutdownLeaks = ShutdownLeaks(self.log)
            else:
                shutdownLeaks = None

            if mozinfo.info["asan"] and (mozinfo.isLinux or mozinfo.isMac):
                lsanLeaks = LSANLeaks(self.log)
            else:
                lsanLeaks = None

            # create an instance to process the output
            outputHandler = self.OutputHandler(
                harness=self,
                utilityPath=utilityPath,
                symbolsPath=symbolsPath,
                dump_screen_on_timeout=not debuggerInfo,
                dump_screen_on_fail=screenshotOnFail,
                shutdownLeaks=shutdownLeaks,
                lsanLeaks=lsanLeaks,
                bisectChunk=bisectChunk)

            def timeoutHandler():
                browserProcessId = outputHandler.browserProcessId
                self.handleTimeout(
                    timeout,
                    proc,
                    utilityPath,
                    debuggerInfo,
                    browserProcessId,
                    testPath)
            kp_kwargs = {'kill_on_timeout': False,
                         'cwd': SCRIPT_DIR,
                         'onTimeout': [timeoutHandler]}
            kp_kwargs['processOutputLine'] = [outputHandler]

            # create mozrunner instance and start the system under test process
            self.lastTestSeen = self.test_name
            startTime = datetime.now()

            # b2g desktop requires Runner even though appname is b2g
            if mozinfo.info.get('appname') == 'b2g' and mozinfo.info.get(
                    'toolkit') != 'gonk':
                runner_cls = mozrunner.Runner
            else:
                runner_cls = mozrunner.runners.get(
                    mozinfo.info.get(
                        'appname',
                        'firefox'),
                    mozrunner.Runner)
            runner = runner_cls(profile=self.profile,
                                binary=cmd,
                                cmdargs=args,
                                env=env,
                                process_class=mozprocess.ProcessHandlerMixin,
                                process_args=kp_kwargs)

            # start the runner
            runner.start(debug_args=debug_args,
                         interactive=interactive,
                         outputTimeout=timeout)
            proc = runner.process_handler
            self.log.info("runtests.py | Application pid: %d" % proc.pid)

            if onLaunch is not None:
                # Allow callers to specify an onLaunch callback to be fired after the
                # app is launched.
                # We call onLaunch for b2g desktop mochitests so that we can
                # run a Marionette script after gecko has completed startup.
                onLaunch()

            # wait until app is finished
            # XXX copy functionality from
            # https://github.com/mozilla/mozbase/blob/master/mozrunner/mozrunner/runner.py#L61
            # until bug 913970 is fixed regarding mozrunner `wait` not returning status
            # see https://bugzilla.mozilla.org/show_bug.cgi?id=913970
            status = proc.wait()
            printstatus(status, "Main app process")
            runner.process_handler = None

            # finalize output handler
            outputHandler.finish()

            # record post-test information
            if status:
                self.message_logger.dump_buffered()
                self.log.info(
                    "TEST-UNEXPECTED-FAIL | %s | application terminated with exit code %s" %
                    (self.lastTestSeen, status))
            else:
                self.lastTestSeen = 'Main app process exited normally'

            self.log.info(
                "runtests.py | Application ran for: %s" % str(
                    datetime.now() - startTime))

            # Do a final check for zombie child processes.
            zombieProcesses = self.checkForZombies(
                processLog,
                utilityPath,
                debuggerInfo)

            # check for crashes
            minidump_path = os.path.join(self.profile.profile, "minidumps")
            crash_count = mozcrash.log_crashes(
                self.log,
                minidump_path,
                symbolsPath,
                test=self.lastTestSeen)

            if crash_count or zombieProcesses:
                status = 1

        finally:
            # cleanup
            if os.path.exists(processLog):
                os.remove(processLog)

        return status

    def initializeLooping(self, options):
        """
          This method is used to clear the contents before each run of for loop.
          This method is used for --run-by-dir and --bisect-chunk.
        """
        self.expectedError.clear()
        self.result.clear()
        options.manifestFile = None
        options.profilePath = None
        self.urlOpts = []

    def getActiveTests(self, options, disabled=True):
        """
          This method is used to parse the manifest and return active filtered tests.
        """
        self.setTestRoot(options)
        manifest = self.getTestManifest(options)
        if manifest:
            # Python 2.6 doesn't allow unicode keys to be used for keyword
            # arguments. This gross hack works around the problem until we
            # rid ourselves of 2.6.
            info = {}
            for k, v in mozinfo.info.items():
                if isinstance(k, unicode):
                    k = k.encode('ascii')
                info[k] = v

            # Bug 883858 - return all tests including disabled tests
            testPath = self.getTestPath(options)
            testPath = testPath.replace('\\', '/')
            if testPath.endswith('.html') or \
               testPath.endswith('.xhtml') or \
               testPath.endswith('.xul') or \
               testPath.endswith('.js'):
                    # In the case where we have a single file, we don't want to
                    # filter based on options such as subsuite.
                tests = manifest.active_tests(disabled=disabled, **info)
                for test in tests:
                    if 'disabled' in test:
                        del test['disabled']

            else:
                filters = [subsuite(options.subsuite)]
                tests = manifest.active_tests(
                    disabled=disabled, filters=filters, **info)
                if len(tests) == 0:
                    tests = manifest.active_tests(disabled=True, **info)

        paths = []

        for test in tests:
            if len(tests) == 1 and 'disabled' in test:
                del test['disabled']

            pathAbs = os.path.abspath(test['path'])
            assert pathAbs.startswith(self.testRootAbs)
            tp = pathAbs[len(self.testRootAbs):].replace('\\', '/').strip('/')

            # Filter out tests if we are using --test-path
            if testPath and not tp.startswith(testPath):
                continue

            if not self.isTest(options, tp):
                self.log.warning(
                    'Warning: %s from manifest %s is not a valid test' %
                    (test['name'], test['manifest']))
                continue

            testob = {'path': tp}
            if 'disabled' in test:
                testob['disabled'] = test['disabled']
            if 'expected' in test:
                testob['expected'] = test['expected']
            paths.append(testob)

        def path_sort(ob1, ob2):
            path1 = ob1['path'].split('/')
            path2 = ob2['path'].split('/')
            return cmp(path1, path2)

        paths.sort(path_sort)

        return paths

    def logPreamble(self, tests):
        """Logs a suite_start message and test_start/test_end at the beginning of a run.
        """
        self.log.suite_start([t['path'] for t in tests])
        for test in tests:
            if 'disabled' in test:
                self.log.test_start(test['path'])
                self.log.test_end(
                    test['path'],
                    'SKIP',
                    message=test['disabled'])

    def getTestsToRun(self, options):
        """
          This method makes a list of tests that are to be run. Required mainly for --bisect-chunk.
        """
        tests = self.getActiveTests(options)
        self.logPreamble(tests)

        testsToRun = []
        for test in tests:
            if 'disabled' in test:
                continue
            testsToRun.append(test['path'])

        return testsToRun

    def runMochitests(self, options, onLaunch=None):
        "This is a base method for calling other methods in this class for --bisect-chunk."
        testsToRun = self.getTestsToRun(options)

        # Making an instance of bisect class for --bisect-chunk option.
        bisect = bisection.Bisect(self)
        finished = False
        status = 0
        bisection_log = 0
        while not finished:
            if options.bisectChunk:
                testsToRun = bisect.pre_test(options, testsToRun, status)
                # To inform that we are in the process of bisection, and to
                # look for bleedthrough
                if options.bisectChunk != "default" and not bisection_log:
                    self.log.info(
                        "TEST-UNEXPECTED-FAIL | Bisection | Please ignore repeats and look for 'Bleedthrough' (if any) at the end of the failure list")
                    bisection_log = 1

            result = self.doTests(options, onLaunch, testsToRun)
            if options.bisectChunk:
                status = bisect.post_test(
                    options,
                    self.expectedError,
                    self.result)
            else:
                status = -1

            if status == -1:
                finished = True

        # We need to print the summary only if options.bisectChunk has a value.
        # Also we need to make sure that we do not print the summary in between
        # running tests via --run-by-dir.
        if options.bisectChunk and options.bisectChunk in self.result:
            bisect.print_summary()

        return result

    def runTests(self, options, onLaunch=None):
        """ Prepare, configure, run tests and cleanup """

        self.setTestRoot(options)

        # Until we have all green, this only runs on bc*/dt*/mochitest-chrome
        # jobs, not webapprt*, jetpack*, or plain
        if options.browserChrome:
            options.runByDir = True

        if not options.runByDir:
            return self.runMochitests(options, onLaunch)

        # code for --run-by-dir
        dirs = self.getDirectories(options)

        if options.totalChunks > 1:
            chunkSize = int(len(dirs) / options.totalChunks) + 1
            start = chunkSize * (options.thisChunk - 1)
            end = chunkSize * (options.thisChunk)
            dirs = dirs[start:end]

        options.totalChunks = None
        options.thisChunk = None
        options.chunkByDir = 0
        result = 1  # default value, if no tests are run.
        inputTestPath = self.getTestPath(options)
        for dir in dirs:
            if inputTestPath and not inputTestPath.startswith(dir):
                continue

            options.testPath = dir
            print "testpath: %s" % options.testPath

            # If we are using --run-by-dir, we should not use the profile path (if) provided
            # by the user, since we need to create a new directory for each run. We would face problems
            # if we use the directory provided by the user.
            result = self.runMochitests(options, onLaunch)

            # Dump the logging buffer
            self.message_logger.dump_buffered()

        # printing total number of tests
        if options.browserChrome:
            print "TEST-INFO | checking window state"
            print "Browser Chrome Test Summary"
            print "\tPassed: %s" % self.countpass
            print "\tFailed: %s" % self.countfail
            print "\tTodo: %s" % self.counttodo
            print "*** End BrowserChrome Test Results ***"
        else:
            print "0 INFO TEST-START | Shutdown"
            print "1 INFO Passed:  %s" % self.countpass
            print "2 INFO Failed:  %s" % self.countfail
            print "3 INFO Todo:    %s" % self.counttodo
            print "4 INFO SimpleTest FINISHED"

        return result

    def doTests(self, options, onLaunch=None, testsToFilter=None):
        # A call to initializeLooping method is required in case of --run-by-dir or --bisect-chunk
        # since we need to initialize variables for each loop.
        if options.bisectChunk or options.runByDir:
            self.initializeLooping(options)

        # get debugger info, a dict of:
        # {'path': path to the debugger (string),
        #  'interactive': whether the debugger is interactive or not (bool)
        #  'args': arguments to the debugger (list)
        # TODO: use mozrunner.local.debugger_arguments:
        # https://github.com/mozilla/mozbase/blob/master/mozrunner/mozrunner/local.py#L42

        debuggerInfo = None
        if options.debugger:
            debuggerInfo = mozdebug.get_debugger_info(
                options.debugger,
                options.debuggerArgs,
                options.debuggerInteractive)

        if options.useTestMediaDevices:
            devices = findTestMediaDevices(self.log)
            if not devices:
                self.log.error("Could not find test media devices to use")
                return 1
            self.mediaDevices = devices

        # buildProfile sets self.profile .
        # This relies on sideeffects and isn't very stateful:
        # https://bugzilla.mozilla.org/show_bug.cgi?id=919300
        self.manifest = self.buildProfile(options)
        if self.manifest is None:
            return 1

        self.leak_report_file = os.path.join(
            options.profilePath,
            "runtests_leaks.log")

        self.browserEnv = self.buildBrowserEnv(
            options,
            debuggerInfo is not None)

        # If there are any Mulet-specific tests doing remote network access,
        # we will not be aware since we are explicitely allowing this, as for
        # B2G
        if mozinfo.info.get(
                'buildapp') == 'mulet' and 'MOZ_DISABLE_NONLOCAL_CONNECTIONS' in self.browserEnv:
            del self.browserEnv['MOZ_DISABLE_NONLOCAL_CONNECTIONS']

        if self.browserEnv is None:
            return 1

        try:
            self.startServers(options, debuggerInfo)

            # testsToFilter parameter is used to filter out the test list that
            # is sent to buildTestPath
            testURL = self.buildTestPath(options, testsToFilter)

            # read the number of tests here, if we are not going to run any,
            # terminate early
            if os.path.exists(
                os.path.join(
                    SCRIPT_DIR,
                    options.testRunManifestFile)):
                with open(os.path.join(SCRIPT_DIR, options.testRunManifestFile)) as fHandle:
                    tests = json.load(fHandle)
                count = 0
                for test in tests['tests']:
                    count += 1
                if count == 0:
                    return 1

            self.buildURLOptions(options, self.browserEnv)
            if self.urlOpts:
                testURL += "?" + "&".join(self.urlOpts)

            if options.webapprtContent:
                options.browserArgs.extend(('-test-mode', testURL))
                testURL = None

            if options.immersiveMode:
                options.browserArgs.extend(('-firefoxpath', options.app))
                options.app = self.immersiveHelperPath

            if options.jsdebugger:
                options.browserArgs.extend(['-jsdebugger'])

            # Remove the leak detection file so it can't "leak" to the tests run.
            # The file is not there if leak logging was not enabled in the
            # application build.
            if os.path.exists(self.leak_report_file):
                os.remove(self.leak_report_file)

            # then again to actually run mochitest
            if options.timeout:
                timeout = options.timeout + 30
            elif options.debugger or not options.autorun:
                timeout = None
            else:
                timeout = 330.0  # default JS harness timeout is 300 seconds

            if options.vmwareRecording:
                self.startVMwareRecording(options)

            # detect shutdown leaks for m-bc runs
            detectShutdownLeaks = mozinfo.info[
                "debug"] and options.browserChrome and not options.webapprtChrome

            self.log.info("runtests.py | Running tests: start.\n")
            try:
                status = self.runApp(testURL,
                                     self.browserEnv,
                                     options.app,
                                     profile=self.profile,
                                     extraArgs=options.browserArgs,
                                     utilityPath=options.utilityPath,
                                     debuggerInfo=debuggerInfo,
                                     symbolsPath=options.symbolsPath,
                                     timeout=timeout,
                                     onLaunch=onLaunch,
                                     detectShutdownLeaks=detectShutdownLeaks,
                                     screenshotOnFail=options.screenshotOnFail,
                                     testPath=options.testPath,
                                     bisectChunk=options.bisectChunk,
                                     quiet=options.quiet
                                     )
            except KeyboardInterrupt:
                self.log.info("runtests.py | Received keyboard interrupt.\n")
                status = -1
            except:
                traceback.print_exc()
                self.log.error(
                    "Automation Error: Received unexpected exception while running application\n")
                status = 1

        finally:
            if options.vmwareRecording:
                self.stopVMwareRecording()
            self.stopServers()

        processLeakLog(self.leak_report_file, options)

        if self.nsprLogs:
            with zipfile.ZipFile("%s/nsprlog.zip" % self.browserEnv["MOZ_UPLOAD_DIR"], "w", zipfile.ZIP_DEFLATED) as logzip:
                for logfile in glob.glob(
                        "%s/nspr*.log*" %
                        tempfile.gettempdir()):
                    logzip.write(logfile)
                    os.remove(logfile)

        self.log.info("runtests.py | Running tests: end.")

        if self.manifest is not None:
            self.cleanup(options)

        return status

    def handleTimeout(
            self,
            timeout,
            proc,
            utilityPath,
            debuggerInfo,
            browserProcessId,
            testPath=None):
        """handle process output timeout"""
        # TODO: bug 913975 : _processOutput should call self.processOutputLine
        # one more time one timeout (I think)
        if testPath:
            error_message = "TEST-UNEXPECTED-TIMEOUT | %s | application timed out after %d seconds with no output on %s" % (
                self.lastTestSeen, int(timeout), testPath)
        else:
            error_message = "TEST-UNEXPECTED-TIMEOUT | %s | application timed out after %d seconds with no output" % (
                self.lastTestSeen, int(timeout))

        self.message_logger.dump_buffered()
        self.message_logger.buffering = False
        self.log.info(error_message)

        browserProcessId = browserProcessId or proc.pid
        self.killAndGetStack(
            browserProcessId,
            utilityPath,
            debuggerInfo,
            dump_screen=not debuggerInfo)

    class OutputHandler(object):

        """line output handler for mozrunner"""

        def __init__(
                self,
                harness,
                utilityPath,
                symbolsPath=None,
                dump_screen_on_timeout=True,
                dump_screen_on_fail=False,
                shutdownLeaks=None,
                lsanLeaks=None,
                bisectChunk=None):
            """
            harness -- harness instance
            dump_screen_on_timeout -- whether to dump the screen on timeout
            """
            self.harness = harness
            self.utilityPath = utilityPath
            self.symbolsPath = symbolsPath
            self.dump_screen_on_timeout = dump_screen_on_timeout
            self.dump_screen_on_fail = dump_screen_on_fail
            self.shutdownLeaks = shutdownLeaks
            self.lsanLeaks = lsanLeaks
            self.bisectChunk = bisectChunk

            # With metro browser runs this script launches the metro test harness which launches the browser.
            # The metro test harness hands back the real browser process id via log output which we need to
            # pick up on and parse out. This variable tracks the real browser
            # process id if we find it.
            self.browserProcessId = None

            self.stackFixerFunction = self.stackFixer()

        def processOutputLine(self, line):
            """per line handler of output for mozprocess"""
            # Parsing the line (by the structured messages logger).
            messages = self.harness.message_logger.parse_line(line)

            for message in messages:
                    # Passing the message to the handlers
                for handler in self.outputHandlers():
                    message = handler(message)

                # Processing the message by the logger
                self.harness.message_logger.process_message(message)

        __call__ = processOutputLine

        def outputHandlers(self):
            """returns ordered list of output handlers"""
            handlers = [self.fix_stack,
                        self.record_last_test,
                        self.dumpScreenOnTimeout,
                        self.dumpScreenOnFail,
                        self.trackShutdownLeaks,
                        self.trackLSANLeaks,
                        self.countline,
                        ]
            if self.bisectChunk:
                handlers.append(self.record_result)
                handlers.append(self.first_error)

            return handlers

        def stackFixer(self):
            """
            return stackFixerFunction, if any, to use on the output lines
            """

            if not mozinfo.info.get('debug'):
                return None

            stackFixerFunction = None

            def import_stackFixerModule(module_name):
                sys.path.insert(0, self.utilityPath)
                module = __import__(module_name, globals(), locals(), [])
                sys.path.pop(0)
                return module

            if self.symbolsPath and os.path.exists(self.symbolsPath):
                # Run each line through a function in fix_stack_using_bpsyms.py (uses breakpad symbol files).
                # This method is preferred for Tinderbox builds, since native
                # symbols may have been stripped.
                stackFixerModule = import_stackFixerModule(
                    'fix_stack_using_bpsyms')
                stackFixerFunction = lambda line: stackFixerModule.fixSymbols(
                    line,
                    self.symbolsPath)

            elif mozinfo.isMac:
                # Run each line through fix_macosx_stack.py (uses atos).
                # This method is preferred for developer machines, so we don't
                # have to run "make buildsymbols".
                stackFixerModule = import_stackFixerModule('fix_macosx_stack')
                stackFixerFunction = lambda line: stackFixerModule.fixSymbols(
                    line)

            elif mozinfo.isLinux:
                # Run each line through fix_linux_stack.py (uses addr2line).
                # This method is preferred for developer machines, so we don't
                # have to run "make buildsymbols".
                stackFixerModule = import_stackFixerModule('fix_linux_stack')
                stackFixerFunction = lambda line: stackFixerModule.fixSymbols(
                    line)

            return stackFixerFunction

        def finish(self):
            if self.shutdownLeaks:
                self.shutdownLeaks.process()

            if self.lsanLeaks:
                self.lsanLeaks.process()

        # output message handlers:
        # these take a message and return a message

        def record_result(self, message):
            # by default make the result key equal to pass.
            if message['action'] == 'test_start':
                key = message['test'].split('/')[-1].strip()
                self.harness.result[key] = "PASS"
            elif message['action'] == 'test_status':
                if 'expected' in message:
                    key = message['test'].split('/')[-1].strip()
                    self.harness.result[key] = "FAIL"
                elif message['status'] == 'FAIL':
                    key = message['test'].split('/')[-1].strip()
                    self.harness.result[key] = "TODO"
            return message

        def first_error(self, message):
            if message['action'] == 'test_status' and 'expected' in message and message[
                    'status'] == 'FAIL':
                key = message['test'].split('/')[-1].strip()
                if key not in self.harness.expectedError:
                    self.harness.expectedError[key] = message.get(
                        'message',
                        message['subtest']).strip()
            return message

        def countline(self, message):
            if message['action'] != 'log':
                return message
            line = message['message']
            val = 0
            try:
                val = int(line.split(':')[-1].strip())
            except ValueError:
                return message

            if "Passed:" in line:
                self.harness.countpass += val
            elif "Failed:" in line:
                self.harness.countfail += val
            elif "Todo:" in line:
                self.harness.counttodo += val
            return message

        def fix_stack(self, message):
            if message['action'] == 'log' and self.stackFixerFunction:
                message['message'] = self.stackFixerFunction(
                    message['message'].encode(
                        'utf-8',
                        'replace'))
            return message

        def record_last_test(self, message):
            """record last test on harness"""
            if message['action'] == 'test_start':
                self.harness.lastTestSeen = message['test']
            return message

        def dumpScreenOnTimeout(self, message):
            if (not self.dump_screen_on_fail
                    and self.dump_screen_on_timeout
                    and message['action'] == 'test_status' and 'expected' in message
                    and "Test timed out" in message['subtest']):
                self.harness.dumpScreen(self.utilityPath)
            return message

        def dumpScreenOnFail(self, message):
            if self.dump_screen_on_fail and 'expected' in message and message[
                    'status'] == 'FAIL':
                self.harness.dumpScreen(self.utilityPath)
            return message

        def trackLSANLeaks(self, message):
            if self.lsanLeaks and message['action'] == 'log':
                self.lsanLeaks.log(message['message'])
            return message

        def trackShutdownLeaks(self, message):
            if self.shutdownLeaks:
                self.shutdownLeaks.log(message)
            return message

    def makeTestConfig(self, options):
        "Creates a test configuration file for customizing test execution."
        options.logFile = options.logFile.replace("\\", "\\\\")
        options.testPath = options.testPath.replace("\\", "\\\\")

        if "MOZ_HIDE_RESULTS_TABLE" in os.environ and os.environ[
                "MOZ_HIDE_RESULTS_TABLE"] == "1":
            options.hideResultsTable = True

        d = dict((k, v) for k, v in options.__dict__.items() if (not k.startswith(
            'log_') or not any([k.endswith(fmt) for fmt in commandline.log_formatters.keys()])))
        d['testRoot'] = self.testRoot
        content = json.dumps(d)

        with open(os.path.join(options.profilePath, "testConfig.js"), "w") as config:
            config.write(content)

    def getTestManifest(self, options):
        if isinstance(options.manifestFile, TestManifest):
            manifest = options.manifestFile
        elif options.manifestFile and os.path.isfile(options.manifestFile):
            manifestFileAbs = os.path.abspath(options.manifestFile)
            assert manifestFileAbs.startswith(SCRIPT_DIR)
            manifest = TestManifest([options.manifestFile], strict=False)
        elif options.manifestFile and os.path.isfile(os.path.join(SCRIPT_DIR, options.manifestFile)):
            manifestFileAbs = os.path.abspath(
                os.path.join(
                    SCRIPT_DIR,
                    options.manifestFile))
            assert manifestFileAbs.startswith(SCRIPT_DIR)
            manifest = TestManifest([manifestFileAbs], strict=False)
        else:
            masterName = self.getTestFlavor(options) + '.ini'
            masterPath = os.path.join(SCRIPT_DIR, self.testRoot, masterName)

            if os.path.exists(masterPath):
                manifest = TestManifest([masterPath], strict=False)
            else:
                self._log.warning(
                    'TestManifest masterPath %s does not exist' %
                    masterPath)

        return manifest

    def getDirectories(self, options):
        """
            Make the list of directories by parsing manifests
        """
        tests = self.getActiveTests(options, False)
        dirlist = []

        for test in tests:
            rootdir = '/'.join(test['path'].split('/')[:-1])
            if rootdir not in dirlist:
                dirlist.append(rootdir)

        return dirlist


def main():
    # parse command line options
    parser = MochitestOptions()
    commandline.add_logging_group(parser)
    options, args = parser.parse_args()
    if options is None:
        # parsing error
        sys.exit(1)
    logger_options = {
        key: value for key,
        value in vars(options).iteritems() if key.startswith('log')}
    mochitest = Mochitest(logger_options)
    options = parser.verifyOptions(options, mochitest)

    options.utilityPath = mochitest.getFullPath(options.utilityPath)
    options.certPath = mochitest.getFullPath(options.certPath)
    if options.symbolsPath and len(urlparse(options.symbolsPath).scheme) < 2:
        options.symbolsPath = mochitest.getFullPath(options.symbolsPath)

    return_code = mochitest.runTests(options)
    mochitest.message_logger.finish()

    sys.exit(return_code)

if __name__ == "__main__":
    main()