diff options
author | Yi Jin <jinyithu@google.com> | 2017-10-23 15:42:44 -0700 |
---|---|---|
committer | Yi Jin <jinyithu@google.com> | 2017-10-23 16:36:06 -0700 |
commit | e08333067a9f4ce9176dff490c4b163f71f56e1e (patch) | |
tree | 09d88398f208cf4abde6feb17e82441c4e9f4c45 | |
parent | c35ca6de375a1a17a3e070f85e7cf15272d92181 (diff) |
Fix several nits
1. Change the APIs of ProtoOutputStream to be able to write bytes
2. Fix the tests in incidentd, stdout is closed so can't capture the
value, use temporaryFile instead.
Test: N/A
Change-Id: Ibc31f2efd068afc6c06188d92f57ca5a754c3683
-rw-r--r-- | cmds/incidentd/tests/Reporter_test.cpp | 14 | ||||
-rw-r--r-- | libs/protoutil/include/android/util/ProtoOutputStream.h | 4 | ||||
-rw-r--r-- | libs/protoutil/src/ProtoOutputStream.cpp | 5 |
3 files changed, 12 insertions, 11 deletions
diff --git a/cmds/incidentd/tests/Reporter_test.cpp b/cmds/incidentd/tests/Reporter_test.cpp index 5d074bcb0e4c..8d99fc7dbbbc 100644 --- a/cmds/incidentd/tests/Reporter_test.cpp +++ b/cmds/incidentd/tests/Reporter_test.cpp @@ -32,8 +32,6 @@ using namespace android::binder; using namespace std; using ::testing::StrEq; using ::testing::Test; -using ::testing::internal::CaptureStdout; -using ::testing::internal::GetCapturedStdout; class TestListener : public IIncidentReportStatusListener { @@ -139,20 +137,24 @@ TEST_F(ReporterTest, RunReportEmpty) { } TEST_F(ReporterTest, RunReportWithHeaders) { + TemporaryFile tf; IncidentReportArgs args1, args2; args1.addSection(1); args2.addSection(2); std::vector<int8_t> header {'a', 'b', 'c', 'd', 'e'}; args2.addHeader(header); - sp<ReportRequest> r1 = new ReportRequest(args1, l, STDOUT_FILENO); - sp<ReportRequest> r2 = new ReportRequest(args2, l, STDOUT_FILENO); + sp<ReportRequest> r1 = new ReportRequest(args1, l, tf.fd); + sp<ReportRequest> r2 = new ReportRequest(args2, l, tf.fd); reporter->batch.add(r1); reporter->batch.add(r2); - CaptureStdout(); ASSERT_EQ(Reporter::REPORT_FINISHED, reporter->runReport()); - EXPECT_THAT(GetCapturedStdout(), StrEq("\n\x5" "abcde")); + + string result; + ReadFileToString(tf.path, &result); + EXPECT_THAT(result, StrEq("\n\x5" "abcde")); + EXPECT_EQ(l->startInvoked, 2); EXPECT_EQ(l->finishInvoked, 2); EXPECT_TRUE(l->startSections.empty()); diff --git a/libs/protoutil/include/android/util/ProtoOutputStream.h b/libs/protoutil/include/android/util/ProtoOutputStream.h index 0f1ccedeaae2..10be6499417f 100644 --- a/libs/protoutil/include/android/util/ProtoOutputStream.h +++ b/libs/protoutil/include/android/util/ProtoOutputStream.h @@ -49,7 +49,7 @@ public: bool write(uint64_t fieldId, long long val); bool write(uint64_t fieldId, bool val); bool write(uint64_t fieldId, std::string val); - bool write(uint64_t fieldId, const char* val); + bool write(uint64_t fieldId, const char* val, size_t size); /** * Starts a sub-message write session. @@ -103,4 +103,4 @@ private: } } -#endif // ANDROID_UTIL_PROTOOUTPUT_STREAM_H
\ No newline at end of file +#endif // ANDROID_UTIL_PROTOOUTPUT_STREAM_H diff --git a/libs/protoutil/src/ProtoOutputStream.cpp b/libs/protoutil/src/ProtoOutputStream.cpp index 15144ac2eb28..9dadf1c20510 100644 --- a/libs/protoutil/src/ProtoOutputStream.cpp +++ b/libs/protoutil/src/ProtoOutputStream.cpp @@ -225,14 +225,13 @@ ProtoOutputStream::write(uint64_t fieldId, string val) } bool -ProtoOutputStream::write(uint64_t fieldId, const char* val) +ProtoOutputStream::write(uint64_t fieldId, const char* val, size_t size) { if (mCompact) return false; const uint32_t id = (uint32_t)fieldId; - int size = 0; - while (val[size] != '\0') size++; switch (fieldId & FIELD_TYPE_MASK) { case TYPE_STRING: + case TYPE_BYTES: writeUtf8StringImpl(id, val, size); return true; default: |