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 |
| 46 NativeTestUtil::NativeTestUtil() {} |
| 47 |
| 48 NativeTestUtil::~NativeTestUtil() {} |
| 49 |
| 50 void NativeTestUtil::CreateFIFO(const char* fifo_path) { |
| 51 unlink(fifo_path); |
| 52 // Default permissions for mkfifo is ignored, chmod is required. |
| 53 if (mkfifo(fifo_path, 0666) || chmod(fifo_path, 0666)) { |
| 54 AndroidLogError("Failed to create fifo %s: %s\n", |
| 55 fifo_path, strerror(errno)); |
| 56 exit(EXIT_FAILURE); |
| 57 } |
| 58 } |
| 59 |
| 60 void NativeTestUtil::RedirectStream( |
| 61 FILE* stream, const char* path, const char* mode) { |
| 62 if (!freopen(path, mode, stream)) { |
| 63 AndroidLogError("Failed to redirect stream to file: %s: %s\n", |
| 64 path, strerror(errno)); |
| 65 exit(EXIT_FAILURE); |
| 66 } |
| 67 } |
| 68 |
| 69 void NativeTestUtil::ParseArgsFromCommandLineFile( |
| 70 const char* path, std::vector<std::string>* args) { |
| 71 FilePath command_line(path); |
| 72 std::string command_line_string; |
| 73 if (file_util::ReadFileToString(command_line, &command_line_string)) { |
| 74 ParseArgsFromString(command_line_string, args); |
| 75 } |
| 76 } |
| 77 |
| 78 |
| 79 int NativeTestUtil::ArgsToArgv(const std::vector<std::string>& args, |
| 80 std::vector<char*>* argv) { |
| 81 // We need to pass in a non-const char**. |
| 82 int argc = args.size(); |
| 83 |
| 84 argv->resize(argc + 1); |
| 85 for (int i = 0; i < argc; ++i) { |
| 86 (*argv)[i] = const_cast<char*>(args[i].c_str()); |
| 87 } |
| 88 (*argv)[argc] = NULL; // argv must be NULL terminated. |
| 89 |
| 90 return argc; |
| 91 } |
| 92 |
| 93 } // namespace testing |
OLD | NEW |