1 : // Copyright 2013 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/agent/asan/stack_capture.h"
16 :
17 : #include "base/memory/scoped_ptr.h"
18 : #include "gtest/gtest.h"
19 :
20 : namespace agent {
21 : namespace asan {
22 :
23 : namespace {
24 :
25 : class StackCaptureTest : public testing::Test {
26 : public:
27 E : void SetUp() OVERRIDE {
28 : // Setup the "global" state.
29 E : StackCapture::Init();
30 E : }
31 : };
32 :
33 : } // namespace
34 :
35 E : TEST_F(StackCaptureTest, InitFromBuffer) {
36 E : StackCapture capture;
37 :
38 : // Validate the capture's initial state.
39 E : EXPECT_FALSE(capture.IsValid());
40 E : EXPECT_EQ(0u, capture.stack_id());
41 E : EXPECT_EQ(0, capture.num_frames());
42 E : EXPECT_EQ(StackCapture::kMaxNumFrames, capture.max_num_frames());
43 E : EXPECT_EQ(NULL, capture.frames());
44 :
45 : // Create some fake stack trace data.
46 E : ULONG stack_id = 10;
47 E : void* frames[StackCapture::kMaxNumFrames + 1] = { 0 };
48 E : for (size_t i = 0; i < arraysize(frames); ++i) {
49 E : frames[i] = reinterpret_cast<void*>(i);
50 E : }
51 :
52 : // Initialize the stack capture without using all of the frames.
53 E : capture.InitFromBuffer(stack_id, frames, 7);
54 E : EXPECT_TRUE(capture.IsValid());
55 E : EXPECT_EQ(10u, capture.stack_id());
56 E : EXPECT_EQ(7, capture.num_frames());
57 E : EXPECT_EQ(StackCapture::kMaxNumFrames, capture.max_num_frames());
58 E : EXPECT_TRUE(capture.frames() != NULL);
59 :
60 : // Attempt to initialize the stack capture using too many frames; the
61 : // resulting capture should truncate to kMaxNumFrames.
62 E : capture.InitFromBuffer(stack_id, frames, arraysize(frames));
63 E : EXPECT_TRUE(capture.IsValid());
64 E : EXPECT_EQ(10u, capture.stack_id());
65 E : EXPECT_EQ(StackCapture::kMaxNumFrames, capture.num_frames());
66 E : EXPECT_EQ(StackCapture::kMaxNumFrames, capture.max_num_frames());
67 E : EXPECT_TRUE(capture.frames() != NULL);
68 E : }
69 :
70 E : TEST_F(StackCaptureTest, InitFromStack) {
71 E : StackCapture capture;
72 :
73 E : EXPECT_FALSE(capture.IsValid());
74 E : EXPECT_EQ(0u, capture.stack_id());
75 E : EXPECT_EQ(0, capture.num_frames());
76 E : EXPECT_EQ(StackCapture::kMaxNumFrames, capture.max_num_frames());
77 :
78 E : capture.InitFromStack();
79 E : EXPECT_TRUE(capture.IsValid());
80 E : EXPECT_LT(0u, capture.num_frames());
81 E : EXPECT_EQ(StackCapture::kMaxNumFrames, capture.max_num_frames());
82 E : }
83 :
84 E : TEST_F(StackCaptureTest, RestrictedFrameCount) {
85 E : StackCapture::set_bottom_frames_to_skip(0);
86 : // Restrict this to a stack depth that is smaller than the stack depth of
87 : // this test.
88 E : StackCapture capture(5);
89 E : EXPECT_FALSE(capture.IsValid());
90 E : EXPECT_EQ(0u, capture.stack_id());
91 E : EXPECT_EQ(0, capture.num_frames());
92 E : EXPECT_EQ(5u, capture.max_num_frames());
93 :
94 E : capture.InitFromStack();
95 E : EXPECT_TRUE(capture.IsValid());
96 E : EXPECT_EQ(5u, capture.num_frames());
97 E : EXPECT_EQ(5u, capture.max_num_frames());
98 E : }
99 :
100 : } // namespace asan
101 : } // namespace agent
|