1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <string>
18
19 #include <android-base/test_utils.h>
20 #include <gtest/gtest.h>
21
22 #include "adb_utils.h"
23 #include "sysdeps.h"
24
TEST(sysdeps,stat)25 TEST(sysdeps, stat) {
26 TemporaryDir td;
27 TemporaryFile tf;
28
29 struct stat st;
30 ASSERT_EQ(0, stat(td.path, &st));
31 ASSERT_FALSE(S_ISREG(st.st_mode));
32 ASSERT_TRUE(S_ISDIR(st.st_mode));
33
34 ASSERT_EQ(0, stat((std::string(td.path) + '/').c_str(), &st));
35 ASSERT_TRUE(S_ISDIR(st.st_mode));
36
37 #if defined(_WIN32)
38 ASSERT_EQ(0, stat((std::string(td.path) + '\\').c_str(), &st));
39 ASSERT_TRUE(S_ISDIR(st.st_mode));
40 #endif
41
42 std::string nonexistent_path = std::string(td.path) + "/nonexistent";
43 ASSERT_EQ(-1, stat(nonexistent_path.c_str(), &st));
44 ASSERT_EQ(ENOENT, errno);
45
46 ASSERT_EQ(-1, stat((nonexistent_path + "/").c_str(), &st));
47 ASSERT_EQ(ENOENT, errno);
48
49 #if defined(_WIN32)
50 ASSERT_EQ(-1, stat((nonexistent_path + "\\").c_str(), &st));
51 ASSERT_EQ(ENOENT, errno);
52 #endif
53
54 ASSERT_EQ(0, stat(tf.path, &st));
55 ASSERT_TRUE(S_ISREG(st.st_mode));
56 ASSERT_FALSE(S_ISDIR(st.st_mode));
57
58 ASSERT_EQ(-1, stat((std::string(tf.path) + '/').c_str(), &st));
59 ASSERT_EQ(ENOTDIR, errno);
60
61 #if defined(_WIN32)
62 ASSERT_EQ(-1, stat((std::string(tf.path) + '\\').c_str(), &st));
63 ASSERT_EQ(ENOTDIR, errno);
64 #endif
65 }
66