OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "testing/android/native_test_util.h" |
| 6 |
| 7 #include <android/log.h> |
| 8 #include <stdarg.h> |
| 9 #include <stdio.h> |
| 10 #include <sys/types.h> |
| 11 #include <sys/stat.h> |
| 12 #include <unistd.h> |
| 13 |
| 14 #include "base/command_line.h" |
| 15 #include "base/file_path.h" |
| 16 #include "base/file_util.h" |
| 17 #include "base/string_util.h" |
| 18 #include "base/strings/string_tokenizer.h" |
| 19 |
| 20 namespace { |
| 21 |
| 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, |
| 32 std::vector<std::string>* args) { |
| 33 base::StringTokenizer tokenizer(command_line, kWhitespaceASCII); |
| 34 tokenizer.set_quote_chars("\""); |
| 35 while (tokenizer.GetNext()) { |
| 36 std::string token; |
| 37 RemoveChars(tokenizer.token(), "\"", &token); |
| 38 args->push_back(token); |
| 39 } |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
| 44 namespace testing { |
| 45 namespace native_test_util { |
| 46 |
| 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( |
| 67 const char* path, std::vector<std::string>* args) { |
| 68 base::FilePath command_line(path); |
| 69 std::string command_line_string; |
| 70 if (file_util::ReadFileToString(command_line, &command_line_string)) { |
| 71 ParseArgsFromString(command_line_string, args); |
| 72 } |
| 73 } |
| 74 |
| 75 int ArgsToArgv(const std::vector<std::string>& args, |
| 76 std::vector<char*>* argv) { |
| 77 // We need to pass in a non-const char**. |
| 78 int argc = args.size(); |
| 79 |
| 80 argv->resize(argc + 1); |
| 81 for (int i = 0; i < argc; ++i) { |
| 82 (*argv)[i] = const_cast<char*>(args[i].c_str()); |
| 83 } |
| 84 (*argv)[argc] = NULL; // argv must be NULL terminated. |
| 85 |
| 86 return argc; |
| 87 } |
| 88 |
| 89 } // namespace native_test_util |
| 90 } // namespace testing |
OLD | NEW |