Debian Codegraph @

tools/fuzzing/interface/harness/FuzzerTestHarness.h

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 * Test harness for XPCOM objects, providing a scoped XPCOM initializer,
8 * nsCOMPtr, nsRefPtr, do_CreateInstance, do_GetService, ns(Auto|C|)String,
9 * and stdio.h/stdlib.h.
10 */
11
12 #ifndef FuzzerTestHarness_h__
13 #define FuzzerTestHarness_h__
14
15 #include "mozilla/ArrayUtils.h"
16 #include "mozilla/Attributes.h"
17 #include "mozilla/IntegerPrintfMacros.h"
18
19 #include "prenv.h"
20 #include "nsComponentManagerUtils.h"
21 #include "nsServiceManagerUtils.h"
22 #include "nsCOMPtr.h"
23 #include "nsString.h"
24 #include "nsAppDirectoryServiceDefs.h"
25 #include "nsDirectoryServiceDefs.h"
26 #include "nsDirectoryServiceUtils.h"
27 #include "nsIDirectoryService.h"
28 #include "nsIFile.h"
29 #include "nsIObserverService.h"
30 #include "nsIServiceManager.h"
31 #include "nsXULAppAPI.h"
32 #include "mozilla/AppShutdown.h"
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdarg.h>
36
37 namespace {
38
39 static uint32_t gFailCount = 0;
40
41 /**
42 * Prints the given failure message and arguments using printf, prepending
43 * "TEST-UNEXPECTED-FAIL " for the benefit of the test harness and
44 * appending "\n" to eliminate having to type it at each call site.
45 */
46 MOZ_FORMAT_PRINTF(1, 2) void fail(const char* msg, ...) {
47 va_list ap;
48
49 printf("TEST-UNEXPECTED-FAIL | ");
50
51 va_start(ap, msg);
52 vprintf(msg, ap);
53 va_end(ap);
54
55 putchar('\n');
56 ++gFailCount;
57 }
58
59 //-----------------------------------------------------------------------------
60
61 class ScopedXPCOM final : public nsIDirectoryServiceProvider2 {
62 public:
63 NS_DECL_ISUPPORTS
64
65 explicit ScopedXPCOM(const char* testName,
66 nsIDirectoryServiceProvider* dirSvcProvider = nullptr)
67 : mDirSvcProvider(dirSvcProvider) {
68 mTestName = testName;
69 printf("Running %s tests...\n", mTestName);
70
71 nsresult rv = NS_InitXPCOM(&mServMgr, nullptr, this);
72 if (NS_FAILED(rv)) {
73 fail("NS_InitXPCOM returned failure code 0x%" PRIx32,
74 static_cast<uint32_t>(rv));
75 mServMgr = nullptr;
76 return;
77 }
78 }
79
80 ~ScopedXPCOM() {
81 // If we created a profile directory, we need to remove it.
82 if (mProfD) {
83 mozilla::AppShutdown::AdvanceShutdownPhase(
84 mozilla::ShutdownPhase::AppShutdownNetTeardown);
85 mozilla::AppShutdown::AdvanceShutdownPhase(
86 mozilla::ShutdownPhase::AppShutdownTeardown);
87 mozilla::AppShutdown::AdvanceShutdownPhase(
88 mozilla::ShutdownPhase::AppShutdown);
89 mozilla::AppShutdown::AdvanceShutdownPhase(
90 mozilla::ShutdownPhase::AppShutdownQM);
91 mozilla::AppShutdown::AdvanceShutdownPhase(
92 mozilla::ShutdownPhase::AppShutdownTelemetry);
93
94 if (NS_FAILED(mProfD->Remove(true))) {
95 NS_WARNING("Problem removing profile directory");
96 }
97
98 mProfD = nullptr;
99 }
100
101 if (mServMgr) {
102 NS_RELEASE(mServMgr);
103 nsresult rv = NS_ShutdownXPCOM(nullptr);
104 if (NS_FAILED(rv)) {
105 fail("XPCOM shutdown failed with code 0x%" PRIx32,
106 static_cast<uint32_t>(rv));
107 exit(1);
108 }
109 }
110
111 printf("Finished running %s tests.\n", mTestName);
112 }
113
114 already_AddRefed<nsIFile> GetProfileDirectory() {
115 if (mProfD) {
116 nsCOMPtr<nsIFile> copy = mProfD;
117 return copy.forget();
118 }
119
120 // Create a unique temporary folder to use for this test.
121 // Note that runcppunittests.py will run tests with a temp
122 // directory as the cwd, so just put something under that.
123 nsCOMPtr<nsIFile> profD;
124 nsresult rv = NS_GetSpecialDirectory(NS_OS_CURRENT_PROCESS_DIR,
125 getter_AddRefs(profD));
126 NS_ENSURE_SUCCESS(rv, nullptr);
127
128 rv = profD->Append(u"cpp-unit-profd"_ns);
129 NS_ENSURE_SUCCESS(rv, nullptr);
130
131 rv = profD->CreateUnique(nsIFile::DIRECTORY_TYPE, 0755);
132 NS_ENSURE_SUCCESS(rv, nullptr);
133
134 mProfD = profD;
135 return profD.forget();
136 }
137
138 already_AddRefed<nsIFile> GetGREDirectory() {
139 if (!mGRED) {
140 char* env = PR_GetEnv("MOZ_XRE_DIR");
141 if (!env) {
142 return nullptr;
143 }
144
145 nsresult rv =
146 NS_NewNativeLocalFile(nsDependentCString(env), getter_AddRefs(mGRED));
147 NS_ENSURE_SUCCESS(rv, nullptr);
148 }
149
150 return do_AddRef(mGRED);
151 }
152
153 already_AddRefed<nsIFile> GetGREBinDirectory() {
154 if (mGREBinD) {
155 nsCOMPtr<nsIFile> copy = mGREBinD;
156 return copy.forget();
157 }
158
159 nsCOMPtr<nsIFile> greD = GetGREDirectory();
160 if (!greD) {
161 return greD.forget();
162 }
163 greD->Clone(getter_AddRefs(mGREBinD));
164
165 #ifdef XP_MACOSX
166 nsAutoCString leafName;
167 mGREBinD->GetNativeLeafName(leafName);
168 if (leafName.EqualsLiteral("Resources")) {
169 mGREBinD->SetNativeLeafName("MacOS"_ns);
170 }
171 #endif
172
173 nsCOMPtr<nsIFile> copy = mGREBinD;
174 return copy.forget();
175 }
176
177 ////////////////////////////////////////////////////////////////////////////
178 //// nsIDirectoryServiceProvider
179
180 NS_IMETHODIMP GetFile(const char* aProperty, bool* _persistent,
181 nsIFile** _result) override {
182 // If we were supplied a directory service provider, ask it first.
183 if (mDirSvcProvider && NS_SUCCEEDED(mDirSvcProvider->GetFile(
184 aProperty, _persistent, _result))) {
185 return NS_OK;
186 }
187
188 // Otherwise, the test harness provides some directories automatically.
189 if (0 == strcmp(aProperty, NS_APP_USER_PROFILE_50_DIR) ||
190 0 == strcmp(aProperty, NS_APP_USER_PROFILE_LOCAL_50_DIR) ||
191 0 == strcmp(aProperty, NS_APP_PROFILE_LOCAL_DIR_STARTUP)) {
192 nsCOMPtr<nsIFile> profD = GetProfileDirectory();
193 NS_ENSURE_TRUE(profD, NS_ERROR_FAILURE);
194
195 nsCOMPtr<nsIFile> clone;
196 nsresult rv = profD->Clone(getter_AddRefs(clone));
197 NS_ENSURE_SUCCESS(rv, rv);
198
199 *_persistent = true;
200 clone.forget(_result);
201 return NS_OK;
202 } else if (0 == strcmp(aProperty, NS_GRE_DIR)) {
203 nsCOMPtr<nsIFile> greD = GetGREDirectory();
204 NS_ENSURE_TRUE(greD, NS_ERROR_FAILURE);
205
206 *_persistent = true;
207 greD.forget(_result);
208 return NS_OK;
209 } else if (0 == strcmp(aProperty, NS_GRE_BIN_DIR)) {
210 nsCOMPtr<nsIFile> greBinD = GetGREBinDirectory();
211 NS_ENSURE_TRUE(greBinD, NS_ERROR_FAILURE);
212
213 *_persistent = true;
214 greBinD.forget(_result);
215 return NS_OK;
216 }
217
218 return NS_ERROR_FAILURE;
219 }
220
221 ////////////////////////////////////////////////////////////////////////////
222 //// nsIDirectoryServiceProvider2
223
224 NS_IMETHODIMP GetFiles(const char* aProperty,
225 nsISimpleEnumerator** _enum) override {
226 // If we were supplied a directory service provider, ask it first.
227 nsCOMPtr<nsIDirectoryServiceProvider2> provider =
228 do_QueryInterface(mDirSvcProvider);
229 if (provider && NS_SUCCEEDED(provider->GetFiles(aProperty, _enum))) {
230 return NS_OK;
231 }
232
233 return NS_ERROR_FAILURE;
234 }
235
236 private:
237 const char* mTestName;
238 nsIServiceManager* mServMgr;
239 nsCOMPtr<nsIDirectoryServiceProvider> mDirSvcProvider;
240 nsCOMPtr<nsIFile> mProfD;
241 nsCOMPtr<nsIFile> mGRED;
242 nsCOMPtr<nsIFile> mGREBinD;
243 };
244
245 NS_IMPL_QUERY_INTERFACE(ScopedXPCOM, nsIDirectoryServiceProvider,
246 nsIDirectoryServiceProvider2)
247
248 NS_IMETHODIMP_(MozExternalRefCountType)
249 ScopedXPCOM::AddRef() { return 2; }
250
251 NS_IMETHODIMP_(MozExternalRefCountType)
252 ScopedXPCOM::Release() { return 1; }
253
254 } // namespace
255
256 #endif // FuzzerTestHarness_h__