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 : // Copied from halt_trim.c
20 : static const NaClMemorySize kMinHaltKeepLength = MAX_INST_LENGTH + 1;
21 :
22 : namespace {
23 :
24 : // Test harness for routines in halt_trim.c.
25 3 : class HaltTrimTests : public ::testing::Test {
26 : protected:
27 3 : HaltTrimTests() {}
28 : };
29 :
30 : // Show that if the file ends with less than the minimum number of halts,
31 : // no trimming occurs.
32 4 : TEST_F(HaltTrimTests, TrimSmallHaltSegment) {
33 : uint8_t small_test[] = {
34 : 0xf4, 0xf4, 0xf4, 0xf4
35 1 : };
36 1 : NaClMemorySize small_test_size = NACL_ARRAY_SIZE(small_test);
37 :
38 1 : EXPECT_EQ(small_test_size,
39 : NCHaltTrimSize(small_test, small_test_size, 16));
40 1 : }
41 :
42 : // Show that we trim to the nearest 32 byte boundary if there
43 : // are a lot of halts.
44 4 : TEST_F(HaltTrimTests, TrimManyHaltsTo32Boundary) {
45 : NaClMemorySize size;
46 : uint8_t large_test[] = {
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 : 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
56 : 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
57 1 : };
58 1 : NaClMemorySize large_test_size = NACL_ARRAY_SIZE(large_test);
59 :
60 1 : size = large_test_size;
61 1 : EXPECT_EQ((NaClMemorySize) 32, NCHaltTrimSize(large_test, size, 32));
62 :
63 1 : size = large_test_size - 40;
64 1 : EXPECT_EQ((NaClMemorySize) 32, NCHaltTrimSize(large_test, size, 32));
65 1 : }
66 :
67 : // Show that if rounding to the nearest block alignment is too big,
68 : // we don't change the size.
69 4 : TEST_F(HaltTrimTests, TrimFailsIfBlockAlignToBig) {
70 : uint8_t large_test[40] = {
71 : 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
72 : 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
73 : 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
74 : 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4, 0xf4,
75 1 : };
76 :
77 1 : EXPECT_EQ((NaClMemorySize) 32, NCHaltTrimSize(large_test, 40, 32));
78 1 : EXPECT_EQ((NaClMemorySize) 20, NCHaltTrimSize(large_test, 20, 32));
79 1 : }
80 :
81 : }; // anonymous namespace
82 :
83 1 : int main(int argc, char *argv[]) {
84 1 : NaClLogModuleInit();
85 1 : testing::InitGoogleTest(&argc, argv);
86 1 : return RUN_ALL_TESTS();
87 2 : }
|