Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2013 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 "base/file.h"
     18 
     19 #include <gtest/gtest.h>
     20 
     21 #include <errno.h>
     22 #include <fcntl.h>
     23 #include <unistd.h>
     24 
     25 #include <string>
     26 
     27 #include "test_utils.h"
     28 
     29 TEST(file, ReadFileToString_ENOENT) {
     30   std::string s("hello");
     31   errno = 0;
     32   ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
     33   EXPECT_EQ(ENOENT, errno);
     34   EXPECT_EQ("", s);  // s was cleared.
     35 }
     36 
     37 TEST(file, ReadFileToString_success) {
     38   std::string s("hello");
     39   ASSERT_TRUE(android::base::ReadFileToString("/proc/version", &s)) << errno;
     40   EXPECT_GT(s.length(), 6U);
     41   EXPECT_EQ('\n', s[s.length() - 1]);
     42   s[5] = 0;
     43   EXPECT_STREQ("Linux", s.c_str());
     44 }
     45 
     46 TEST(file, WriteStringToFile) {
     47   TemporaryFile tf;
     48   ASSERT_TRUE(tf.fd != -1);
     49   ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename)) << errno;
     50   std::string s;
     51   ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
     52   EXPECT_EQ("abc", s);
     53 }
     54 
     55 TEST(file, WriteStringToFile2) {
     56   TemporaryFile tf;
     57   ASSERT_TRUE(tf.fd != -1);
     58   ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename, 0660,
     59                                                getuid(), getgid()))
     60       << errno;
     61   struct stat sb;
     62   ASSERT_EQ(0, stat(tf.filename, &sb));
     63   ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT));
     64   ASSERT_EQ(getuid(), sb.st_uid);
     65   ASSERT_EQ(getgid(), sb.st_gid);
     66   std::string s;
     67   ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
     68   EXPECT_EQ("abc", s);
     69 }
     70 
     71 TEST(file, WriteStringToFd) {
     72   TemporaryFile tf;
     73   ASSERT_TRUE(tf.fd != -1);
     74   ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
     75 
     76   ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << errno;
     77 
     78   std::string s;
     79   ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << errno;
     80   EXPECT_EQ("abc", s);
     81 }
     82 
     83 TEST(file, ReadFully) {
     84   int fd = open("/proc/version", O_RDONLY);
     85   ASSERT_NE(-1, fd) << strerror(errno);
     86 
     87   char buf[1024];
     88   memset(buf, 0, sizeof(buf));
     89   ASSERT_TRUE(android::base::ReadFully(fd, buf, 5));
     90   ASSERT_STREQ("Linux", buf);
     91 
     92   ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
     93 
     94   ASSERT_FALSE(android::base::ReadFully(fd, buf, sizeof(buf)));
     95 
     96   close(fd);
     97 }
     98 
     99 TEST(file, WriteFully) {
    100   TemporaryFile tf;
    101   ASSERT_TRUE(tf.fd != -1);
    102   ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));
    103   std::string s;
    104   ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
    105   EXPECT_EQ("abc", s);
    106 }
    107