Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / native_client / src / trusted / validator / x86 / halt_trim_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 // Unit tests for code in halt_trim.c
8
9 #ifndef NACL_TRUSTED_BUT_NOT_TCB
10 #error("This file is not meant for use in the TCB")
11 #endif
12
13 #include "gtest/gtest.h"
14 #include "native_client/src/include/nacl_macros.h"
15 #include "native_client/src/shared/platform/nacl_log.h"
16 #include "native_client/src/trusted/validator/x86/halt_trim.h"
17 #include "native_client/src/trusted/validator/x86/ncinstbuffer.h"
18
19
20 namespace {
21
22 // Test harness for routines in halt_trim.c.
23 class HaltTrimTests : public ::testing::Test {
24  protected:
25   HaltTrimTests() {}
26 };
27
28 // Show that if the file ends with less than the minimum number of halts,
29 // no trimming occurs.
30 TEST_F(HaltTrimTests, TrimSmallHaltSegment) {
31   uint8_t small_test[] = {
32     0xf4, 0xf4, 0xf4, 0xf4
33   };
34   NaClMemorySize small_test_size = NACL_ARRAY_SIZE(small_test);
35
36   EXPECT_EQ(small_test_size,
37             NCHaltTrimSize(small_test, small_test_size, 16));
38 }
39
40 // Show that we trim to the nearest 32 byte boundary if there
41 // are a lot of halts.
42 TEST_F(HaltTrimTests, TrimManyHaltsTo32Boundary) {
43   NaClMemorySize size;
44   uint8_t large_test[] = {
45     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
46     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
47     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
48     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
49     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
50     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
51     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
52     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
53     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
54     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
55   };
56   NaClMemorySize large_test_size = NACL_ARRAY_SIZE(large_test);
57
58   size = large_test_size;
59   EXPECT_EQ((NaClMemorySize) 32, NCHaltTrimSize(large_test, size, 32));
60
61   size = large_test_size - 40;
62   EXPECT_EQ((NaClMemorySize) 32, NCHaltTrimSize(large_test, size, 32));
63 }
64
65 // Show that if rounding to the nearest block alignment is too big,
66 // we don't change the size.
67 TEST_F(HaltTrimTests, TrimFailsIfBlockAlignToBig) {
68   uint8_t large_test[40] = {
69     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
70     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
71     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
72     0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
73   };
74
75   EXPECT_EQ((NaClMemorySize) 32,  NCHaltTrimSize(large_test, 40, 32));
76   EXPECT_EQ((NaClMemorySize) 20,  NCHaltTrimSize(large_test, 20, 32));
77 }
78
79 };  // anonymous namespace
80
81 int main(int argc, char *argv[]) {
82   NaClLogModuleInit();
83   testing::InitGoogleTest(&argc, argv);
84   return RUN_ALL_TESTS();
85 }