OSDN Git Service

Merge "Fix memory leak on jdwp_process_free()"
[android-x86/system-core.git] / adb / adb_io_test.cpp
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 "adb_io.h"
18
19 #include <gtest/gtest.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 #include <string>
26
27 #include "base/file.h"
28
29 class TemporaryFile {
30  public:
31   TemporaryFile() {
32     init("/data/local/tmp");
33     if (fd == -1) {
34       init("/tmp");
35     }
36   }
37
38   ~TemporaryFile() {
39     close(fd);
40     unlink(filename);
41   }
42
43   int fd;
44   char filename[1024];
45
46  private:
47   void init(const char* tmp_dir) {
48     snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
49     fd = mkstemp(filename);
50   }
51 };
52
53 TEST(io, ReadFdExactly_whole) {
54   const char expected[] = "Foobar";
55   TemporaryFile tf;
56   ASSERT_NE(-1, tf.fd);
57
58   ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
59   ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
60
61   // Test reading the whole file.
62   char buf[sizeof(expected)] = {};
63   ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1)) << strerror(errno);
64   EXPECT_STREQ(expected, buf);
65 }
66
67 TEST(io, ReadFdExactly_eof) {
68   const char expected[] = "Foobar";
69   TemporaryFile tf;
70   ASSERT_NE(-1, tf.fd);
71
72   ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
73   ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
74
75   // Test that not having enough data will fail.
76   char buf[sizeof(expected) + 1] = {};
77   ASSERT_FALSE(ReadFdExactly(tf.fd, buf, sizeof(buf)));
78   EXPECT_EQ(0, errno) << strerror(errno);
79 }
80
81 TEST(io, ReadFdExactly_partial) {
82   const char input[] = "Foobar";
83   TemporaryFile tf;
84   ASSERT_NE(-1, tf.fd);
85
86   ASSERT_TRUE(android::base::WriteStringToFd(input, tf.fd)) << strerror(errno);
87   ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
88
89   // Test reading a partial file.
90   char buf[sizeof(input) - 1] = {};
91   ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1));
92
93   std::string expected(input);
94   expected.pop_back();
95   EXPECT_STREQ(expected.c_str(), buf);
96 }
97
98 TEST(io, WriteFdExactly_whole) {
99   const char expected[] = "Foobar";
100   TemporaryFile tf;
101   ASSERT_NE(-1, tf.fd);
102
103   // Test writing the whole string to the file.
104   ASSERT_TRUE(WriteFdExactly(tf.fd, expected, sizeof(expected)))
105     << strerror(errno);
106   ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
107
108   std::string s;
109   ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
110   EXPECT_STREQ(expected, s.c_str());
111 }
112
113 TEST(io, WriteFdExactly_partial) {
114   const char buf[] = "Foobar";
115   TemporaryFile tf;
116   ASSERT_NE(-1, tf.fd);
117
118   // Test writing a partial string to the file.
119   ASSERT_TRUE(WriteFdExactly(tf.fd, buf, sizeof(buf) - 2)) << strerror(errno);
120   ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
121
122   std::string expected(buf);
123   expected.pop_back();
124
125   std::string s;
126   ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
127   EXPECT_EQ(expected, s);
128 }
129
130 TEST(io, WriteStringFully) {
131   const char str[] = "Foobar";
132   TemporaryFile tf;
133   ASSERT_NE(-1, tf.fd);
134
135   // Test writing a partial string to the file.
136   ASSERT_TRUE(WriteStringFully(tf.fd, str)) << strerror(errno);
137   ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
138
139   std::string s;
140   ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
141   EXPECT_STREQ(str, s.c_str());
142 }