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/system/memory.h" 6 7 #include <stddef.h> 8 #include <stdint.h> 9 10 #include <limits> 11 12 #include "mojo/public/c/system/macros.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace mojo { 16 namespace system { 17 namespace { 18 19 TEST(MemoryTest, Valid) { 20 char my_char; 21 int32_t my_int32; 22 int64_t my_int64; 23 char my_char_array[5]; 24 int32_t my_int32_array[5]; 25 int64_t my_int64_array[5]; 26 27 // |VerifyUserPointer|: 28 29 EXPECT_TRUE(VerifyUserPointer<char>(&my_char)); 30 EXPECT_TRUE(VerifyUserPointer<int32_t>(&my_int32)); 31 EXPECT_TRUE(VerifyUserPointer<int64_t>(&my_int64)); 32 33 // |VerifyUserPointerWithCount|: 34 35 EXPECT_TRUE(VerifyUserPointerWithCount<char>(my_char_array, 5)); 36 EXPECT_TRUE(VerifyUserPointerWithCount<int32_t>(my_int32_array, 5)); 37 EXPECT_TRUE(VerifyUserPointerWithCount<int64_t>(my_int64_array, 5)); 38 39 // It shouldn't care what the pointer is if the count is zero. 40 EXPECT_TRUE(VerifyUserPointerWithCount<char>(NULL, 0)); 41 EXPECT_TRUE(VerifyUserPointerWithCount<int32_t>(NULL, 0)); 42 EXPECT_TRUE(VerifyUserPointerWithCount<int32_t>( 43 reinterpret_cast<const int32_t*>(1), 0)); 44 EXPECT_TRUE(VerifyUserPointerWithCount<int64_t>(NULL, 0)); 45 EXPECT_TRUE(VerifyUserPointerWithCount<int64_t>( 46 reinterpret_cast<const int64_t*>(1), 0)); 47 48 // |VerifyUserPointerWithSize|: 49 50 EXPECT_TRUE(VerifyUserPointerWithSize<1>(&my_char, sizeof(my_char))); 51 EXPECT_TRUE(VerifyUserPointerWithSize<1>(&my_int32, sizeof(my_int32))); 52 EXPECT_TRUE(VerifyUserPointerWithSize<MOJO_ALIGNOF(int32_t)>( 53 &my_int32, sizeof(my_int32))); 54 EXPECT_TRUE(VerifyUserPointerWithSize<1>(&my_int64, sizeof(my_int64))); 55 EXPECT_TRUE(VerifyUserPointerWithSize<MOJO_ALIGNOF(int64_t)>( 56 &my_int64, sizeof(my_int64))); 57 58 EXPECT_TRUE(VerifyUserPointerWithSize<1>(my_char_array, 59 sizeof(my_char_array))); 60 EXPECT_TRUE(VerifyUserPointerWithSize<1>(my_int32_array, 61 sizeof(my_int32_array))); 62 EXPECT_TRUE(VerifyUserPointerWithSize<MOJO_ALIGNOF(int32_t)>( 63 my_int32_array, sizeof(my_int32_array))); 64 EXPECT_TRUE(VerifyUserPointerWithSize<1>(my_int64_array, 65 sizeof(my_int64_array))); 66 EXPECT_TRUE(VerifyUserPointerWithSize<MOJO_ALIGNOF(int64_t)>( 67 my_int64_array, sizeof(my_int64_array))); 68 } 69 70 TEST(MemoryTest, Invalid) { 71 // Note: |VerifyUserPointer...()| are defined to be "best effort" checks (and 72 // may always return true). Thus these tests of invalid cases only reflect the 73 // current implementation. 74 75 // These tests depend on |int32_t| and |int64_t| having nontrivial alignment. 76 MOJO_COMPILE_ASSERT(MOJO_ALIGNOF(int32_t) != 1, 77 int32_t_does_not_have_to_be_aligned); 78 MOJO_COMPILE_ASSERT(MOJO_ALIGNOF(int64_t) != 1, 79 int64_t_does_not_have_to_be_aligned); 80 81 int32_t my_int32; 82 int64_t my_int64; 83 84 // |VerifyUserPointer|: 85 86 EXPECT_FALSE(VerifyUserPointer<char>(NULL)); 87 EXPECT_FALSE(VerifyUserPointer<int32_t>(NULL)); 88 EXPECT_FALSE(VerifyUserPointer<int64_t>(NULL)); 89 90 // Unaligned: 91 EXPECT_FALSE(VerifyUserPointer<int32_t>(reinterpret_cast<const int32_t*>(1))); 92 EXPECT_FALSE(VerifyUserPointer<int64_t>(reinterpret_cast<const int64_t*>(1))); 93 94 // |VerifyUserPointerWithCount|: 95 96 EXPECT_FALSE(VerifyUserPointerWithCount<char>(NULL, 1)); 97 EXPECT_FALSE(VerifyUserPointerWithCount<int32_t>(NULL, 1)); 98 EXPECT_FALSE(VerifyUserPointerWithCount<int64_t>(NULL, 1)); 99 100 // Unaligned: 101 EXPECT_FALSE(VerifyUserPointerWithCount<int32_t>( 102 reinterpret_cast<const int32_t*>(1), 1)); 103 EXPECT_FALSE(VerifyUserPointerWithCount<int64_t>( 104 reinterpret_cast<const int64_t*>(1), 1)); 105 106 // Count too big: 107 EXPECT_FALSE(VerifyUserPointerWithCount<int32_t>( 108 &my_int32, std::numeric_limits<size_t>::max())); 109 EXPECT_FALSE(VerifyUserPointerWithCount<int64_t>( 110 &my_int64, std::numeric_limits<size_t>::max())); 111 112 // |VerifyUserPointerWithSize|: 113 114 EXPECT_FALSE(VerifyUserPointerWithSize<1>(NULL, 1)); 115 EXPECT_FALSE(VerifyUserPointerWithSize<4>(NULL, 1)); 116 EXPECT_FALSE(VerifyUserPointerWithSize<4>(NULL, 4)); 117 EXPECT_FALSE(VerifyUserPointerWithSize<8>(NULL, 1)); 118 EXPECT_FALSE(VerifyUserPointerWithSize<8>(NULL, 4)); 119 EXPECT_FALSE(VerifyUserPointerWithSize<8>(NULL, 8)); 120 121 // Unaligned: 122 EXPECT_FALSE(VerifyUserPointerWithSize<4>(reinterpret_cast<const int32_t*>(1), 123 1)); 124 EXPECT_FALSE(VerifyUserPointerWithSize<4>(reinterpret_cast<const int32_t*>(1), 125 4)); 126 EXPECT_FALSE(VerifyUserPointerWithSize<8>(reinterpret_cast<const int32_t*>(1), 127 1)); 128 EXPECT_FALSE(VerifyUserPointerWithSize<8>(reinterpret_cast<const int32_t*>(1), 129 4)); 130 EXPECT_FALSE(VerifyUserPointerWithSize<8>(reinterpret_cast<const int32_t*>(1), 131 8)); 132 } 133 134 } // namespace 135 } // namespace system 136 } // namespace mojo 137