diff options
author | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-17 22:28:37 +0000 |
---|---|---|
committer | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-17 22:28:37 +0000 |
commit | 27c263aaaa3f1746555cc9f0eefd7dd64778900e (patch) | |
tree | 835a28e0ec6781eb1a1bf6099c593ba5dc0f2188 /jingle | |
parent | a8fa4cae78997e202331dda99748b93a78394f59 (diff) | |
download | chromium_src-27c263aaaa3f1746555cc9f0eefd7dd64778900e.zip chromium_src-27c263aaaa3f1746555cc9f0eefd7dd64778900e.tar.gz chromium_src-27c263aaaa3f1746555cc9f0eefd7dd64778900e.tar.bz2 |
[Sync] Fix crash when converting Notification to string
Also add test.
BUG=142925
Review URL: https://chromiumcodereview.appspot.com/10831347
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@152158 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'jingle')
-rw-r--r-- | jingle/jingle.gyp | 1 | ||||
-rw-r--r-- | jingle/notifier/listener/notification_defines.cc | 16 | ||||
-rw-r--r-- | jingle/notifier/listener/notification_defines_unittest.cc | 27 |
3 files changed, 35 insertions, 9 deletions
diff --git a/jingle/jingle.gyp b/jingle/jingle.gyp index 7902678..015c84f 100644 --- a/jingle/jingle.gyp +++ b/jingle/jingle.gyp @@ -171,6 +171,7 @@ 'notifier/communicator/login_settings_unittest.cc', 'notifier/communicator/single_login_attempt_unittest.cc', 'notifier/listener/non_blocking_push_client_unittest.cc', + 'notifier/listener/notification_defines_unittest.cc', 'notifier/listener/push_client_unittest.cc', 'notifier/listener/push_notifications_send_update_task_unittest.cc', 'notifier/listener/push_notifications_subscribe_task_unittest.cc', diff --git a/jingle/notifier/listener/notification_defines.cc b/jingle/notifier/listener/notification_defines.cc index f496df1..7431ea2 100644 --- a/jingle/notifier/listener/notification_defines.cc +++ b/jingle/notifier/listener/notification_defines.cc @@ -6,7 +6,7 @@ #include <cstddef> -#include "base/json/json_writer.h" +#include "base/json/string_escape.h" #include "base/logging.h" #include "base/string_util.h" #include "base/values.h" @@ -65,14 +65,12 @@ bool Notification::Equals(const Notification& other) const { } std::string Notification::ToString() const { - // Put into a DictionaryValue and convert to a string; this gives a - // nice hex-encoding of |data|, which may be a binary string. - DictionaryValue dict; - dict.SetString("channel", channel); - dict.SetString("data", data); - std::string str; - base::JSONWriter::Write(&dict, &str); - return str; + // |channel| or |data| could hold binary data, so use GetDoubleQuotedJson() + // to escape them. + const std::string& printable_channel = base::GetDoubleQuotedJson(channel); + const std::string& printable_data = base::GetDoubleQuotedJson(data); + return + "{ channel: " + printable_channel + ", data: " + printable_data + " }"; } } // namespace notifier diff --git a/jingle/notifier/listener/notification_defines_unittest.cc b/jingle/notifier/listener/notification_defines_unittest.cc new file mode 100644 index 0000000..85dd889 --- /dev/null +++ b/jingle/notifier/listener/notification_defines_unittest.cc @@ -0,0 +1,27 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <string> + +#include "base/string_util.h" +#include "jingle/notifier/listener/notification_defines.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace notifier { +namespace { + +class NotificationTest : public testing::Test {}; + +// Create a notification with binary data in the data field. +// Converting it to string shouldn't cause a crash. +TEST_F(NotificationTest, BinaryData) { + const char kNonUtf8Data[] = { '\xff', '\0' }; + EXPECT_FALSE(IsStringUTF8(kNonUtf8Data)); + Notification notification; + notification.data = kNonUtf8Data; + EXPECT_EQ("{ channel: \"\", data: \"\\u00FF\" }", notification.ToString()); +} + +} // namespace +} // namespace notifier |