Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / native_client / src / trusted / validator / x86 / nc_remaining_memory_tests.cc
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 NCRemainingMemory 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 NCRemainingMemoryTests : public ::testing::Test {
28  protected:
29   void SetUp();
30   NCRemainingMemoryTests();
31   NCRemainingMemory _memory;        /* Memory being tested. */
32   uint8_t _buffer[kMaxBufferSize];  /* Memory buffer to use. */
33   const size_t _buffer_size;        /* Actually size of test data. */
34   static const char* kTestData;     /* Data put into memory for testing. */
35 };
36
37 /* Simnple sample size for testing. */
38 const size_t kSampleSize = 10;
39
40 const char* NCRemainingMemoryTests::kTestData = "Test data";
41
42 NCRemainingMemoryTests::NCRemainingMemoryTests()
43     : _buffer_size(strlen(kTestData)) {
44   /* Fill buffer with bad test data, so that we know if bad accesses occur
45    * past the end of the buffer.
46    */
47   for (size_t i = 0; i < kMaxBufferSize; ++i) {
48     _buffer[i] = 'X';
49   }
50   /* Now fill in the good test data. */
51   for (size_t i = 0; i < _buffer_size; ++i) {
52     _buffer[i] = (uint8_t) kTestData[i];
53   }
54 }
55
56 void NCRemainingMemoryTests::SetUp() {
57   NCRemainingMemoryInit(_buffer, _buffer_size, &_memory);
58 }
59
60 /* Test (macro) NCRemainingMemoryGetNext, and see if it properly
61  * sees the data in the memory buffer.
62  */
63 TEST_F(NCRemainingMemoryTests, GetNextGetsData) {
64   /* First verify that we get text as defined in the test data. */
65   for (size_t i = 0; i < _buffer_size; ++i) {
66     EXPECT_EQ(NCRemainingMemoryGetNext(&_memory), (uint8_t) kTestData[i])
67         << "testing GetNext(" << i << ")";
68     NCRemainingMemoryRead(&_memory);
69   }
70   /* Now verify that if we look at the next (i.e. peek), we get 0. */
71   EXPECT_EQ(0, NCRemainingMemoryGetNext(&_memory)) << "testing GetNext(eof).";
72 }
73
74 /* Test function NCRemainingMemoryLookahead, and see if it properly
75  * sees the data in the memory buffer.
76  */
77 TEST_F(NCRemainingMemoryTests, LookaheadGetsData) {
78   size_t chars_read = 0;
79   /* Try looking ahead from each possible position in the buffer. */
80   for (size_t chars_left = _buffer_size; chars_left > 0; --chars_left) {
81     /* First verify that for the characters left, lookahead matches. */
82     for (size_t i = 0; i < chars_left; ++i) {
83       EXPECT_EQ(NCRemainingMemoryLookahead(&_memory, i),
84                 (uint8_t) kTestData[chars_read + i])
85           << "testing Lookahed(" << i << " after " << chars_read
86           << " chars read.";
87     }
88     /* Verify that any further lookahead returns 0. */
89     EXPECT_EQ(0, NCRemainingMemoryLookahead(&_memory, chars_left));
90     EXPECT_EQ(0, NCRemainingMemoryLookahead(&_memory, chars_left+1));
91     EXPECT_EQ(0, NCRemainingMemoryLookahead(&_memory, chars_left+10));
92     EXPECT_EQ(0, NCRemainingMemoryLookahead(&_memory, chars_left+111));
93     NCRemainingMemoryRead(&_memory);
94     ++chars_read;
95   }
96 }
97
98 /* Test function NCRemainingMemoryRead, and see if it properly reads
99  * data from the memory buffer.
100  */
101 TEST_F(NCRemainingMemoryTests, ReadGetsData) {
102   /* First verify that we get text as defined in the test data. */
103   for (size_t i = 0; i < _buffer_size; ++i) {
104     EXPECT_EQ(NCRemainingMemoryRead(&_memory), (uint8_t) kTestData[i])
105         << "after reading " << i << " characters.";
106   }
107   /* Now verify that zero is returned for any additional reads. */
108   EXPECT_EQ(0, NCRemainingMemoryRead(&_memory))
109       << "after reading all characters.";
110   EXPECT_EQ(0, NCRemainingMemoryRead(&_memory))
111       << "after reading too far past characters";
112 }
113
114 /* Test if the field read_length is properly updated by the read function. */
115 TEST_F(NCRemainingMemoryTests, ReadLengthTests) {
116   /* Verify that read count is initially zero. */
117   EXPECT_EQ(0, _memory.read_length) << "should be initially zero read lenght";
118   /* Verify that we update the read length while there is real data. */
119   for (size_t i = 0; i < _buffer_size; ++i) {
120     EXPECT_EQ(i, _memory.read_length);
121     NCRemainingMemoryRead(&_memory);
122   }
123   /* Verify that no matter how many more reads we do, the read length
124    * doesn't increase.
125    */
126   NCRemainingMemoryRead(&_memory);
127   EXPECT_EQ(_buffer_size, (size_t)_memory.read_length);
128   NCRemainingMemoryRead(&_memory);
129   EXPECT_EQ(_buffer_size, (size_t)_memory.read_length);
130 }
131
132 /* Test if the field overflow_count is properly updated by the read function. */
133 TEST_F(NCRemainingMemoryTests, OverflowCountTests) {
134   /* Verify that the overflow count stays zero while there is still data
135    * in the buffer.
136    */
137   for (size_t i = 0; i < _buffer_size; ++i) {
138     EXPECT_EQ(0, _memory.overflow_count);
139     NCRemainingMemoryRead(&_memory);
140   }
141   /* Verify that any further reading causes the overflow counter to
142    * be bumped.
143    */
144   for (size_t i = 0; i < kSampleSize; ++i) {
145     EXPECT_EQ(i, _memory.overflow_count);
146     NCRemainingMemoryRead(&_memory);
147   }
148 }
149
150 /* Test if function NCRemainingMemoryReset works. */
151 TEST_F(NCRemainingMemoryTests, ResetDataTests) {
152   /* Try reseting on possible buffer positions. */
153   for (size_t i = 0; i < _buffer_size; ++i) {
154     /* Try different read lengths before reseting. */
155     for (size_t read_size = 0; read_size < _buffer_size + kSampleSize;
156          ++read_size) {
157       for (size_t k = 0; k < read_size; k++) {
158         NCRemainingMemoryRead(&_memory);
159       }
160       NCRemainingMemoryReset(&_memory);
161       EXPECT_EQ(0, _memory.read_length);
162       EXPECT_EQ(0, _memory.overflow_count);
163       EXPECT_EQ(NCRemainingMemoryGetNext(&_memory),
164                 (uint8_t) kTestData[i]);
165     }
166     NCRemainingMemoryRead(&_memory);
167     NCRemainingMemoryAdvance(&_memory);
168   }
169 }
170
171 /* Test if function NCRemainingMemoryAdvance works. */
172 TEST_F(NCRemainingMemoryTests, AdvanceDataTests) {
173   /* Try advancing from various locations within the memory. */
174   for (size_t i = 0; i < _buffer_size; ++i) {
175     SetUp();
176     for (size_t j = 0; j < i; ++j) {
177       NCRemainingMemoryRead(&_memory);
178     }
179     NCRemainingMemoryAdvance(&_memory);
180     EXPECT_EQ(0, _memory.read_length);
181     EXPECT_EQ(0, _memory.overflow_count);
182     EXPECT_EQ(NCRemainingMemoryGetNext(&_memory),
183               (uint8_t) kTestData[i]);
184   }
185 }
186
187 };  // anonymous namespace
188
189 int main(int argc, char *argv[]) {
190   testing::InitGoogleTest(&argc, argv);
191   return RUN_ALL_TESTS();
192 }