xref: /aosp_15_r20/external/google-breakpad/src/client/mac/handler/testcases/DynamicImagesTests.cc (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2008 Google LLC
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following disclaimer
10 // in the documentation and/or other materials provided with the
11 // distribution.
12 //     * Neither the name of Google LLC nor the names of its
13 // contributors may be used to endorse or promote products derived from
14 // this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 //
29 //  DynamicImagesTests.cpp
30 //  minidump_test
31 //
32 //  Created by Neal Sidhwaney on 4/17/08.
33 //  Copyright 2008 Google LLC
34 //
35 
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>  // Must come first
38 #endif
39 
40 #include "client/mac/handler/testcases/DynamicImagesTests.h"
41 #include "client/mac/handler/dynamic_images.h"
42 
43 DynamicImagesTests test2(TEST_INVOCATION(DynamicImagesTests,
44                                          ReadTaskMemoryTest));
45 DynamicImagesTests test3(TEST_INVOCATION(DynamicImagesTests,
46                                          ReadLibrariesFromLocalTaskTest));
47 
DynamicImagesTests(TestInvocation * invocation)48 DynamicImagesTests::DynamicImagesTests(TestInvocation* invocation)
49     : TestCase(invocation) {
50 }
51 
~DynamicImagesTests()52 DynamicImagesTests::~DynamicImagesTests() {
53 }
54 
ReadTaskMemoryTest()55 void DynamicImagesTests::ReadTaskMemoryTest() {
56   kern_return_t kr;
57 
58   // pick test2 as a symbol we know to be valid to read
59   // anything will work, really
60   void* addr = reinterpret_cast<void*>(&test2);
61   std::vector<uint8_t> buf(getpagesize());
62 
63   fprintf(stderr, "reading 0x%p\n", addr);
64   kr = google_breakpad::ReadTaskMemory(mach_task_self(),
65                                        (uint64_t)addr,
66                                        getpagesize(),
67                                        buf);
68 
69   CPTAssert(kr == KERN_SUCCESS);
70 
71   CPTAssert(0 == memcmp(&buf[0], (const void*)addr, getpagesize()));
72 }
73 
ReadLibrariesFromLocalTaskTest()74 void DynamicImagesTests::ReadLibrariesFromLocalTaskTest() {
75 
76   mach_port_t me = mach_task_self();
77   google_breakpad::DynamicImages* d = new google_breakpad::DynamicImages(me);
78 
79   fprintf(stderr,"Local task image count: %d\n", d->GetImageCount());
80 
81   CPTAssert(d->GetImageCount() > 0);
82 }
83