OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 <stdio.h> |
| 6 #include <unistd.h> |
| 7 |
| 8 #include <utility> |
| 9 |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "mojo/public/c/environment/logger.h" |
| 12 #include "mojo/public/cpp/application/application_impl.h" |
| 13 #include "mojo/public/cpp/application/application_test_base.h" |
| 14 #include "mojo/public/cpp/system/functions.h" |
| 15 #include "mojo/services/log/interfaces/entry.mojom.h" |
| 16 #include "mojo/services/log/interfaces/log.mojom.h" |
| 17 #include "services/log/log_impl.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 namespace mojo { |
| 21 namespace log { |
| 22 namespace { |
| 23 |
| 24 using LogImplTest = mojo::test::ApplicationTestBase; |
| 25 |
| 26 // We need to supply a ApplicationConnection to LogImpl::Create(). |
| 27 class TestApplicationConnection : public ApplicationConnection { |
| 28 public: |
| 29 const std::string& GetConnectionURL() override { |
| 30 static std::string kConnectionURL = "mojo:log"; |
| 31 return kConnectionURL; |
| 32 } |
| 33 |
| 34 const std::string& GetRemoteApplicationURL() override { |
| 35 static std::string kRemoteApplicationURL = "mojo:log_impl_unittest"; |
| 36 return kRemoteApplicationURL; |
| 37 } |
| 38 |
| 39 ServiceProvider* GetServiceProvider() override { return nullptr; } |
| 40 void SetServiceConnectorForName(ServiceConnector* service_connector, |
| 41 const std::string& name) override {} |
| 42 }; |
| 43 |
| 44 // Tests the Log service implementation by calling its AddEntry and verifying |
| 45 // the log message it prints to the supplied FILE stream. |
| 46 TEST_F(LogImplTest, AddEntryOutput) { |
| 47 // LogImpl will write to p[1] (which we wrap into a FILE*), we will EXPECT |
| 48 // what's written by reading in p[0]. |
| 49 int p[2]; |
| 50 pipe(p); |
| 51 |
| 52 FILE* output_file = fdopen(p[1], "w"); |
| 53 ASSERT_NE(nullptr, output_file); |
| 54 |
| 55 LogPtr log; |
| 56 auto log_request = GetProxy(&log); |
| 57 TestApplicationConnection app_connection; |
| 58 LogImpl::Create(&app_connection, std::move(log_request), output_file); |
| 59 |
| 60 Entry entry; |
| 61 entry.log_level = MOJO_LOG_LEVEL_INFO; |
| 62 entry.timestamp = GetTimeTicksNow(); |
| 63 entry.source_file = "file.ext"; |
| 64 entry.source_line = 0; |
| 65 entry.message = "1234567890"; |
| 66 log->AddEntry(entry.Clone()); |
| 67 |
| 68 entry.source_line = 1; |
| 69 log->AddEntry(entry.Clone()); |
| 70 |
| 71 entry.source_file.reset(); |
| 72 log->AddEntry(entry.Clone()); |
| 73 |
| 74 entry.message.reset(); |
| 75 log->AddEntry(entry.Clone()); |
| 76 |
| 77 base::MessageLoop::current()->RunUntilIdle(); |
| 78 // We need to close p[1] it so a read(p[0],..) of a very large size will not |
| 79 // block. |
| 80 fclose(output_file); |
| 81 |
| 82 const char* expected_string = |
| 83 "<mojo:log_impl_unittest> [INFO] file.ext: 1234567890\n" |
| 84 "<mojo:log_impl_unittest> [INFO] file.ext:1: 1234567890\n" |
| 85 "<mojo:log_impl_unittest> [INFO] 1234567890\n" |
| 86 "<mojo:log_impl_unittest> [INFO] <no message>\n"; |
| 87 |
| 88 char actual_string[1024 * 10]; |
| 89 size_t nbytes = read(p[0], actual_string, sizeof(actual_string)); |
| 90 EXPECT_EQ(static_cast<size_t>(strlen(expected_string)), nbytes); |
| 91 |
| 92 actual_string[nbytes] = '\0'; |
| 93 EXPECT_STREQ(expected_string, actual_string); |
| 94 } |
| 95 |
| 96 } // namespace |
| 97 } // namespace log |
| 98 } // namespace mojo |
OLD | NEW |