Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: src/trusted/validator/x86/nc_inst_bytes_tests.cc

Issue 625923004: Delete old x86 validator. (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: rebase master Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7 /*
8 * Unit tests for struct NCInstBytes in ncinstbuffer.{h,cc}.
9 */
10
11 #ifndef NACL_TRUSTED_BUT_NOT_TCB
12 #error("This file is not meant for use in the TCB")
13 #endif
14
15 #include <stdio.h>
16
17 #include "gtest/gtest.h"
18
19 #include "native_client/src/trusted/validator/x86/ncinstbuffer.h"
20
21 namespace {
22
23 /* Constant defining maximum buffer size for memory. */
24 const size_t kMaxBufferSize = 30;
25
26 /* Test harness for class NCRemainingMemory. */
27 class NCInstBytesTests : public ::testing::Test {
28 protected:
29 void SetUp();
30 NCInstBytesTests();
31
32 /* Data for testing. */
33 NCInstBytes _bytes; /* Inst bytes (read). */
34 NCRemainingMemory _memory; /* Memory being tested. */
35 uint8_t _buffer[kMaxBufferSize]; /* Memory buffer to use. */
36 const size_t _buffer_size; /* Actually size of test data. */
37 static const char* kTestData; /* Data put into memory for testing. */
38
39 /* Verify if the contents of kTestData matches the contents
40 * of _bytes, up to _buffer_size; Remaining elements are zero.
41 */
42 void VerifyBytesMatchTestData();
43 };
44
45 const char* NCInstBytesTests::kTestData = "Test data";
46
47 NCInstBytesTests::NCInstBytesTests()
48 : _buffer_size(strlen(kTestData)) {
49 /* Fill memory buffer with bad test data, so that we know if bad accesses
50 * occur past the end of the buffer.
51 */
52 for (size_t i = 0; i < kMaxBufferSize; ++i) {
53 _buffer[i] = 'X';
54 }
55 /* Now fill in the good test data. */
56 for (size_t i = 0; i < _buffer_size; ++i) {
57 _buffer[i] = (uint8_t) kTestData[i];
58 }
59 }
60
61 void NCInstBytesTests::VerifyBytesMatchTestData() {
62 /* First show that while there is real data in the memory,
63 * it has been copied to the bytes buffer.
64 */
65 size_t match_limit =
66 (_bytes.length <= _buffer_size) ? _bytes.length : _buffer_size;
67 for (size_t i = 0; i < match_limit; ++i) {
68 EXPECT_EQ(_bytes.byte[i], kTestData[i])
69 << "value verification of byte " << i;
70 }
71 /* Now show that any remaining bytes (after the memory
72 * has been read) are 0.
73 */
74 for (size_t i = match_limit; i < _bytes.length; ++i) {
75 EXPECT_EQ(0, _bytes.byte[i])
76 << "zero verification of byte " << i;
77 }
78 }
79
80 void NCInstBytesTests::SetUp() {
81 NCRemainingMemoryInit(_buffer, _buffer_size, &_memory);
82 NCInstBytesInitMemory(&_bytes, &_memory);
83 /* Be sure to verify that we can run the tests, which require
84 * a couple of extra slots for overflow.
85 */
86 ASSERT_LT(_buffer_size + 1, (size_t) MAX_INST_LENGTH)
87 << "Set up failed due to kTestData being too long.";
88 }
89
90 /* Test function NCInstBytesPeek, and see if it properly sees the data in
91 * the instruction buffer.
92 */
93 TEST_F(NCInstBytesTests, PeekGetsData) {
94 /* First verify that we get text as defined in the test data. */
95 for (size_t i = 0; i < _buffer_size; ++i) {
96 EXPECT_EQ(NCInstBytesPeek(&_bytes, i), (uint8_t) kTestData[i])
97 << "peeking " << i << " bytes ahead.";
98 }
99 /* Now verify that if we look past the end of the data, we
100 * get 0.
101 */
102 EXPECT_EQ(0, NCInstBytesPeek(&_bytes, _buffer_size))
103 << "peeking one byte past end of buffer.";
104 EXPECT_EQ(0, NCInstBytesPeek(&_bytes, _buffer_size + 100))
105 << "peeking 100 bytes past end of buffer.";
106 EXPECT_EQ(0, _bytes.length);
107 VerifyBytesMatchTestData();
108 }
109
110 /* Test function NCInstBytesRead, and see if it properly read
111 * data from the memory buffer.
112 */
113 TEST_F(NCInstBytesTests, ReadGetsData) {
114 /* First verify that we get text as defined in the test data. */
115 for (size_t i = 0; i < _buffer_size; ++i) {
116 EXPECT_EQ(NCInstBytesRead(&_bytes), (uint8_t) kTestData[i])
117 << "after reading " << i << " characters";
118 VerifyBytesMatchTestData();
119 }
120 EXPECT_EQ(_buffer_size, _bytes.length);
121 VerifyBytesMatchTestData();
122 /* Now verify that zero is returned for any additional reads. */
123 for (size_t i = _bytes.length; i < MAX_INST_LENGTH; ++i) {
124 EXPECT_EQ(0, NCInstBytesRead(&_bytes))
125 << "after reading all characters";
126 }
127 VerifyBytesMatchTestData();
128 /* Verify that we can apply a read, even if buffer overflow occurs,
129 * and the only effect is that the buffer length is not increased.
130 */
131 EXPECT_EQ(MAX_INST_LENGTH, _bytes.length);
132 EXPECT_EQ(0, NCInstBytesRead(&_bytes));
133 EXPECT_EQ(MAX_INST_LENGTH, _bytes.length);
134 }
135
136 /* Test function NCInstBytesReadBytes, and see if it properly reads
137 * data from the memory buffer.
138 */
139 TEST_F(NCInstBytesTests, ReadBytesGetsData) {
140 /* First verify that we get text as defined in the test data. */
141 size_t chars_read = 0;
142 size_t stride = 4;
143 VerifyBytesMatchTestData();
144 while (chars_read < MAX_INST_LENGTH) {
145 if ((chars_read + stride) > MAX_INST_LENGTH) {
146 stride = MAX_INST_LENGTH - chars_read;
147 }
148 NCInstBytesReadBytes(stride, &_bytes);
149 chars_read += stride;
150 VerifyBytesMatchTestData();
151 }
152 /* Verify that we can apply additional reads without overflowing
153 * the buffer.
154 */
155 NCInstBytesReadBytes(2, &_bytes);
156 EXPECT_EQ(MAX_INST_LENGTH, _bytes.length) << "Buffer overflow fix failed.";
157 }
158
159 /* Test function NCInstBytesReset, and see if we back up to
160 * the beginning of the instruction.
161 */
162 TEST_F(NCInstBytesTests, Reset) {
163 /* First read some bytes into the bytes buffer. */
164 NCInstBytesReadBytes(5, &_bytes);
165 VerifyBytesMatchTestData();
166 /* Now reset and see if we moved back. */
167 NCInstBytesReset(&_bytes);
168 EXPECT_EQ(0, _bytes.length) << "Reset didn't fix buffer length.";
169 VerifyBytesMatchTestData();
170 EXPECT_EQ(0, _memory.read_length) << "Reset didn't fix memory length.";
171 EXPECT_EQ(0, _memory.overflow_count) << "Reset didn't fix memory overflow.";
172 EXPECT_EQ((void*) _buffer, (void*) _memory.cur_pos)
173 << "Reset did not reset memory.";
174 }
175
176 }; // anonymous namespace
177
178 int main(int argc, char *argv[]) {
179 testing::InitGoogleTest(&argc, argv);
180 return RUN_ALL_TESTS();
181 }
OLDNEW
« no previous file with comments | « src/trusted/validator/x86/nacl_regs64.h ('k') | src/trusted/validator/x86/nc_remaining_memory_tests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698