SharedBuffer_test.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define __STDC_LIMIT_MACROS
  17. #include <gtest/gtest.h>
  18. #include <memory>
  19. #include <stdint.h>
  20. #include "SharedBuffer.h"
  21. TEST(SharedBufferTest, TestAlloc) {
  22. EXPECT_DEATH(android::SharedBuffer::alloc(SIZE_MAX), "");
  23. EXPECT_DEATH(android::SharedBuffer::alloc(SIZE_MAX - sizeof(android::SharedBuffer)), "");
  24. // Make sure we don't die here.
  25. // Check that null is returned, as we are asking for the whole address space.
  26. android::SharedBuffer* buf =
  27. android::SharedBuffer::alloc(SIZE_MAX - sizeof(android::SharedBuffer) - 1);
  28. ASSERT_EQ(nullptr, buf);
  29. buf = android::SharedBuffer::alloc(0);
  30. ASSERT_NE(nullptr, buf);
  31. ASSERT_EQ(0U, buf->size());
  32. buf->release();
  33. }
  34. TEST(SharedBufferTest, TestEditResize) {
  35. android::SharedBuffer* buf = android::SharedBuffer::alloc(10);
  36. EXPECT_DEATH(buf->editResize(SIZE_MAX - sizeof(android::SharedBuffer)), "");
  37. buf = android::SharedBuffer::alloc(10);
  38. EXPECT_DEATH(buf->editResize(SIZE_MAX), "");
  39. buf = android::SharedBuffer::alloc(10);
  40. // Make sure we don't die here.
  41. // Check that null is returned, as we are asking for the whole address space.
  42. buf = buf->editResize(SIZE_MAX - sizeof(android::SharedBuffer) - 1);
  43. ASSERT_EQ(nullptr, buf);
  44. buf = android::SharedBuffer::alloc(10);
  45. buf = buf->editResize(0);
  46. ASSERT_EQ(0U, buf->size());
  47. buf->release();
  48. }