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 <stdint.h>
14
15 #include "api/array_view.h"
16 #include "test/gtest.h"
17
18 namespace rtc {
19
TEST(ZeroMemoryTest,TestZeroMemory)20 TEST(ZeroMemoryTest, TestZeroMemory) {
21 static const size_t kBufferSize = 32;
22 uint8_t buffer[kBufferSize];
23 for (size_t i = 0; i < kBufferSize; i++) {
24 buffer[i] = static_cast<uint8_t>(i + 1);
25 }
26 ExplicitZeroMemory(buffer, sizeof(buffer));
27 for (size_t i = 0; i < kBufferSize; i++) {
28 EXPECT_EQ(buffer[i], 0);
29 }
30 }
31
TEST(ZeroMemoryTest,TestZeroArrayView)32 TEST(ZeroMemoryTest, TestZeroArrayView) {
33 static const size_t kBufferSize = 32;
34 uint8_t buffer[kBufferSize];
35 for (size_t i = 0; i < kBufferSize; i++) {
36 buffer[i] = static_cast<uint8_t>(i + 1);
37 }
38 ExplicitZeroMemory(rtc::ArrayView<uint8_t>(buffer, sizeof(buffer)));
39 for (size_t i = 0; i < kBufferSize; i++) {
40 EXPECT_EQ(buffer[i], 0);
41 }
42 }
43
44 // While this test doesn't actually test anything, it can be used to check
45 // the compiler output to make sure the call to "ExplicitZeroMemory" is not
46 // optimized away.
TEST(ZeroMemoryTest,TestZeroMemoryUnused)47 TEST(ZeroMemoryTest, TestZeroMemoryUnused) {
48 static const size_t kBufferSize = 32;
49 uint8_t buffer[kBufferSize];
50 ExplicitZeroMemory(buffer, sizeof(buffer));
51 }
52
53 } // namespace rtc
54