OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "testing/android/native_test_util.h" | 5 #include "testing/android/native_test_util.h" |
6 | 6 |
7 #include <android/log.h> | |
8 #include <stdarg.h> | |
9 #include <stdio.h> | |
10 #include <sys/stat.h> | |
11 #include <sys/types.h> | |
12 #include <unistd.h> | |
13 | |
14 #include "base/command_line.h" | |
15 #include "base/file_util.h" | 7 #include "base/file_util.h" |
16 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
17 #include "base/strings/string_tokenizer.h" | 9 #include "base/strings/string_tokenizer.h" |
18 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
19 | 11 |
20 namespace { | 12 namespace { |
21 | 13 |
22 const char kLogTag[] = "chromium"; | |
23 | |
24 void AndroidLogError(const char* format, ...) { | |
25 va_list args; | |
26 va_start(args, format); | |
27 __android_log_vprint(ANDROID_LOG_ERROR, kLogTag, format, args); | |
28 va_end(args); | |
29 } | |
30 | |
31 void ParseArgsFromString(const std::string& command_line, | 14 void ParseArgsFromString(const std::string& command_line, |
32 std::vector<std::string>* args) { | 15 std::vector<std::string>* args) { |
33 base::StringTokenizer tokenizer(command_line, kWhitespaceASCII); | 16 base::StringTokenizer tokenizer(command_line, kWhitespaceASCII); |
34 tokenizer.set_quote_chars("\""); | 17 tokenizer.set_quote_chars("\""); |
35 while (tokenizer.GetNext()) { | 18 while (tokenizer.GetNext()) { |
36 std::string token; | 19 std::string token; |
37 RemoveChars(tokenizer.token(), "\"", &token); | 20 RemoveChars(tokenizer.token(), "\"", &token); |
38 args->push_back(token); | 21 args->push_back(token); |
39 } | 22 } |
40 } | 23 } |
41 | 24 |
42 } // namespace | 25 } // namespace |
43 | 26 |
44 namespace testing { | 27 namespace testing { |
45 namespace native_test_util { | 28 namespace native_test_util { |
46 | 29 |
47 void CreateFIFO(const char* fifo_path) { | |
48 unlink(fifo_path); | |
49 // Default permissions for mkfifo is ignored, chmod is required. | |
50 if (mkfifo(fifo_path, 0666) || chmod(fifo_path, 0666)) { | |
51 AndroidLogError("Failed to create fifo %s: %s\n", | |
52 fifo_path, strerror(errno)); | |
53 exit(EXIT_FAILURE); | |
54 } | |
55 } | |
56 | |
57 void RedirectStream( | |
58 FILE* stream, const char* path, const char* mode) { | |
59 if (!freopen(path, mode, stream)) { | |
60 AndroidLogError("Failed to redirect stream to file: %s: %s\n", | |
61 path, strerror(errno)); | |
62 exit(EXIT_FAILURE); | |
63 } | |
64 } | |
65 | |
66 void ParseArgsFromCommandLineFile( | 30 void ParseArgsFromCommandLineFile( |
67 const char* path, std::vector<std::string>* args) { | 31 const char* path, std::vector<std::string>* args) { |
68 base::FilePath command_line(path); | 32 base::FilePath command_line(path); |
69 std::string command_line_string; | 33 std::string command_line_string; |
70 if (file_util::ReadFileToString(command_line, &command_line_string)) { | 34 if (file_util::ReadFileToString(command_line, &command_line_string)) { |
71 ParseArgsFromString(command_line_string, args); | 35 ParseArgsFromString(command_line_string, args); |
72 } | 36 } |
73 } | 37 } |
74 | 38 |
75 int ArgsToArgv(const std::vector<std::string>& args, | 39 int ArgsToArgv(const std::vector<std::string>& args, |
76 std::vector<char*>* argv) { | 40 std::vector<char*>* argv) { |
77 // We need to pass in a non-const char**. | 41 // We need to pass in a non-const char**. |
78 int argc = args.size(); | 42 int argc = args.size(); |
79 | 43 |
80 argv->resize(argc + 1); | 44 argv->resize(argc + 1); |
81 for (int i = 0; i < argc; ++i) { | 45 for (int i = 0; i < argc; ++i) { |
82 (*argv)[i] = const_cast<char*>(args[i].c_str()); | 46 (*argv)[i] = const_cast<char*>(args[i].c_str()); |
83 } | 47 } |
84 (*argv)[argc] = NULL; // argv must be NULL terminated. | 48 (*argv)[argc] = NULL; // argv must be NULL terminated. |
85 | 49 |
86 return argc; | 50 return argc; |
87 } | 51 } |
88 | 52 |
89 } // namespace native_test_util | 53 } // namespace native_test_util |
90 } // namespace testing | 54 } // namespace testing |
OLD | NEW |