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.
7 // Unit tests for code in halt_trim.c
9 #ifndef NACL_TRUSTED_BUT_NOT_TCB
10 #error("This file is not meant for use in the TCB")
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"
22 // Test harness for routines in halt_trim.c.
23 class HaltTrimTests : public ::testing::Test {
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
34 NaClMemorySize small_test_size = NACL_ARRAY_SIZE(small_test);
36 EXPECT_EQ(small_test_size,
37 NCHaltTrimSize(small_test, small_test_size, 16));
40 // Show that we trim to the nearest 32 byte boundary if there
41 // are a lot of halts.
42 TEST_F(HaltTrimTests, TrimManyHaltsTo32Boundary) {
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,
56 NaClMemorySize large_test_size = NACL_ARRAY_SIZE(large_test);
58 size = large_test_size;
59 EXPECT_EQ((NaClMemorySize) 32, NCHaltTrimSize(large_test, size, 32));
61 size = large_test_size - 40;
62 EXPECT_EQ((NaClMemorySize) 32, NCHaltTrimSize(large_test, size, 32));
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,
75 EXPECT_EQ((NaClMemorySize) 32, NCHaltTrimSize(large_test, 40, 32));
76 EXPECT_EQ((NaClMemorySize) 20, NCHaltTrimSize(large_test, 20, 32));
79 }; // anonymous namespace
81 int main(int argc, char *argv[]) {
83 testing::InitGoogleTest(&argc, argv);
84 return RUN_ALL_TESTS();