Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / toolkit / crashreporter / test / TestCrashReporterAPI.cpp
blob84c4d52629f2e82b57838ef209bcfa443f464251
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla Breakpad integration
17 * The Initial Developer of the Original Code is
18 * Ted Mielczarek <ted.mielczarek@gmail.com>
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include <stdlib.h>
39 #include <stdio.h>
41 #include "prenv.h"
42 #include "nsIComponentManager.h"
43 #include "nsISimpleEnumerator.h"
44 #include "nsIServiceManager.h"
45 #include "nsServiceManagerUtils.h"
46 #include "nsDirectoryServiceDefs.h"
47 #include "nsIProperties.h"
48 #include "nsCOMPtr.h"
49 #include "nsILocalFile.h"
51 #include "nsExceptionHandler.h"
53 // Defined in nsExceptionHandler.cpp, but not normally exposed
54 namespace CrashReporter {
55 bool GetAnnotation(const nsACString& key, nsACString& data);
58 #define ok(message, test) do { \
59 if (!(test)) \
60 return message; \
61 } while (0)
62 #define equals(message, a, b) ok(message, a == b)
63 #define ok_nsresult(message, rv) ok(message, NS_SUCCEEDED(rv))
64 #define fail_nsresult(message, rv) ok(message, NS_FAILED(rv))
65 #define run_test(test) do { char *message = test(); tests_run++; \
66 if (message) return message; } while (0)
67 int tests_run;
71 char *
72 test_init_exception_handler()
74 nsCOMPtr<nsILocalFile> lf;
75 // we don't plan on launching the crash reporter in this app anyway,
76 // so it's ok to pass a bogus nsILocalFile
77 ok_nsresult("NS_NewNativeLocalFile", NS_NewNativeLocalFile(EmptyCString(),
78 PR_TRUE,
79 getter_AddRefs(lf)));
81 ok_nsresult("CrashReporter::SetExceptionHandler",
82 CrashReporter::SetExceptionHandler(lf, nsnull));
83 return 0;
86 char *
87 test_set_minidump_path()
89 nsresult rv;
90 nsCOMPtr<nsIProperties> directoryService =
91 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
93 ok_nsresult("do_GetService", rv);
95 nsCOMPtr<nsILocalFile> currentDirectory;
96 rv = directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR,
97 NS_GET_IID(nsILocalFile),
98 getter_AddRefs(currentDirectory));
99 ok_nsresult("directoryService->Get", rv);
101 nsAutoString currentDirectoryPath;
102 rv = currentDirectory->GetPath(currentDirectoryPath);
103 ok_nsresult("currentDirectory->GetPath", rv);
105 ok_nsresult("CrashReporter::SetMinidumpPath",
106 CrashReporter::SetMinidumpPath(currentDirectoryPath));
108 return 0;
111 char *
112 test_annotate_crash_report_basic()
114 ok_nsresult("CrashReporter::AnnotateCrashReport: basic 1",
115 CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("test"),
116 NS_LITERAL_CSTRING("some data")));
119 nsCAutoString result;
120 ok("CrashReporter::GetAnnotation", CrashReporter::GetAnnotation(NS_LITERAL_CSTRING("test"),
121 result));
122 nsCString msg = result + NS_LITERAL_CSTRING(" == ") +
123 NS_LITERAL_CSTRING("some data");
124 equals((char*)PromiseFlatCString(msg).get(), result,
125 NS_LITERAL_CSTRING("some data"));
127 // now replace it with something else
128 ok_nsresult("CrashReporter::AnnotateCrashReport: basic 2",
129 CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("test"),
130 NS_LITERAL_CSTRING("some other data")));
133 ok("CrashReporter::GetAnnotation", CrashReporter::GetAnnotation(NS_LITERAL_CSTRING("test"),
134 result));
135 msg = result + NS_LITERAL_CSTRING(" == ") +
136 NS_LITERAL_CSTRING("some other data");
137 equals((char*)PromiseFlatCString(msg).get(), result,
138 NS_LITERAL_CSTRING("some other data"));
139 return 0;
142 char *
143 test_appendnotes_crash_report()
145 // Append two notes
146 ok_nsresult("CrashReporter::AppendAppNotesToCrashReport: 1",
147 CrashReporter::AppendAppNotesToCrashReport(NS_LITERAL_CSTRING("some data")));
150 ok_nsresult("CrashReporter::AppendAppNotesToCrashReport: 2",
151 CrashReporter::AppendAppNotesToCrashReport(NS_LITERAL_CSTRING("some other data")));
153 // ensure that the result is correct
154 nsCAutoString result;
155 ok("CrashReporter::GetAnnotation",
156 CrashReporter::GetAnnotation(NS_LITERAL_CSTRING("Notes"),
157 result));
159 nsCString msg = result + NS_LITERAL_CSTRING(" == ") +
160 NS_LITERAL_CSTRING("some datasome other data");
161 equals((char*)PromiseFlatCString(msg).get(), result,
162 NS_LITERAL_CSTRING("some datasome other data"));
163 return 0;
166 char *
167 test_annotate_crash_report_invalid_equals()
169 fail_nsresult("CrashReporter::AnnotateCrashReport: invalid = in key",
170 CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("test=something"),
171 NS_LITERAL_CSTRING("some data")));
172 return 0;
175 char *
176 test_annotate_crash_report_invalid_cr()
178 fail_nsresult("CrashReporter::AnnotateCrashReport: invalid \n in key",
179 CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("test\nsomething"),
180 NS_LITERAL_CSTRING("some data")));
181 return 0;
184 char *
185 test_unset_exception_handler()
187 ok_nsresult("CrashReporter::UnsetExceptionHandler",
188 CrashReporter::UnsetExceptionHandler());
189 return 0;
192 static char* all_tests()
194 run_test(test_init_exception_handler);
195 run_test(test_set_minidump_path);
196 run_test(test_annotate_crash_report_basic);
197 run_test(test_annotate_crash_report_invalid_equals);
198 run_test(test_annotate_crash_report_invalid_cr);
199 run_test(test_appendnotes_crash_report);
200 run_test(test_unset_exception_handler);
201 return 0;
205 main (int argc, char **argv)
207 NS_InitXPCOM2(nsnull, nsnull, nsnull);
209 PR_SetEnv("MOZ_CRASHREPORTER=1");
211 char* result = all_tests();
212 if (result != 0) {
213 printf("TEST-UNEXPECTED-FAIL | %s | %s\n", __FILE__, result);
215 else {
216 printf("TEST-PASS | %s | all tests passed\n", __FILE__);
218 printf("Tests run: %d\n", tests_run);
220 NS_ShutdownXPCOM(nsnull);
222 return result != 0;