1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "android/opengl/EmuglBackendScanner.h"
16
17 #include "android/base/system/System.h"
18 #include "android/base/testing/TestSystem.h"
19 #include "android/base/testing/TestTempDir.h"
20 #include "android/utils/path.h"
21
22 #include <gtest/gtest.h>
23
24 #include <string>
25 #include <vector>
26
27 #include <fcntl.h>
28 #ifndef _MSC_VER
29 #include <unistd.h>
30 #endif
31
32 using android::base::System;
33 using android::base::TestSystem;
34 using android::base::TestTempDir;
35
make_dir(const std::string & path)36 static void make_dir(const std::string& path) {
37 EXPECT_EQ(0, ::android_mkdir(path.c_str(), 0755));
38 }
39
make_subdir(const std::string & path,const char * subdir)40 static void make_subdir(const std::string& path, const char* subdir) {
41 std::string dir = path;
42 dir.append("/");
43 dir.append(subdir);
44 make_dir(dir);
45 }
46
make_subfile(const std::string & dir,const char * file)47 static void make_subfile(const std::string& dir, const char* file) {
48 std::string path = dir;
49 path.append("/");
50 path.append(file);
51 int fd = ::open(path.c_str(), O_WRONLY|O_CREAT, 0755);
52 EXPECT_GE(fd, 0);
53 ::close(fd);
54 }
55
56 namespace android {
57 namespace opengl {
58
TEST(EmuglBackendScanner,noLibDir)59 TEST(EmuglBackendScanner, noLibDir) {
60 TestTempDir myDir("emugl_backend_scanner");
61 // Don't create any files
62 std::vector<std::string> names = EmuglBackendScanner::scanDir(myDir.path());
63 EXPECT_TRUE(names.empty());
64 }
65
TEST(EmuglBackendScanner,noBackends)66 TEST(EmuglBackendScanner, noBackends) {
67 TestTempDir myDir("emugl_backend_scanner");
68 // Create lib directory.
69 std::string libDir(myDir.path());
70 libDir += "/";
71 libDir += System::kLibSubDir;
72 make_dir(libDir);
73
74 // Don't create any files
75 std::vector<std::string> names = EmuglBackendScanner::scanDir(myDir.path());
76 EXPECT_TRUE(names.empty());
77 }
78
TEST(EmuglBackendScanner,listBackends)79 TEST(EmuglBackendScanner, listBackends) {
80 TestSystem testSys("foo", System::kProgramBitness);
81 TestTempDir* myDir = testSys.getTempRoot();
82
83 // Create lib directory.
84 std::string libDir(myDir->path());
85 libDir += "/foo/";
86 make_dir(libDir);
87 libDir += System::kLibSubDir;
88 make_dir(libDir);
89
90 // Create first backend sub-directory.
91
92 // to be ignored, doesn't begin with 'gles_'
93 make_subdir(libDir, "first");
94
95 // second entry for the backend named 'second'
96 make_subdir(libDir, "gles_second");
97
98 // should be ignored, must have something after 'gles_' prefix.
99 make_subdir(libDir, "gles_");
100
101 // should be ignored: is a file, not a directory.
102 make_subfile(libDir, "gles_fourth");
103
104 // should be the first returned backend, due to alphabetical order.
105 make_subdir(libDir, "gles_fifth");
106
107 // should be returned as the third backend, due to alphabetical order.
108 make_subdir(libDir, "gles_sixth");
109
110 // Now check the scanner
111 std::vector<std::string> names = EmuglBackendScanner::scanDir(PATH_SEP "foo");
112 ASSERT_EQ(3U, names.size());
113 EXPECT_STREQ("fifth", names[0].c_str());
114 EXPECT_STREQ("second", names[1].c_str());
115 EXPECT_STREQ("sixth", names[2].c_str());
116 }
117
TEST(EmuglBackendScanner,listBackendsWithProgramBitness)118 TEST(EmuglBackendScanner, listBackendsWithProgramBitness) {
119 TestSystem testSys("foo", 32);
120 TestTempDir* myDir = testSys.getTempRoot();
121
122 myDir->makeSubDir("foo");
123 myDir->makeSubDir("foo/lib");
124 myDir->makeSubDir("foo/lib/gles_first");
125 myDir->makeSubDir("foo/lib/gles_second");
126 myDir->makeSubDir("foo/lib/gles_third");
127
128 myDir->makeSubDir("foo/lib64");
129 myDir->makeSubDir("foo/lib64/gles_fourth");
130 myDir->makeSubDir("foo/lib64/gles_fifth");
131 myDir->makeSubDir("foo/lib64/gles_sixth");
132
133 std::vector<std::string> names = EmuglBackendScanner::scanDir(PATH_SEP "foo", 32);
134 ASSERT_EQ(3U, names.size());
135 EXPECT_STREQ("first", names[0].c_str());
136 EXPECT_STREQ("second", names[1].c_str());
137 EXPECT_STREQ("third", names[2].c_str());
138
139 names = EmuglBackendScanner::scanDir(PATH_SEP "foo", 64);
140 ASSERT_EQ(3U, names.size());
141 EXPECT_STREQ("fifth", names[0].c_str());
142 EXPECT_STREQ("fourth", names[1].c_str());
143 EXPECT_STREQ("sixth", names[2].c_str());
144 }
145
146 } // namespace opengl
147 } // namespace android
148