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 "shell/data_pipe_peek.h" |
6 | 6 |
7 #include "mojo/shell/context.h" | 7 #include "shell/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 shell { |
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(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(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(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(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); |
53 | 53 |
54 timeout = 500; // Should cause peek to timeout after about 0.5ms. | 54 timeout = 500; // Should cause peek to timeout after about 0.5ms. |
55 EXPECT_FALSE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); | 55 EXPECT_FALSE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); |
56 | 56 |
57 EXPECT_EQ(MOJO_RESULT_OK, | 57 EXPECT_EQ(MOJO_RESULT_OK, |
58 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE)); | 58 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE)); |
59 EXPECT_EQ(1u, bytes1); | 59 EXPECT_EQ(1u, bytes1); |
60 | 60 |
61 EXPECT_TRUE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); | 61 EXPECT_TRUE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); |
62 EXPECT_EQ(bytes, std::string(s5)); | 62 EXPECT_EQ(bytes, std::string(s5)); |
63 | 63 |
64 // If the consumer side of the pipe is closed, peek should fail. | 64 // If the consumer side of the pipe is closed, peek should fail. |
65 | 65 |
66 data_pipe.consumer_handle.reset(); | 66 data_pipe.consumer_handle.reset(); |
67 timeout = 0; | 67 timeout = 0; |
68 EXPECT_FALSE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); | 68 EXPECT_FALSE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); |
69 } | 69 } |
70 | 70 |
71 TEST(DataPipePeek, PeekLine) { | 71 TEST(DataPipePeek, PeekLine) { |
72 Context::EnsureEmbedderIsInitialized(); | 72 Context::EnsureEmbedderIsInitialized(); |
73 | 73 |
74 DataPipe data_pipe; | 74 DataPipe data_pipe; |
75 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get()); | 75 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get()); |
76 DataPipeProducerHandle producer(data_pipe.producer_handle.get()); | 76 DataPipeProducerHandle producer(data_pipe.producer_handle.get()); |
77 | 77 |
78 // Inialize the pipe with 4 bytes and no newline. | 78 // Inialize the pipe with 4 bytes and no newline. |
79 | 79 |
80 const char* s4 = "1234"; | 80 const char* s4 = "1234"; |
81 uint32_t num_bytes4 = 4; | 81 uint32_t num_bytes4 = 4; |
82 EXPECT_EQ(MOJO_RESULT_OK, | 82 EXPECT_EQ(MOJO_RESULT_OK, |
83 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE)); | 83 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE)); |
84 EXPECT_EQ(4u, num_bytes4); | 84 EXPECT_EQ(4u, num_bytes4); |
85 | 85 |
86 // Peeking for a line should fail. | 86 // Peeking for a line should fail. |
87 | 87 |
88 std::string str; | 88 std::string str; |
89 size_t max_str_length = 5; | 89 size_t max_str_length = 5; |
90 MojoDeadline timeout = 0; | 90 MojoDeadline timeout = 0; |
91 EXPECT_FALSE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); | 91 EXPECT_FALSE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
92 | 92 |
93 // Writing a newline should cause PeekLine to succeed. | 93 // Writing a newline should cause PeekLine to succeed. |
94 | 94 |
95 uint32_t bytes1 = 1; | 95 uint32_t bytes1 = 1; |
96 const char* s1 = "\n"; | 96 const char* s1 = "\n"; |
97 EXPECT_EQ(MOJO_RESULT_OK, | 97 EXPECT_EQ(MOJO_RESULT_OK, |
98 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE)); | 98 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE)); |
99 EXPECT_EQ(1u, bytes1); | 99 EXPECT_EQ(1u, bytes1); |
100 | 100 |
101 EXPECT_TRUE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); | 101 EXPECT_TRUE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
102 EXPECT_EQ(str, std::string(s4) + "\n"); | 102 EXPECT_EQ(str, std::string(s4) + "\n"); |
103 | 103 |
104 // If the max_line_length parameter is less than the length of the | 104 // If the max_line_length parameter is less than the length of the |
105 // newline terminated string, then peek should fail. | 105 // newline terminated string, then peek should fail. |
106 | 106 |
107 max_str_length = 3; | 107 max_str_length = 3; |
108 EXPECT_FALSE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); | 108 EXPECT_FALSE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
109 } | 109 } |
110 | 110 |
111 } // namespace | 111 } // namespace |
112 } // namespace shell | 112 } // namespace shell |
113 } // namespace mojo | 113 } // namespace mojo |
OLD | NEW |