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 : #include <vector>
8 :
9 : #include "gtest/gtest.h"
10 :
11 : #include "native_client/src/include/nacl_macros.h"
12 : #include "native_client/src/include/nacl_string.h"
13 : #include "native_client/src/trusted/nonnacl_util/sel_ldr_launcher.h"
14 :
15 : using ::std::vector;
16 :
17 : #define NACL_STRING_ARRAY_TO_VECTOR(a) \
18 : vector<nacl::string>(a, a + NACL_ARRAY_SIZE(a))
19 :
20 : namespace {
21 :
22 : // Tests for SelLdrLauncher
23 :
24 : // Helper function that tests construction of the command line
25 : // via InitCommandLine() and BuildCommandLine() calls. The executable path
26 : // (0th arg) is not tested and should not be included in |expected_argv|.
27 1 : void ExpectSelLdrLauncherArgv(std::vector<nacl::string>& expected_argv) {
28 1 : nacl::SelLdrLauncher launcher;
29 :
30 : // Prepare the fixed sample args.
31 1 : int imc_fd = NACL_NO_FILE_DESC;
32 1 : const char* sel_ldr_argv[] = { "-X", "5" };
33 1 : const char* application_argv[] = { "foo" };
34 1 : const char* extra_expected_argv[] = { "-X", "5", "--", "foo" };
35 10 : for (size_t i = 0; i < NACL_ARRAY_SIZE(extra_expected_argv); i++) {
36 4 : expected_argv.push_back(extra_expected_argv[i]);
37 : }
38 :
39 : // Initialize the arg fields and check the ones that have public getters.
40 : launcher.InitCommandLine(imc_fd,
41 : NACL_STRING_ARRAY_TO_VECTOR(sel_ldr_argv),
42 1 : NACL_STRING_ARRAY_TO_VECTOR(application_argv));
43 :
44 : // Build the command line and verify it, skipping the executable.
45 2 : vector<nacl::string> command;
46 1 : launcher.BuildCommandLine(&command);
47 :
48 : // command vector includes the sel_ldr executable.
49 1 : size_t ignored_args = 1;
50 : #if NACL_LINUX
51 : // Skip the bootstrap executable, the sel_ldr executable, and --r_debug.
52 : ignored_args += 2;
53 : #endif
54 :
55 1 : ASSERT_EQ(expected_argv.size() + ignored_args, command.size());
56 6 : for (size_t i = 0; i < expected_argv.size(); i++) {
57 5 : EXPECT_EQ(expected_argv[i], command[i + ignored_args]);
58 0 : }
59 : }
60 :
61 :
62 4 : TEST(SelLdrLauncherTest, BuildCommandLine) {
63 1 : vector<nacl::string> expected_R;
64 2 : expected_R.push_back("-R");
65 2 : ExpectSelLdrLauncherArgv(expected_R);
66 1 : }
67 :
68 : } // namespace
69 :
70 1 : int main(int argc, char *argv[]) {
71 1 : testing::InitGoogleTest(&argc, argv);
72 1 : return RUN_ALL_TESTS();
73 2 : }
|