10c44256ae45968080c562aaf743f63019f580763Josh Gao/* 20c44256ae45968080c562aaf743f63019f580763Josh Gao * Copyright (C) 2016 The Android Open Source Project 30c44256ae45968080c562aaf743f63019f580763Josh Gao * 40c44256ae45968080c562aaf743f63019f580763Josh Gao * Licensed under the Apache License, Version 2.0 (the "License"); 50c44256ae45968080c562aaf743f63019f580763Josh Gao * you may not use this file except in compliance with the License. 60c44256ae45968080c562aaf743f63019f580763Josh Gao * You may obtain a copy of the License at 70c44256ae45968080c562aaf743f63019f580763Josh Gao * 80c44256ae45968080c562aaf743f63019f580763Josh Gao * http://www.apache.org/licenses/LICENSE-2.0 90c44256ae45968080c562aaf743f63019f580763Josh Gao * 100c44256ae45968080c562aaf743f63019f580763Josh Gao * Unless required by applicable law or agreed to in writing, software 110c44256ae45968080c562aaf743f63019f580763Josh Gao * distributed under the License is distributed on an "AS IS" BASIS, 120c44256ae45968080c562aaf743f63019f580763Josh Gao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130c44256ae45968080c562aaf743f63019f580763Josh Gao * See the License for the specific language governing permissions and 140c44256ae45968080c562aaf743f63019f580763Josh Gao * limitations under the License. 150c44256ae45968080c562aaf743f63019f580763Josh Gao */ 160c44256ae45968080c562aaf743f63019f580763Josh Gao 170c44256ae45968080c562aaf743f63019f580763Josh Gao#include "android-base/quick_exit.h" 180c44256ae45968080c562aaf743f63019f580763Josh Gao 190c44256ae45968080c562aaf743f63019f580763Josh Gao#include <gtest/gtest.h> 200c44256ae45968080c562aaf743f63019f580763Josh Gao 210c44256ae45968080c562aaf743f63019f580763Josh Gao#include <errno.h> 220c44256ae45968080c562aaf743f63019f580763Josh Gao#include <fcntl.h> 230c44256ae45968080c562aaf743f63019f580763Josh Gao#include <unistd.h> 240c44256ae45968080c562aaf743f63019f580763Josh Gao 250c44256ae45968080c562aaf743f63019f580763Josh Gao#include <string> 260c44256ae45968080c562aaf743f63019f580763Josh Gao 270c44256ae45968080c562aaf743f63019f580763Josh Gao#include "android-base/test_utils.h" 280c44256ae45968080c562aaf743f63019f580763Josh Gao 290c44256ae45968080c562aaf743f63019f580763Josh Gao// These tests are a bit sketchy, since each test run adds global state that affects subsequent 300c44256ae45968080c562aaf743f63019f580763Josh Gao// tests (including ones not in this file!). Exit with 0 in Exiter and stick the at_quick_exit test 310c44256ae45968080c562aaf743f63019f580763Josh Gao// at the end to hack around this. 320c44256ae45968080c562aaf743f63019f580763Josh Gaostruct Exiter { 330c44256ae45968080c562aaf743f63019f580763Josh Gao ~Exiter() { 340c44256ae45968080c562aaf743f63019f580763Josh Gao _Exit(0); 350c44256ae45968080c562aaf743f63019f580763Josh Gao } 360c44256ae45968080c562aaf743f63019f580763Josh Gao}; 370c44256ae45968080c562aaf743f63019f580763Josh Gao 380c44256ae45968080c562aaf743f63019f580763Josh GaoTEST(quick_exit, smoke) { 390c44256ae45968080c562aaf743f63019f580763Josh Gao ASSERT_EXIT(android::base::quick_exit(123), testing::ExitedWithCode(123), ""); 400c44256ae45968080c562aaf743f63019f580763Josh Gao} 410c44256ae45968080c562aaf743f63019f580763Josh Gao 420c44256ae45968080c562aaf743f63019f580763Josh GaoTEST(quick_exit, skip_static_destructors) { 430c44256ae45968080c562aaf743f63019f580763Josh Gao static Exiter exiter; 440c44256ae45968080c562aaf743f63019f580763Josh Gao ASSERT_EXIT(android::base::quick_exit(123), testing::ExitedWithCode(123), ""); 450c44256ae45968080c562aaf743f63019f580763Josh Gao} 460c44256ae45968080c562aaf743f63019f580763Josh Gao 470c44256ae45968080c562aaf743f63019f580763Josh GaoTEST(quick_exit, at_quick_exit) { 480c44256ae45968080c562aaf743f63019f580763Josh Gao static int counter = 4; 490c44256ae45968080c562aaf743f63019f580763Josh Gao // "Functions passed to at_quick_exit are called in reverse order of their registration." 500c44256ae45968080c562aaf743f63019f580763Josh Gao ASSERT_EQ(0, android::base::at_quick_exit([]() { _exit(counter); })); 510c44256ae45968080c562aaf743f63019f580763Josh Gao ASSERT_EQ(0, android::base::at_quick_exit([]() { counter += 2; })); 520c44256ae45968080c562aaf743f63019f580763Josh Gao ASSERT_EQ(0, android::base::at_quick_exit([]() { counter *= 10; })); 530c44256ae45968080c562aaf743f63019f580763Josh Gao ASSERT_EXIT(android::base::quick_exit(123), testing::ExitedWithCode(42), ""); 540c44256ae45968080c562aaf743f63019f580763Josh Gao} 55