Skip to content

Commit

Permalink
[Sync] Fix crash when converting Notification to string
Browse files Browse the repository at this point in the history
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
  • Loading branch information
akalin@chromium.org committed Aug 17, 2012
1 parent a8fa4ca commit 27c263a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions jingle/jingle.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
16 changes: 7 additions & 9 deletions jingle/notifier/listener/notification_defines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
27 changes: 27 additions & 0 deletions jingle/notifier/listener/notification_defines_unittest.cc
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 27c263a

Please sign in to comment.