• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 
6 // test_utils_unittest.cpp: Unit tests for ANGLE's test utility functions
7 
8 #include "gtest/gtest.h"
9 
10 #include "common/system_utils.h"
11 #include "util/Timer.h"
12 #include "util/test_utils.h"
13 #include "util/test_utils_unittest_helper.h"
14 
15 using namespace angle;
16 
17 namespace
18 {
19 #if defined(ANGLE_PLATFORM_WINDOWS)
20 constexpr char kRunAppHelperExecutable[] = "test_utils_unittest_helper.exe";
21 #else
22 constexpr char kRunAppHelperExecutable[] = "test_utils_unittest_helper";
23 #endif
24 
25 // Transforms various line endings into C/Unix line endings:
26 //
27 // - A\nB -> A\nB
28 // - A\rB -> A\nB
29 // - A\r\nB -> A\nB
NormalizeNewLines(const std::string & str)30 std::string NormalizeNewLines(const std::string &str)
31 {
32     std::string result;
33 
34     for (size_t i = 0; i < str.size(); ++i)
35     {
36         if (str[i] == '\r')
37         {
38             if (i + 1 < str.size() && str[i + 1] == '\n')
39             {
40                 ++i;
41             }
42             result += '\n';
43         }
44         else
45         {
46             result += str[i];
47         }
48     }
49 
50     return result;
51 }
52 
53 // Tests that Sleep() actually waits some time.
TEST(TestUtils,Sleep)54 TEST(TestUtils, Sleep)
55 {
56     Timer timer;
57     timer.start();
58     angle::Sleep(500);
59     timer.stop();
60 
61     // Use a slightly fuzzy range
62     EXPECT_GT(timer.getElapsedTime(), 0.48);
63 }
64 
65 constexpr uint32_t kMaxPath = 1000;
66 
67 // Temporary file creation is not supported on Android right now.
68 #if defined(ANGLE_PLATFORM_ANDROID)
69 #    define MAYBE_CreateAndDeleteTemporaryFile DISABLED_CreateAndDeleteTemporaryFile
70 #    define MAYBE_CreateAndDeleteFileInTempDir DISABLED_CreateAndDeleteFileInTempDir
71 #else
72 #    define MAYBE_CreateAndDeleteTemporaryFile CreateAndDeleteTemporaryFile
73 #    define MAYBE_CreateAndDeleteFileInTempDir CreateAndDeleteFileInTempDir
74 #endif  // defined(ANGLE_PLATFORM_ANDROID)
75 
76 // Test creating and deleting temporary file.
TEST(TestUtils,MAYBE_CreateAndDeleteTemporaryFile)77 TEST(TestUtils, MAYBE_CreateAndDeleteTemporaryFile)
78 {
79     char path[kMaxPath] = {};
80     ASSERT_TRUE(CreateTemporaryFile(path, kMaxPath));
81     ASSERT_TRUE(strlen(path) > 0);
82 
83     const char kOutputString[] = "test output";
84 
85     FILE *fp = fopen(path, "wt");
86     ASSERT_NE(fp, nullptr);
87     int retval = fputs(kOutputString, fp);
88     fclose(fp);
89 
90     EXPECT_GE(retval, 0);
91 
92     // Test ReadEntireFileToString
93     char actualString[kMaxPath];
94     EXPECT_TRUE(ReadEntireFileToString(path, actualString, kMaxPath));
95     EXPECT_EQ(strcmp(actualString, kOutputString), 0);
96 
97     // Delete the temporary file.
98     EXPECT_TRUE(angle::DeleteFile(path));
99 }
100 
101 // Tests creating and deleting a file in the system temp dir.
TEST(TestUtils,MAYBE_CreateAndDeleteFileInTempDir)102 TEST(TestUtils, MAYBE_CreateAndDeleteFileInTempDir)
103 {
104     char tempDir[kMaxPath];
105     ASSERT_TRUE(GetTempDir(tempDir, kMaxPath));
106 
107     char path[kMaxPath] = {};
108     ASSERT_TRUE(CreateTemporaryFileInDir(tempDir, path, kMaxPath));
109     ASSERT_TRUE(strlen(path) > 0);
110 
111     const char kOutputString[] = "test output";
112 
113     FILE *fp = fopen(path, "wt");
114     ASSERT_NE(fp, nullptr);
115     int retval = fputs(kOutputString, fp);
116     fclose(fp);
117 
118     EXPECT_GE(retval, 0);
119 
120     // Test ReadEntireFileToString
121     char actualString[kMaxPath];
122     EXPECT_TRUE(ReadEntireFileToString(path, actualString, kMaxPath));
123     EXPECT_EQ(strcmp(actualString, kOutputString), 0);
124 
125     // Delete the temporary file.
126     EXPECT_TRUE(angle::DeleteFile(path));
127 }
128 
129 // TODO: android support. http://anglebug.com/3125
130 #if defined(ANGLE_PLATFORM_ANDROID)
131 #    define MAYBE_RunApp DISABLED_RunApp
132 #    define MAYBE_RunAppAsync DISABLED_RunAppAsync
133 // TODO: fuchsia support. http://anglebug.com/3161
134 #elif defined(ANGLE_PLATFORM_FUCHSIA)
135 #    define MAYBE_RunApp DISABLED_RunApp
136 #    define MAYBE_RunAppAsync DISABLED_RunAppAsync
137 #else
138 #    define MAYBE_RunApp RunApp
139 #    define MAYBE_RunAppAsync RunAppAsync
140 #endif  // defined(ANGLE_PLATFORM_ANDROID)
141 
142 // Test running an external application and receiving its output
TEST(TestUtils,MAYBE_RunApp)143 TEST(TestUtils, MAYBE_RunApp)
144 {
145     std::string executablePath = GetExecutableDirectory();
146     EXPECT_NE(executablePath, "");
147     executablePath += "/";
148     executablePath += kRunAppHelperExecutable;
149 
150     std::vector<const char *> args = {executablePath.c_str(), kRunAppTestArg1, kRunAppTestArg2};
151 
152     // Test that the application can be executed.
153     {
154         ProcessHandle process(args, true, true);
155         EXPECT_TRUE(process->started());
156         EXPECT_TRUE(process->finish());
157         EXPECT_TRUE(process->finished());
158 
159         EXPECT_GT(process->getElapsedTimeSeconds(), 0.0);
160         EXPECT_EQ(kRunAppTestStdout, NormalizeNewLines(process->getStdout()));
161         EXPECT_EQ(kRunAppTestStderr, NormalizeNewLines(process->getStderr()));
162         EXPECT_EQ(EXIT_SUCCESS, process->getExitCode());
163     }
164 
165     // Test that environment variables reach the child.
166     {
167         bool setEnvDone = SetEnvironmentVar(kRunAppTestEnvVarName, kRunAppTestEnvVarValue);
168         EXPECT_TRUE(setEnvDone);
169 
170         ProcessHandle process(LaunchProcess(args, true, true));
171         EXPECT_TRUE(process->started());
172         EXPECT_TRUE(process->finish());
173 
174         EXPECT_GT(process->getElapsedTimeSeconds(), 0.0);
175         EXPECT_EQ("", process->getStdout());
176         EXPECT_EQ(kRunAppTestEnvVarValue, NormalizeNewLines(process->getStderr()));
177         EXPECT_EQ(EXIT_SUCCESS, process->getExitCode());
178 
179         // Unset environment var.
180         SetEnvironmentVar(kRunAppTestEnvVarName, "");
181     }
182 }
183 
184 // Test running an external application and receiving its output asynchronously.
TEST(TestUtils,MAYBE_RunAppAsync)185 TEST(TestUtils, MAYBE_RunAppAsync)
186 {
187     std::string executablePath = GetExecutableDirectory();
188     EXPECT_NE(executablePath, "");
189     executablePath += "/";
190     executablePath += kRunAppHelperExecutable;
191 
192     std::vector<const char *> args = {executablePath.c_str(), kRunAppTestArg1, kRunAppTestArg2};
193 
194     // Test that the application can be executed.
195     {
196         ProcessHandle process(args, true, true);
197         EXPECT_TRUE(process->started());
198 
199         constexpr double kTimeout = 3.0;
200 
201         Timer timer;
202         timer.start();
203         while (!process->finished() && timer.getElapsedTime() < kTimeout)
204         {
205             angle::Sleep(1);
206         }
207 
208         EXPECT_TRUE(process->finished());
209         EXPECT_GT(process->getElapsedTimeSeconds(), 0.0);
210         EXPECT_EQ(kRunAppTestStdout, NormalizeNewLines(process->getStdout()));
211         EXPECT_EQ(kRunAppTestStderr, NormalizeNewLines(process->getStderr()));
212         EXPECT_EQ(EXIT_SUCCESS, process->getExitCode());
213     }
214 }
215 
216 // Verify that NumberOfProcessors returns something sane.
TEST(TestUtils,NumberOfProcessors)217 TEST(TestUtils, NumberOfProcessors)
218 {
219     int numProcs = angle::NumberOfProcessors();
220     EXPECT_GT(numProcs, 0);
221     EXPECT_LT(numProcs, 1000);
222 }
223 }  // namespace
224