Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/public/c/bindings/buffer.h" | |
| 6 | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 | |
| 9 namespace { | |
| 10 | |
| 11 TEST(MojomBufferTest, RoundTo8) { | |
| 12 char buffer[100]; | |
| 13 struct MojomBuffer mbuf = { | |
| 14 buffer, sizeof(buffer), | |
| 15 0, // num_bytes_used | |
| 16 }; | |
| 17 | |
| 18 // This should actually allocate 8 bytes: | |
| 19 EXPECT_EQ(buffer, MojomBuffer_Allocate(&mbuf, 6)); | |
| 20 EXPECT_EQ(8ul, mbuf.num_bytes_used); | |
| 21 // The start of the next buffer is buffer + 8 bytes: | |
| 22 EXPECT_EQ(buffer + 8, MojomBuffer_Allocate(&mbuf, 6)); | |
| 23 EXPECT_EQ(16ul, mbuf.num_bytes_used); | |
| 24 // 8-bye allocation results in 8-byte allocation. | |
| 25 EXPECT_EQ(buffer + 16, MojomBuffer_Allocate(&mbuf, 8)); | |
| 26 EXPECT_EQ(24ul, mbuf.num_bytes_used); | |
| 27 } | |
| 28 | |
| 29 TEST(MojomBufferTest, Failure) { | |
| 30 char buffer[100]; | |
| 31 struct MojomBuffer mbuf = { | |
| 32 buffer, sizeof(buffer), | |
| 33 0, // num_bytes_used | |
| 34 }; | |
| 35 | |
| 36 // Allocate too much space. | |
| 37 EXPECT_EQ(NULL, MojomBuffer_Allocate(&mbuf, sizeof(buffer) + 10)); | |
| 38 // Allocating 0 bytes should fail as well. | |
| 39 EXPECT_EQ(NULL, MojomBuffer_Allocate(&mbuf, 0)); | |
| 40 | |
| 41 // Setup the buffer data so that it will overflow. | |
| 42 mbuf.num_bytes_used = UINT32_MAX - 7; // This divides by 8. | |
|
viettrungluu
2016/06/15 15:59:49
nit: You should probably explicitly include <stdin
vardhan
2016/06/15 16:10:21
Done.
| |
| 43 mbuf.buf_size = UINT32_MAX; | |
| 44 | |
| 45 // Rounds to 8, and allocates more than it has: | |
| 46 EXPECT_EQ(NULL, MojomBuffer_Allocate(&mbuf, 1)); | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| OLD | NEW |