| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <sys/file.h> | 6 #include <sys/file.h> |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/hash_tables.h" | 9 #include "base/hash_tables.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "chrome/browser/chromeos/external_metrics.h" | 11 #include "chrome/browser/chromeos/external_metrics.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace chromeos { // Need this because of the FRIEND_TEST | 14 namespace chromeos { // Need this because of the FRIEND_TEST |
| 15 | 15 |
| 16 class ExternalMetricsTest : public testing::Test { | 16 class ExternalMetricsTest : public testing::Test { |
| 17 }; | 17 }; |
| 18 | 18 |
| 19 // Because the metrics service is not essential, errors will not cause the | 19 // Because the metrics service is not essential, errors will not cause the |
| 20 // program to terminate. However, the errors produce logs. | 20 // program to terminate. However, the errors produce logs. |
| 21 | 21 |
| 22 #define MAXLENGTH ExternalMetrics::kMetricsMessageMaxLength | 22 #define MAXLENGTH ExternalMetrics::kMetricsMessageMaxLength |
| 23 | 23 |
| 24 static void SendMessage(const char* path, const char* name, const char* value) { | 24 static void SendMessage(const char* path, const char* name, const char* value) { |
| 25 int fd = open(path, O_CREAT | O_APPEND | O_WRONLY, 0666); | 25 int fd = open(path, O_CREAT | O_APPEND | O_WRONLY, 0666); |
| 26 int32 l = strlen(name) + strlen(value) + 2 + sizeof(l); | 26 int32 l = strlen(name) + strlen(value) + 2 + sizeof(l); |
| 27 int num_bytes; | 27 size_t num_bytes = 0; |
| 28 num_bytes = write(fd, &l, sizeof(l)); | 28 num_bytes += write(fd, &l, sizeof(l)); |
| 29 num_bytes = write(fd, name, strlen(name) + 1); | 29 num_bytes += write(fd, name, strlen(name) + 1); |
| 30 num_bytes = write(fd, value, strlen(value) + 1); | 30 num_bytes += write(fd, value, strlen(value) + 1); |
| 31 EXPECT_EQ(num_bytes, sizeof(l) + strlen(name) + strlen(value) + 2); |
| 31 close(fd); | 32 close(fd); |
| 32 } | 33 } |
| 33 | 34 |
| 34 static scoped_ptr<std::string> received_name; | 35 static scoped_ptr<std::string> received_name; |
| 35 static scoped_ptr<std::string> received_value; | 36 static scoped_ptr<std::string> received_value; |
| 36 int received_count = 0; | 37 int received_count = 0; |
| 37 | 38 |
| 38 static void ReceiveMessage(const char* name, const char* value) { | 39 static void ReceiveMessage(const char* name, const char* value) { |
| 39 received_name.reset(new std::string(name)); | 40 received_name.reset(new std::string(name)); |
| 40 received_value.reset(new std::string(value)); | 41 received_value.reset(new std::string(value)); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 external_metrics->CollectEvents(); | 127 external_metrics->CollectEvents(); |
| 127 EXPECT_EQ(expect_count, received_count); | 128 EXPECT_EQ(expect_count, received_count); |
| 128 | 129 |
| 129 // Checks that we survive when file doesn't exist. | 130 // Checks that we survive when file doesn't exist. |
| 130 EXPECT_EQ(0, unlink(path)); | 131 EXPECT_EQ(0, unlink(path)); |
| 131 external_metrics->CollectEvents(); | 132 external_metrics->CollectEvents(); |
| 132 EXPECT_EQ(expect_count, received_count); | 133 EXPECT_EQ(expect_count, received_count); |
| 133 } | 134 } |
| 134 | 135 |
| 135 } // namespace chromeos | 136 } // namespace chromeos |
| OLD | NEW |