Path: blob/main_old/util/test_utils_unittest.cpp
1693 views
//1// Copyright 2019 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.45// test_utils_unittest.cpp: Unit tests for ANGLE's test utility functions67#include "gtest/gtest.h"89#include "common/system_utils.h"10#include "util/Timer.h"11#include "util/test_utils.h"12#include "util/test_utils_unittest_helper.h"1314using namespace angle;1516namespace17{18#if defined(ANGLE_PLATFORM_WINDOWS)19constexpr char kRunAppHelperExecutable[] = "test_utils_unittest_helper.exe";20#elif (ANGLE_PLATFORM_IOS)21constexpr char kRunAppHelperExecutable[] =22"../test_utils_unittest_helper.app/test_utils_unittest_helper";23#else24constexpr char kRunAppHelperExecutable[] = "test_utils_unittest_helper";25#endif2627// Transforms various line endings into C/Unix line endings:28//29// - A\nB -> A\nB30// - A\rB -> A\nB31// - A\r\nB -> A\nB32std::string NormalizeNewLines(const std::string &str)33{34std::string result;3536for (size_t i = 0; i < str.size(); ++i)37{38if (str[i] == '\r')39{40if (i + 1 < str.size() && str[i + 1] == '\n')41{42++i;43}44result += '\n';45}46else47{48result += str[i];49}50}5152return result;53}5455// Tests that Sleep() actually waits some time.56TEST(TestUtils, Sleep)57{58Timer timer;59timer.start();60angle::Sleep(500);61timer.stop();6263// Use a slightly fuzzy range64EXPECT_GT(timer.getElapsedTime(), 0.48);65}6667constexpr uint32_t kMaxPath = 1000;6869// Temporary file creation is not supported on Android right now.70#if defined(ANGLE_PLATFORM_ANDROID)71# define MAYBE_CreateAndDeleteTemporaryFile DISABLED_CreateAndDeleteTemporaryFile72# define MAYBE_CreateAndDeleteFileInTempDir DISABLED_CreateAndDeleteFileInTempDir73#else74# define MAYBE_CreateAndDeleteTemporaryFile CreateAndDeleteTemporaryFile75# define MAYBE_CreateAndDeleteFileInTempDir CreateAndDeleteFileInTempDir76#endif // defined(ANGLE_PLATFORM_ANDROID)7778// Test creating and deleting temporary file.79TEST(TestUtils, MAYBE_CreateAndDeleteTemporaryFile)80{81char path[kMaxPath] = {};82ASSERT_TRUE(CreateTemporaryFile(path, kMaxPath));83ASSERT_TRUE(strlen(path) > 0);8485const char kOutputString[] = "test output";8687FILE *fp = fopen(path, "wt");88ASSERT_NE(fp, nullptr);89int retval = fputs(kOutputString, fp);90fclose(fp);9192EXPECT_GE(retval, 0);9394// Test ReadEntireFileToString95char actualString[kMaxPath];96EXPECT_TRUE(ReadEntireFileToString(path, actualString, kMaxPath));97EXPECT_EQ(strcmp(actualString, kOutputString), 0);9899// Delete the temporary file.100EXPECT_TRUE(angle::DeleteFile(path));101}102103// Tests creating and deleting a file in the system temp dir.104TEST(TestUtils, MAYBE_CreateAndDeleteFileInTempDir)105{106char tempDir[kMaxPath];107ASSERT_TRUE(GetTempDir(tempDir, kMaxPath));108109char path[kMaxPath] = {};110ASSERT_TRUE(CreateTemporaryFileInDir(tempDir, path, kMaxPath));111ASSERT_TRUE(strlen(path) > 0);112113const char kOutputString[] = "test output";114115FILE *fp = fopen(path, "wt");116ASSERT_NE(fp, nullptr);117int retval = fputs(kOutputString, fp);118fclose(fp);119120EXPECT_GE(retval, 0);121122// Test ReadEntireFileToString123char actualString[kMaxPath];124EXPECT_TRUE(ReadEntireFileToString(path, actualString, kMaxPath));125EXPECT_EQ(strcmp(actualString, kOutputString), 0);126127// Delete the temporary file.128EXPECT_TRUE(angle::DeleteFile(path));129}130131// TODO: android support. http://anglebug.com/3125132#if defined(ANGLE_PLATFORM_ANDROID)133# define MAYBE_RunApp DISABLED_RunApp134# define MAYBE_RunAppAsync DISABLED_RunAppAsync135# define MAYBE_RunAppAsyncRedirectStderrToStdout DISABLED_RunAppAsyncRedirectStderrToStdout136// TODO: fuchsia support. http://anglebug.com/3161137#elif defined(ANGLE_PLATFORM_FUCHSIA)138# define MAYBE_RunApp DISABLED_RunApp139# define MAYBE_RunAppAsync DISABLED_RunAppAsync140# define MAYBE_RunAppAsyncRedirectStderrToStdout DISABLED_RunAppAsyncRedirectStderrToStdout141#else142# define MAYBE_RunApp RunApp143# define MAYBE_RunAppAsync RunAppAsync144# define MAYBE_RunAppAsyncRedirectStderrToStdout RunAppAsyncRedirectStderrToStdout145#endif // defined(ANGLE_PLATFORM_ANDROID)146147// Test running an external application and receiving its output148TEST(TestUtils, MAYBE_RunApp)149{150std::string executablePath = GetExecutableDirectory();151EXPECT_NE(executablePath, "");152executablePath += "/";153executablePath += kRunAppHelperExecutable;154155std::vector<const char *> args = {executablePath.c_str(), kRunAppTestArg1, kRunAppTestArg2};156157// Test that the application can be executed.158{159ProcessHandle process(args, ProcessOutputCapture::StdoutAndStderrSeparately);160EXPECT_TRUE(process->started());161EXPECT_TRUE(process->finish());162EXPECT_TRUE(process->finished());163164EXPECT_GT(process->getElapsedTimeSeconds(), 0.0);165EXPECT_EQ(kRunAppTestStdout, NormalizeNewLines(process->getStdout()));166EXPECT_EQ(kRunAppTestStderr, NormalizeNewLines(process->getStderr()));167EXPECT_EQ(EXIT_SUCCESS, process->getExitCode());168}169170// Test that environment variables reach the child.171{172bool setEnvDone = SetEnvironmentVar(kRunAppTestEnvVarName, kRunAppTestEnvVarValue);173EXPECT_TRUE(setEnvDone);174175ProcessHandle process(LaunchProcess(args, ProcessOutputCapture::StdoutAndStderrSeparately));176EXPECT_TRUE(process->started());177EXPECT_TRUE(process->finish());178179EXPECT_GT(process->getElapsedTimeSeconds(), 0.0);180EXPECT_EQ("", process->getStdout());181EXPECT_EQ(kRunAppTestEnvVarValue, NormalizeNewLines(process->getStderr()));182EXPECT_EQ(EXIT_SUCCESS, process->getExitCode());183184// Unset environment var.185SetEnvironmentVar(kRunAppTestEnvVarName, "");186}187}188189// Test running an external application and receiving its output asynchronously.190TEST(TestUtils, MAYBE_RunAppAsync)191{192std::string executablePath = GetExecutableDirectory();193EXPECT_NE(executablePath, "");194executablePath += "/";195executablePath += kRunAppHelperExecutable;196197std::vector<const char *> args = {executablePath.c_str(), kRunAppTestArg1, kRunAppTestArg2};198199// Test that the application can be executed and the output is captured correctly.200{201ProcessHandle process(args, ProcessOutputCapture::StdoutAndStderrSeparately);202EXPECT_TRUE(process->started());203204constexpr double kTimeout = 3.0;205206Timer timer;207timer.start();208while (!process->finished() && timer.getElapsedTime() < kTimeout)209{210angle::Sleep(1);211}212213EXPECT_TRUE(process->finished());214EXPECT_GT(process->getElapsedTimeSeconds(), 0.0);215EXPECT_EQ(kRunAppTestStdout, NormalizeNewLines(process->getStdout()));216EXPECT_EQ(kRunAppTestStderr, NormalizeNewLines(process->getStderr()));217EXPECT_EQ(EXIT_SUCCESS, process->getExitCode());218}219}220221// Test running an external application and receiving its stdout and stderr output interleaved.222TEST(TestUtils, MAYBE_RunAppAsyncRedirectStderrToStdout)223{224std::string executablePath = GetExecutableDirectory();225EXPECT_NE(executablePath, "");226executablePath += "/";227executablePath += kRunAppHelperExecutable;228229std::vector<const char *> args = {executablePath.c_str(), kRunAppTestArg1, kRunAppTestArg2};230231// Test that the application can be executed and the output is captured correctly.232{233ProcessHandle process(args, ProcessOutputCapture::StdoutAndStderrInterleaved);234EXPECT_TRUE(process->started());235236constexpr double kTimeout = 3.0;237238Timer timer;239timer.start();240while (!process->finished() && timer.getElapsedTime() < kTimeout)241{242angle::Sleep(1);243}244245EXPECT_TRUE(process->finished());246EXPECT_GT(process->getElapsedTimeSeconds(), 0.0);247EXPECT_EQ(std::string(kRunAppTestStdout) + kRunAppTestStderr,248NormalizeNewLines(process->getStdout()));249EXPECT_EQ("", process->getStderr());250EXPECT_EQ(EXIT_SUCCESS, process->getExitCode());251}252}253254// Verify that NumberOfProcessors returns something reasonable.255TEST(TestUtils, NumberOfProcessors)256{257int numProcs = angle::NumberOfProcessors();258EXPECT_GT(numProcs, 0);259EXPECT_LT(numProcs, 1000);260}261} // namespace262263264