OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/edk/test/test_utils.h" | 5 #include "mojo/edk/test/test_utils.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/posix/eintr_wrapper.h" | 12 #include "base/posix/eintr_wrapper.h" |
13 | 13 |
14 using mojo::embedder::ScopedPlatformHandle; | 14 using mojo::platform::PlatformHandle; |
| 15 using mojo::platform::ScopedPlatformHandle; |
15 | 16 |
16 namespace mojo { | 17 namespace mojo { |
17 namespace test { | 18 namespace test { |
18 | 19 |
19 bool BlockingWrite(const embedder::PlatformHandle& handle, | 20 bool BlockingWrite(const PlatformHandle& handle, |
20 const void* buffer, | 21 const void* buffer, |
21 size_t bytes_to_write, | 22 size_t bytes_to_write, |
22 size_t* bytes_written) { | 23 size_t* bytes_written) { |
23 int original_flags = fcntl(handle.fd, F_GETFL); | 24 int original_flags = fcntl(handle.fd, F_GETFL); |
24 if (original_flags == -1 || | 25 if (original_flags == -1 || |
25 fcntl(handle.fd, F_SETFL, original_flags & (~O_NONBLOCK)) != 0) { | 26 fcntl(handle.fd, F_SETFL, original_flags & (~O_NONBLOCK)) != 0) { |
26 return false; | 27 return false; |
27 } | 28 } |
28 | 29 |
29 ssize_t result = HANDLE_EINTR(write(handle.fd, buffer, bytes_to_write)); | 30 ssize_t result = HANDLE_EINTR(write(handle.fd, buffer, bytes_to_write)); |
30 | 31 |
31 fcntl(handle.fd, F_SETFL, original_flags); | 32 fcntl(handle.fd, F_SETFL, original_flags); |
32 | 33 |
33 if (result < 0) | 34 if (result < 0) |
34 return false; | 35 return false; |
35 | 36 |
36 *bytes_written = result; | 37 *bytes_written = result; |
37 return true; | 38 return true; |
38 } | 39 } |
39 | 40 |
40 bool BlockingRead(const embedder::PlatformHandle& handle, | 41 bool BlockingRead(const PlatformHandle& handle, |
41 void* buffer, | 42 void* buffer, |
42 size_t buffer_size, | 43 size_t buffer_size, |
43 size_t* bytes_read) { | 44 size_t* bytes_read) { |
44 int original_flags = fcntl(handle.fd, F_GETFL); | 45 int original_flags = fcntl(handle.fd, F_GETFL); |
45 if (original_flags == -1 || | 46 if (original_flags == -1 || |
46 fcntl(handle.fd, F_SETFL, original_flags & (~O_NONBLOCK)) != 0) { | 47 fcntl(handle.fd, F_SETFL, original_flags & (~O_NONBLOCK)) != 0) { |
47 return false; | 48 return false; |
48 } | 49 } |
49 | 50 |
50 ssize_t result = HANDLE_EINTR(read(handle.fd, buffer, buffer_size)); | 51 ssize_t result = HANDLE_EINTR(read(handle.fd, buffer, buffer_size)); |
51 | 52 |
52 fcntl(handle.fd, F_SETFL, original_flags); | 53 fcntl(handle.fd, F_SETFL, original_flags); |
53 | 54 |
54 if (result < 0) | 55 if (result < 0) |
55 return false; | 56 return false; |
56 | 57 |
57 *bytes_read = result; | 58 *bytes_read = result; |
58 return true; | 59 return true; |
59 } | 60 } |
60 | 61 |
61 bool NonBlockingRead(const embedder::PlatformHandle& handle, | 62 bool NonBlockingRead(const PlatformHandle& handle, |
62 void* buffer, | 63 void* buffer, |
63 size_t buffer_size, | 64 size_t buffer_size, |
64 size_t* bytes_read) { | 65 size_t* bytes_read) { |
65 ssize_t result = HANDLE_EINTR(read(handle.fd, buffer, buffer_size)); | 66 ssize_t result = HANDLE_EINTR(read(handle.fd, buffer, buffer_size)); |
66 | 67 |
67 if (result < 0) { | 68 if (result < 0) { |
68 if (errno != EAGAIN && errno != EWOULDBLOCK) | 69 if (errno != EAGAIN && errno != EWOULDBLOCK) |
69 return false; | 70 return false; |
70 | 71 |
71 *bytes_read = 0; | 72 *bytes_read = 0; |
72 } else { | 73 } else { |
73 *bytes_read = result; | 74 *bytes_read = result; |
74 } | 75 } |
75 | 76 |
76 return true; | 77 return true; |
77 } | 78 } |
78 | 79 |
79 ScopedPlatformHandle PlatformHandleFromFILE(util::ScopedFILE fp) { | 80 ScopedPlatformHandle PlatformHandleFromFILE(util::ScopedFILE fp) { |
80 CHECK(fp); | 81 CHECK(fp); |
81 int rv = dup(fileno(fp.get())); | 82 int rv = dup(fileno(fp.get())); |
82 PCHECK(rv != -1) << "dup"; | 83 PCHECK(rv != -1) << "dup"; |
83 return ScopedPlatformHandle(embedder::PlatformHandle(rv)); | 84 return ScopedPlatformHandle(PlatformHandle(rv)); |
84 } | 85 } |
85 | 86 |
86 util::ScopedFILE FILEFromPlatformHandle(ScopedPlatformHandle h, | 87 util::ScopedFILE FILEFromPlatformHandle(ScopedPlatformHandle h, |
87 const char* mode) { | 88 const char* mode) { |
88 CHECK(h.is_valid()); | 89 CHECK(h.is_valid()); |
89 util::ScopedFILE rv(fdopen(h.release().fd, mode)); | 90 util::ScopedFILE rv(fdopen(h.release().fd, mode)); |
90 PCHECK(rv) << "fdopen"; | 91 PCHECK(rv) << "fdopen"; |
91 return rv; | 92 return rv; |
92 } | 93 } |
93 | 94 |
94 } // namespace test | 95 } // namespace test |
95 } // namespace mojo | 96 } // namespace mojo |
OLD | NEW |