xref: /aosp_15_r20/external/cronet/base/apple/call_with_eh_frame_unittest.mm (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1// Copyright 2015 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/apple/call_with_eh_frame.h"
6
7#import <Foundation/Foundation.h>
8
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace base::apple {
12namespace {
13
14class CallWithEHFrameTest : public testing::Test {
15 protected:
16  void ThrowException() {
17    @throw [NSException exceptionWithName:@"TestException"
18                                   reason:@"Testing exceptions"
19                                 userInfo:nil];
20  }
21};
22
23// Catching from within the EHFrame is allowed.
24TEST_F(CallWithEHFrameTest, CatchExceptionHigher) {
25  bool __block saw_exception = false;
26  base::apple::CallWithEHFrame(^{
27    @try {
28      ThrowException();
29    } @catch (NSException* exception) {
30      saw_exception = true;
31    }
32  });
33  EXPECT_TRUE(saw_exception);
34}
35
36// Trying to catch an exception outside the EHFrame is blocked.
37TEST_F(CallWithEHFrameTest, CatchExceptionLower) {
38  auto catch_exception_lower = ^{
39    bool saw_exception = false;
40    @try {
41      base::apple::CallWithEHFrame(^{
42        ThrowException();
43      });
44    } @catch (NSException* exception) {
45      saw_exception = true;
46    }
47    ASSERT_FALSE(saw_exception);
48  };
49  EXPECT_DEATH(catch_exception_lower(), "");
50}
51
52}  // namespace
53}  // namespace base::apple
54