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
3 // found in the LICENSE file.
5 #include "mojo/shell/application_manager/data_pipe_peek.h"
7 #include "mojo/shell/context.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 TEST(DataPipePeek
, PeekNBytes
) {
15 Context::EnsureEmbedderIsInitialized();
18 DataPipeConsumerHandle
consumer(data_pipe
.consumer_handle
.get());
19 DataPipeProducerHandle
producer(data_pipe
.producer_handle
.get());
21 // Inialize the pipe with 4 bytes.
23 const char* s4
= "1234";
24 uint32_t num_bytes4
= 4;
25 EXPECT_EQ(MOJO_RESULT_OK
,
26 WriteDataRaw(producer
, s4
, &num_bytes4
, MOJO_WRITE_DATA_FLAG_NONE
));
27 EXPECT_EQ(4u, num_bytes4
);
29 // We're not consuming data, so peeking for 4 bytes should always succeed.
32 MojoDeadline timeout
= 0;
33 EXPECT_TRUE(BlockingPeekNBytes(consumer
, &bytes
, num_bytes4
, timeout
));
34 EXPECT_EQ(bytes
, std::string(s4
));
36 timeout
= 1000; // 1ms
37 EXPECT_TRUE(BlockingPeekNBytes(consumer
, &bytes
, num_bytes4
, timeout
));
38 EXPECT_EQ(bytes
, std::string(s4
));
40 timeout
= MOJO_DEADLINE_INDEFINITE
;
41 EXPECT_TRUE(BlockingPeekNBytes(consumer
, &bytes
, num_bytes4
, timeout
));
42 EXPECT_EQ(bytes
, std::string(s4
));
44 // Peeking for 5 bytes should fail, until another byte is written.
47 uint32_t num_bytes5
= 5;
49 const char* s5
= "12345";
52 EXPECT_FALSE(BlockingPeekNBytes(consumer
, &bytes
, num_bytes5
, timeout
));
54 timeout
= 500; // Should cause peek to timeout after about 0.5ms.
55 EXPECT_FALSE(BlockingPeekNBytes(consumer
, &bytes
, num_bytes5
, timeout
));
57 EXPECT_EQ(MOJO_RESULT_OK
,
58 WriteDataRaw(producer
, s1
, &bytes1
, MOJO_WRITE_DATA_FLAG_NONE
));
59 EXPECT_EQ(1u, bytes1
);
61 EXPECT_TRUE(BlockingPeekNBytes(consumer
, &bytes
, num_bytes5
, timeout
));
62 EXPECT_EQ(bytes
, std::string(s5
));
64 // If the consumer side of the pipe is closed, peek should fail.
66 data_pipe
.consumer_handle
.reset();
68 EXPECT_FALSE(BlockingPeekNBytes(consumer
, &bytes
, num_bytes5
, timeout
));
71 TEST(DataPipePeek
, PeekLine
) {
72 Context::EnsureEmbedderIsInitialized();
75 DataPipeConsumerHandle
consumer(data_pipe
.consumer_handle
.get());
76 DataPipeProducerHandle
producer(data_pipe
.producer_handle
.get());
78 // Inialize the pipe with 4 bytes and no newline.
80 const char* s4
= "1234";
81 uint32_t num_bytes4
= 4;
82 EXPECT_EQ(MOJO_RESULT_OK
,
83 WriteDataRaw(producer
, s4
, &num_bytes4
, MOJO_WRITE_DATA_FLAG_NONE
));
84 EXPECT_EQ(4u, num_bytes4
);
86 // Peeking for a line should fail.
89 size_t max_str_length
= 5;
90 MojoDeadline timeout
= 0;
91 EXPECT_FALSE(BlockingPeekLine(consumer
, &str
, max_str_length
, timeout
));
93 // Writing a newline should cause PeekLine to succeed.
96 const char* s1
= "\n";
97 EXPECT_EQ(MOJO_RESULT_OK
,
98 WriteDataRaw(producer
, s1
, &bytes1
, MOJO_WRITE_DATA_FLAG_NONE
));
99 EXPECT_EQ(1u, bytes1
);
101 EXPECT_TRUE(BlockingPeekLine(consumer
, &str
, max_str_length
, timeout
));
102 EXPECT_EQ(str
, std::string(s4
) + "\n");
104 // If the max_line_length parameter is less than the length of the
105 // newline terminated string, then peek should fail.
108 EXPECT_FALSE(BlockingPeekLine(consumer
, &str
, max_str_length
, timeout
));