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/shell/data_pipe_peek.h" | 5 #include "mojo/shell/data_pipe_peek.h" |
6 | 6 |
7 #include "mojo/runner/context.h" | 7 #include "mojo/runner/context.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace mojo { | 10 namespace mojo { |
11 namespace shell { | 11 namespace runner { |
12 namespace { | 12 namespace { |
13 | 13 |
14 TEST(DataPipePeek, PeekNBytes) { | 14 TEST(DataPipePeek, PeekNBytes) { |
15 Context::EnsureEmbedderIsInitialized(); | 15 Context::EnsureEmbedderIsInitialized(); |
16 | 16 |
17 DataPipe data_pipe; | 17 DataPipe data_pipe; |
18 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get()); | 18 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get()); |
19 DataPipeProducerHandle producer(data_pipe.producer_handle.get()); | 19 DataPipeProducerHandle producer(data_pipe.producer_handle.get()); |
20 | 20 |
21 // Inialize the pipe with 4 bytes. | 21 // Inialize the pipe with 4 bytes. |
22 | 22 |
23 const char* s4 = "1234"; | 23 const char* s4 = "1234"; |
24 uint32_t num_bytes4 = 4; | 24 uint32_t num_bytes4 = 4; |
25 EXPECT_EQ(MOJO_RESULT_OK, | 25 EXPECT_EQ(MOJO_RESULT_OK, |
26 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE)); | 26 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE)); |
27 EXPECT_EQ(4u, num_bytes4); | 27 EXPECT_EQ(4u, num_bytes4); |
28 | 28 |
29 // We're not consuming data, so peeking for 4 bytes should always succeed. | 29 // We're not consuming data, so peeking for 4 bytes should always succeed. |
30 | 30 |
31 std::string bytes; | 31 std::string bytes; |
32 MojoDeadline timeout = 0; | 32 MojoDeadline timeout = 0; |
33 EXPECT_TRUE(BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); | 33 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); |
34 EXPECT_EQ(bytes, std::string(s4)); | 34 EXPECT_EQ(bytes, std::string(s4)); |
35 | 35 |
36 timeout = 1000; // 1ms | 36 timeout = 1000; // 1ms |
37 EXPECT_TRUE(BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); | 37 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); |
38 EXPECT_EQ(bytes, std::string(s4)); | 38 EXPECT_EQ(bytes, std::string(s4)); |
39 | 39 |
40 timeout = MOJO_DEADLINE_INDEFINITE; | 40 timeout = MOJO_DEADLINE_INDEFINITE; |
41 EXPECT_TRUE(BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); | 41 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); |
42 EXPECT_EQ(bytes, std::string(s4)); | 42 EXPECT_EQ(bytes, std::string(s4)); |
43 | 43 |
44 // Peeking for 5 bytes should fail, until another byte is written. | 44 // Peeking for 5 bytes should fail, until another byte is written. |
45 | 45 |
46 uint32_t bytes1 = 1; | 46 uint32_t bytes1 = 1; |
47 uint32_t num_bytes5 = 5; | 47 uint32_t num_bytes5 = 5; |
48 const char* s1 = "5"; | 48 const char* s1 = "5"; |
49 const char* s5 = "12345"; | 49 const char* s5 = "12345"; |
50 | 50 |
51 timeout = 0; | 51 timeout = 0; |
52 EXPECT_FALSE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); | 52 EXPECT_FALSE( |
| 53 shell::BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); |
53 | 54 |
54 timeout = 500; // Should cause peek to timeout after about 0.5ms. | 55 timeout = 500; // Should cause peek to timeout after about 0.5ms. |
55 EXPECT_FALSE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); | 56 EXPECT_FALSE( |
| 57 shell::BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); |
56 | 58 |
57 EXPECT_EQ(MOJO_RESULT_OK, | 59 EXPECT_EQ(MOJO_RESULT_OK, |
58 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE)); | 60 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE)); |
59 EXPECT_EQ(1u, bytes1); | 61 EXPECT_EQ(1u, bytes1); |
60 | 62 |
61 EXPECT_TRUE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); | 63 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); |
62 EXPECT_EQ(bytes, std::string(s5)); | 64 EXPECT_EQ(bytes, std::string(s5)); |
63 | 65 |
64 // If the consumer side of the pipe is closed, peek should fail. | 66 // If the consumer side of the pipe is closed, peek should fail. |
65 | 67 |
66 data_pipe.consumer_handle.reset(); | 68 data_pipe.consumer_handle.reset(); |
67 timeout = 0; | 69 timeout = 0; |
68 EXPECT_FALSE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); | 70 EXPECT_FALSE( |
| 71 shell::BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); |
69 } | 72 } |
70 | 73 |
71 TEST(DataPipePeek, PeekLine) { | 74 TEST(DataPipePeek, PeekLine) { |
72 Context::EnsureEmbedderIsInitialized(); | 75 Context::EnsureEmbedderIsInitialized(); |
73 | 76 |
74 DataPipe data_pipe; | 77 DataPipe data_pipe; |
75 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get()); | 78 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get()); |
76 DataPipeProducerHandle producer(data_pipe.producer_handle.get()); | 79 DataPipeProducerHandle producer(data_pipe.producer_handle.get()); |
77 | 80 |
78 // Inialize the pipe with 4 bytes and no newline. | 81 // Inialize the pipe with 4 bytes and no newline. |
79 | 82 |
80 const char* s4 = "1234"; | 83 const char* s4 = "1234"; |
81 uint32_t num_bytes4 = 4; | 84 uint32_t num_bytes4 = 4; |
82 EXPECT_EQ(MOJO_RESULT_OK, | 85 EXPECT_EQ(MOJO_RESULT_OK, |
83 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE)); | 86 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE)); |
84 EXPECT_EQ(4u, num_bytes4); | 87 EXPECT_EQ(4u, num_bytes4); |
85 | 88 |
86 // Peeking for a line should fail. | 89 // Peeking for a line should fail. |
87 | 90 |
88 std::string str; | 91 std::string str; |
89 size_t max_str_length = 5; | 92 size_t max_str_length = 5; |
90 MojoDeadline timeout = 0; | 93 MojoDeadline timeout = 0; |
91 EXPECT_FALSE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); | 94 EXPECT_FALSE( |
| 95 shell::BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
92 | 96 |
93 // Writing a newline should cause PeekLine to succeed. | 97 // Writing a newline should cause PeekLine to succeed. |
94 | 98 |
95 uint32_t bytes1 = 1; | 99 uint32_t bytes1 = 1; |
96 const char* s1 = "\n"; | 100 const char* s1 = "\n"; |
97 EXPECT_EQ(MOJO_RESULT_OK, | 101 EXPECT_EQ(MOJO_RESULT_OK, |
98 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE)); | 102 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE)); |
99 EXPECT_EQ(1u, bytes1); | 103 EXPECT_EQ(1u, bytes1); |
100 | 104 |
101 EXPECT_TRUE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); | 105 EXPECT_TRUE(shell::BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
102 EXPECT_EQ(str, std::string(s4) + "\n"); | 106 EXPECT_EQ(str, std::string(s4) + "\n"); |
103 | 107 |
104 // If the max_line_length parameter is less than the length of the | 108 // If the max_line_length parameter is less than the length of the |
105 // newline terminated string, then peek should fail. | 109 // newline terminated string, then peek should fail. |
106 | 110 |
107 max_str_length = 3; | 111 max_str_length = 3; |
108 EXPECT_FALSE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); | 112 EXPECT_FALSE( |
| 113 shell::BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
109 } | 114 } |
110 | 115 |
111 } // namespace | 116 } // namespace |
112 } // namespace shell | 117 } // namespace runner |
113 } // namespace mojo | 118 } // namespace mojo |
OLD | NEW |