Joachim Bauch | 58daf40 | 2017-12-21 22:00:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "rtc_base/zero_memory.h" |
| 12 | |
| 13 | #include "api/array_view.h" |
| 14 | #include "rtc_base/buffer.h" |
| 15 | #include "rtc_base/gunit.h" |
| 16 | |
| 17 | namespace rtc { |
| 18 | |
| 19 | TEST(ZeroMemoryTest, TestZeroMemory) { |
| 20 | static const size_t kBufferSize = 32; |
| 21 | uint8_t buffer[kBufferSize]; |
| 22 | for (size_t i = 0; i < kBufferSize; i++) { |
| 23 | buffer[i] = static_cast<uint8_t>(i + 1); |
| 24 | } |
| 25 | ExplicitZeroMemory(buffer, sizeof(buffer)); |
| 26 | for (size_t i = 0; i < kBufferSize; i++) { |
| 27 | EXPECT_EQ(buffer[i], 0); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | TEST(ZeroMemoryTest, TestZeroArrayView) { |
| 32 | static const size_t kBufferSize = 32; |
| 33 | uint8_t buffer[kBufferSize]; |
| 34 | for (size_t i = 0; i < kBufferSize; i++) { |
| 35 | buffer[i] = static_cast<uint8_t>(i + 1); |
| 36 | } |
| 37 | ExplicitZeroMemory(rtc::ArrayView<uint8_t>(buffer, sizeof(buffer))); |
| 38 | for (size_t i = 0; i < kBufferSize; i++) { |
| 39 | EXPECT_EQ(buffer[i], 0); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // While this test doesn't actually test anything, it can be used to check |
| 44 | // the compiler output to make sure the call to "ExplicitZeroMemory" is not |
| 45 | // optimized away. |
| 46 | TEST(ZeroMemoryTest, TestZeroMemoryUnused) { |
| 47 | static const size_t kBufferSize = 32; |
| 48 | uint8_t buffer[kBufferSize]; |
| 49 | ExplicitZeroMemory(buffer, sizeof(buffer)); |
| 50 | } |
| 51 | |
| 52 | } // namespace rtc |