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