Edit

kc3-lang/angle/util/test_utils_unittest.cpp

Branch :

  • Show log

    Commit

  • Author : Jamie Madill
    Date : 2019-10-09 10:26:25
    Hash : 95a736bd
    Message : Reland "Add more test_utils functions." This is a reland of 5fcfcea4a9379633a83a67fc1d94938cb31f2a9c Re-land uses static linking with angle_util. The root cause of the CFI error wasn't solved. Static linking works around the problem by not using any export rules. Second re-land fixes missing imports for libEGL and libGLESv2 static varieties. Original change's description: > Add more test_utils functions. > > Includes methods for creating temporary files, deleting files, and > reading files into a string. Also renames GetPathSeparator to mention > it's only used for environment variables. Includes a new virtual type > angle::Process that will be used to implement cross-platform async > Process launching for tests. Also includes a way to specify a custom > crash handler callback. > > Also adds a few unit tests for the new functionality. They are disabled > on Android because the functions are not needed by the new test runner. > > Bug: angleproject:3162 > Change-Id: I3e2c2e9837608884c98379fa0f78c9ffbe158d73 > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1821940 > Commit-Queue: Jamie Madill <jmadill@chromium.org> > Reviewed-by: Jonah Ryan-Davis <jonahr@google.com> Bug: chromium:1015810 Bug: angleproject:3162 Change-Id: I2a18b819b0f91df610ad12ffedea2b38349fe7cf Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1879859 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: Yuly Novikov <ynovikov@chromium.org>

  • util/test_utils_unittest.cpp
  • //
    // Copyright 2019 The ANGLE Project Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style license that can be
    // found in the LICENSE file.
    
    // test_utils_unittest.cpp: Unit tests for ANGLE's test utility functions
    
    #include "gtest/gtest.h"
    
    #include "common/system_utils.h"
    #include "util/Timer.h"
    #include "util/test_utils.h"
    #include "util/test_utils_unittest_helper.h"
    
    using namespace angle;
    
    namespace
    {
    #if defined(ANGLE_PLATFORM_WINDOWS)
    constexpr char kRunAppHelperExecutable[] = "test_utils_unittest_helper.exe";
    #else
    constexpr char kRunAppHelperExecutable[] = "test_utils_unittest_helper";
    #endif
    
    // Transforms various line endings into C/Unix line endings:
    //
    // - A\nB -> A\nB
    // - A\rB -> A\nB
    // - A\r\nB -> A\nB
    std::string NormalizeNewLines(const std::string &str)
    {
        std::string result;
    
        for (size_t i = 0; i < str.size(); ++i)
        {
            if (str[i] == '\r')
            {
                if (i + 1 < str.size() && str[i + 1] == '\n')
                {
                    ++i;
                }
                result += '\n';
            }
            else
            {
                result += str[i];
            }
        }
    
        return result;
    }
    
    // Tests that Sleep() actually waits some time.
    TEST(TestUtils, Sleep)
    {
        Timer timer;
        timer.start();
        angle::Sleep(500);
        timer.stop();
    
        // Use a slightly fuzzy range
        EXPECT_GT(timer.getElapsedTime(), 0.48);
    }
    
    constexpr uint32_t kMaxPath = 1000;
    
    // Temporary file creation is not supported on Android right now.
    #if defined(ANGLE_PLATFORM_ANDROID)
    #    define MAYBE_CreateAndDeleteTemporaryFile DISABLED_CreateAndDeleteTemporaryFile
    #    define MAYBE_CreateAndDeleteFileInTempDir DISABLED_CreateAndDeleteFileInTempDir
    #else
    #    define MAYBE_CreateAndDeleteTemporaryFile CreateAndDeleteTemporaryFile
    #    define MAYBE_CreateAndDeleteFileInTempDir CreateAndDeleteFileInTempDir
    #endif  // defined(ANGLE_PLATFORM_ANDROID)
    
    // Test creating and deleting temporary file.
    TEST(TestUtils, MAYBE_CreateAndDeleteTemporaryFile)
    {
        char path[kMaxPath] = {};
        ASSERT_TRUE(CreateTemporaryFile(path, kMaxPath));
        ASSERT_TRUE(strlen(path) > 0);
    
        const char kOutputString[] = "test output";
    
        FILE *fp = fopen(path, "wt");
        ASSERT_NE(fp, nullptr);
        int retval = fputs(kOutputString, fp);
        fclose(fp);
    
        EXPECT_GE(retval, 0);
    
        // Test ReadEntireFileToString
        char actualString[kMaxPath];
        EXPECT_TRUE(ReadEntireFileToString(path, actualString, kMaxPath));
        EXPECT_EQ(strcmp(actualString, kOutputString), 0);
    
        // Delete the temporary file.
        EXPECT_TRUE(angle::DeleteFile(path));
    }
    
    // Tests creating and deleting a file in the system temp dir.
    TEST(TestUtils, MAYBE_CreateAndDeleteFileInTempDir)
    {
        char tempDir[kMaxPath];
        ASSERT_TRUE(GetTempDir(tempDir, kMaxPath));
    
        char path[kMaxPath] = {};
        ASSERT_TRUE(CreateTemporaryFileInDir(tempDir, path, kMaxPath));
        ASSERT_TRUE(strlen(path) > 0);
    
        const char kOutputString[] = "test output";
    
        FILE *fp = fopen(path, "wt");
        ASSERT_NE(fp, nullptr);
        int retval = fputs(kOutputString, fp);
        fclose(fp);
    
        EXPECT_GE(retval, 0);
    
        // Test ReadEntireFileToString
        char actualString[kMaxPath];
        EXPECT_TRUE(ReadEntireFileToString(path, actualString, kMaxPath));
        EXPECT_EQ(strcmp(actualString, kOutputString), 0);
    
        // Delete the temporary file.
        EXPECT_TRUE(angle::DeleteFile(path));
    }
    
    // Test running an external application and receiving its output
    TEST(TestUtils, RunApp)
    {
    #if defined(ANGLE_PLATFORM_ANDROID)
        // TODO: android support. http://anglebug.com/3125
        return;
    #endif
    
    #if defined(ANGLE_PLATFORM_FUCHSIA)
        // TODO: fuchsia support. http://anglebug.com/3161
        return;
    #endif
    
        std::string executablePath = GetExecutableDirectory();
        EXPECT_NE(executablePath, "");
        executablePath += "/";
        executablePath += kRunAppHelperExecutable;
    
        std::vector<const char *> args = {executablePath.c_str(), kRunAppTestArg1, kRunAppTestArg2,
                                          nullptr};
    
        // Test that the application can be executed.
        {
            ProcessHandle process(args, true, true);
            EXPECT_TRUE(process->started());
            EXPECT_TRUE(process->finish());
            EXPECT_TRUE(process->finished());
    
            EXPECT_EQ(kRunAppTestStdout, NormalizeNewLines(process->getStdout()));
            EXPECT_EQ(kRunAppTestStderr, NormalizeNewLines(process->getStderr()));
            EXPECT_EQ(EXIT_SUCCESS, process->getExitCode());
        }
    
        // Test that environment variables reach the cild.
        {
            bool setEnvDone = SetEnvironmentVar(kRunAppTestEnvVarName, kRunAppTestEnvVarValue);
            EXPECT_TRUE(setEnvDone);
    
            ProcessHandle process(LaunchProcess(args, true, true));
            EXPECT_TRUE(process->started());
            EXPECT_TRUE(process->finish());
    
            EXPECT_EQ("", process->getStdout());
            EXPECT_EQ(kRunAppTestEnvVarValue, NormalizeNewLines(process->getStderr()));
            EXPECT_EQ(EXIT_SUCCESS, process->getExitCode());
        }
    }
    
    // Verify that NumberOfProcessors returns something sane.
    TEST(TestUtils, NumberOfProcessors)
    {
        int numProcs = angle::NumberOfProcessors();
        EXPECT_GT(numProcs, 0);
        EXPECT_LT(numProcs, 1000);
    }
    }  // namespace