1# coding=UTF-8
2
3from __future__ import absolute_import
4
5import uuid
6
7import pytest
8from py._path.common import fspath
9
10import mozcrash
11
12
13@pytest.fixture(scope="session")
14def stackwalk(tmpdir_factory):
15    stackwalk = tmpdir_factory.mktemp("stackwalk_binary").join("stackwalk")
16    stackwalk.write("fake binary")
17    stackwalk.chmod(0o744)
18    return stackwalk
19
20
21@pytest.fixture
22def check_for_crashes(tmpdir, stackwalk, monkeypatch):
23    monkeypatch.delenv("MINIDUMP_SAVE_PATH", raising=False)
24
25    def wrapper(
26        dump_directory=fspath(tmpdir),
27        symbols_path="symbols_path",
28        stackwalk_binary=fspath(stackwalk),
29        dump_save_path=None,
30        test_name=None,
31        quiet=True,
32    ):
33        return mozcrash.check_for_crashes(
34            dump_directory,
35            symbols_path,
36            stackwalk_binary,
37            dump_save_path,
38            test_name,
39            quiet,
40        )
41
42    return wrapper
43
44
45@pytest.fixture
46def check_for_java_exception():
47    def wrapper(logcat=None, test_name=None, quiet=True):
48        return mozcrash.check_for_java_exception(logcat, test_name, quiet)
49
50    return wrapper
51
52
53@pytest.fixture
54def minidump_files(request, tmpdir):
55    files = []
56
57    for i in range(getattr(request, "param", 1)):
58        name = uuid.uuid4()
59
60        dmp = tmpdir.join("{}.dmp".format(name))
61        dmp.write("foo")
62
63        extra = tmpdir.join("{}.extra".format(name))
64
65        extra.write_text(
66            u"""
67{
68  "ContentSandboxLevel":"2",
69  "TelemetryEnvironment":"{��}",
70  "EMCheckCompatibility":"true",
71  "ProductName":"Firefox",
72  "ContentSandboxCapabilities":"119",
73  "TelemetryClientId":"",
74  "Vendor":"Mozilla",
75  "InstallTime":"1000000000",
76  "Theme":"classic/1.0",
77  "ReleaseChannel":"default",
78  "ServerURL":"https://crash-reports.mozilla.com",
79  "SafeMode":"0",
80  "ContentSandboxCapable":"1",
81  "useragent_locale":"en-US",
82  "Version":"55.0a1",
83  "BuildID":"20170512114708",
84  "ProductID":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
85  "MozCrashReason": "MOZ_CRASH()",
86  "TelemetryServerURL":"",
87  "DOMIPCEnabled":"1",
88  "Add-ons":"",
89  "CrashTime":"1494582646",
90  "UptimeTS":"14.9179586",
91  "ThreadIdNameMapping":"",
92  "ContentSandboxEnabled":"1",
93  "ProcessType":"content",
94  "StartupTime":"1000000000",
95  "URL":"about:home"
96}
97
98        """,
99            encoding="utf-8",
100        )
101
102        files.append({"dmp": dmp, "extra": extra})
103
104    return files
105
106
107@pytest.fixture(autouse=True)
108def mock_popen(monkeypatch):
109    """Generate a class that can mock subprocess.Popen.
110
111    :param stdouts: Iterable that should return an iterable for the
112                    stdout of each process in turn.
113    """
114
115    class MockPopen(object):
116        def __init__(self, args, *args_rest, **kwargs):
117            # all_popens.append(self)
118            self.args = args
119            self.returncode = 0
120
121        def communicate(self):
122            return (u"Stackwalk command: {}".format(" ".join(self.args)), "")
123
124        def wait(self):
125            return self.returncode
126
127    monkeypatch.setattr(mozcrash.mozcrash.subprocess, "Popen", MockPopen)
128