| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 // validation. | 57 // validation. |
| 58 void TestGetExceptionPorts(const ExceptionPorts& exception_ports, | 58 void TestGetExceptionPorts(const ExceptionPorts& exception_ports, |
| 59 mach_port_t expect_port, | 59 mach_port_t expect_port, |
| 60 exception_behavior_t expect_behavior) { | 60 exception_behavior_t expect_behavior) { |
| 61 const exception_mask_t kExceptionMask = EXC_MASK_CRASH; | 61 const exception_mask_t kExceptionMask = EXC_MASK_CRASH; |
| 62 | 62 |
| 63 thread_state_flavor_t expect_flavor = (expect_behavior == EXCEPTION_DEFAULT) | 63 thread_state_flavor_t expect_flavor = (expect_behavior == EXCEPTION_DEFAULT) |
| 64 ? THREAD_STATE_NONE | 64 ? THREAD_STATE_NONE |
| 65 : MACHINE_THREAD_STATE; | 65 : MACHINE_THREAD_STATE; |
| 66 | 66 |
| 67 std::vector<ExceptionPorts::ExceptionHandler> crash_handler; | 67 ExceptionPorts::ExceptionHandlerVector crash_handler; |
| 68 ASSERT_TRUE( | 68 ASSERT_TRUE( |
| 69 exception_ports.GetExceptionPorts(kExceptionMask, &crash_handler)); | 69 exception_ports.GetExceptionPorts(kExceptionMask, &crash_handler)); |
| 70 | 70 |
| 71 if (expect_port != MACH_PORT_NULL) { | 71 if (expect_port != MACH_PORT_NULL) { |
| 72 ASSERT_EQ(1u, crash_handler.size()); | 72 ASSERT_EQ(1u, crash_handler.size()); |
| 73 base::mac::ScopedMachSendRight port_owner(crash_handler[0].port); | |
| 74 | 73 |
| 75 EXPECT_EQ(kExceptionMask, crash_handler[0].mask); | 74 EXPECT_EQ(kExceptionMask, crash_handler[0].mask); |
| 76 EXPECT_EQ(expect_port, crash_handler[0].port); | 75 EXPECT_EQ(expect_port, crash_handler[0].port); |
| 77 EXPECT_EQ(expect_behavior, crash_handler[0].behavior); | 76 EXPECT_EQ(expect_behavior, crash_handler[0].behavior); |
| 78 EXPECT_EQ(expect_flavor, crash_handler[0].flavor); | 77 EXPECT_EQ(expect_flavor, crash_handler[0].flavor); |
| 79 } else { | 78 } else { |
| 80 EXPECT_TRUE(crash_handler.empty()); | 79 EXPECT_TRUE(crash_handler.empty()); |
| 81 } | 80 } |
| 82 | 81 |
| 83 std::vector<ExceptionPorts::ExceptionHandler> handlers; | 82 ExceptionPorts::ExceptionHandlerVector handlers; |
| 84 ASSERT_TRUE(exception_ports.GetExceptionPorts(ExcMaskValid(), &handlers)); | 83 ASSERT_TRUE(exception_ports.GetExceptionPorts(ExcMaskValid(), &handlers)); |
| 85 | 84 |
| 86 EXPECT_GE(handlers.size(), crash_handler.size()); | 85 EXPECT_GE(handlers.size(), crash_handler.size()); |
| 87 bool found = false; | 86 bool found = false; |
| 88 for (const ExceptionPorts::ExceptionHandler& handler : handlers) { | 87 for (const ExceptionPorts::ExceptionHandler& handler : handlers) { |
| 89 if ((handler.mask & kExceptionMask) != 0) { | 88 if ((handler.mask & kExceptionMask) != 0) { |
| 90 base::mac::ScopedMachSendRight port_owner(handler.port); | |
| 91 | |
| 92 EXPECT_FALSE(found); | 89 EXPECT_FALSE(found); |
| 93 found = true; | 90 found = true; |
| 94 EXPECT_EQ(expect_port, handler.port); | 91 EXPECT_EQ(expect_port, handler.port); |
| 95 EXPECT_EQ(expect_behavior, handler.behavior); | 92 EXPECT_EQ(expect_behavior, handler.behavior); |
| 96 EXPECT_EQ(expect_flavor, handler.flavor); | 93 EXPECT_EQ(expect_flavor, handler.flavor); |
| 97 } | 94 } |
| 98 } | 95 } |
| 99 | 96 |
| 100 if (expect_port != MACH_PORT_NULL) { | 97 if (expect_port != MACH_PORT_NULL) { |
| 101 EXPECT_TRUE(found); | 98 EXPECT_TRUE(found); |
| (...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 | 576 |
| 580 TEST(ExceptionPorts, HostExceptionPorts) { | 577 TEST(ExceptionPorts, HostExceptionPorts) { |
| 581 // ExceptionPorts isn’t expected to work as non-root. Just do a quick test to | 578 // ExceptionPorts isn’t expected to work as non-root. Just do a quick test to |
| 582 // make sure that TargetTypeName() returns the right string, and that the | 579 // make sure that TargetTypeName() returns the right string, and that the |
| 583 // underlying host_get_exception_ports() function appears to be called by | 580 // underlying host_get_exception_ports() function appears to be called by |
| 584 // looking for a KERN_INVALID_ARGUMENT return value. Or, on the off chance | 581 // looking for a KERN_INVALID_ARGUMENT return value. Or, on the off chance |
| 585 // that the test is being run as root, just look for KERN_SUCCESS. | 582 // that the test is being run as root, just look for KERN_SUCCESS. |
| 586 // host_set_exception_ports() is not tested, because if the test were running | 583 // host_set_exception_ports() is not tested, because if the test were running |
| 587 // as root and the call succeeded, it would have global effects. | 584 // as root and the call succeeded, it would have global effects. |
| 588 | 585 |
| 586 const bool expect_success = geteuid() == 0; |
| 587 |
| 589 base::mac::ScopedMachSendRight host(mach_host_self()); | 588 base::mac::ScopedMachSendRight host(mach_host_self()); |
| 590 ExceptionPorts explicit_host_ports(ExceptionPorts::kTargetTypeHost, host); | 589 ExceptionPorts explicit_host_ports(ExceptionPorts::kTargetTypeHost, host); |
| 591 EXPECT_STREQ("host", explicit_host_ports.TargetTypeName()); | 590 EXPECT_STREQ("host", explicit_host_ports.TargetTypeName()); |
| 592 | 591 |
| 593 std::vector<ExceptionPorts::ExceptionHandler> handlers; | 592 ExceptionPorts::ExceptionHandlerVector explicit_handlers; |
| 594 bool rv = explicit_host_ports.GetExceptionPorts(ExcMaskValid(), &handlers); | 593 bool rv = |
| 595 if (geteuid() == 0) { | 594 explicit_host_ports.GetExceptionPorts(ExcMaskValid(), &explicit_handlers); |
| 596 EXPECT_TRUE(rv); | 595 EXPECT_EQ(expect_success, rv); |
| 597 } else { | |
| 598 EXPECT_FALSE(rv); | |
| 599 } | |
| 600 | 596 |
| 601 ExceptionPorts implicit_host_ports(ExceptionPorts::kTargetTypeHost, | 597 ExceptionPorts implicit_host_ports(ExceptionPorts::kTargetTypeHost, |
| 602 HOST_NULL); | 598 HOST_NULL); |
| 603 EXPECT_STREQ("host", implicit_host_ports.TargetTypeName()); | 599 EXPECT_STREQ("host", implicit_host_ports.TargetTypeName()); |
| 604 | 600 |
| 605 rv = implicit_host_ports.GetExceptionPorts(ExcMaskValid(), &handlers); | 601 ExceptionPorts::ExceptionHandlerVector implicit_handlers; |
| 606 if (geteuid() == 0) { | 602 rv = |
| 607 EXPECT_TRUE(rv); | 603 implicit_host_ports.GetExceptionPorts(ExcMaskValid(), &implicit_handlers); |
| 608 } else { | 604 EXPECT_EQ(expect_success, rv); |
| 609 EXPECT_FALSE(rv); | 605 |
| 610 } | 606 EXPECT_EQ(explicit_handlers.size(), implicit_handlers.size()); |
| 611 } | 607 } |
| 612 | 608 |
| 613 } // namespace | 609 } // namespace |
| 614 } // namespace test | 610 } // namespace test |
| 615 } // namespace crashpad | 611 } // namespace crashpad |
| OLD | NEW |