Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1200)

Side by Side Diff: ios/web/web_state/ui/web_view_js_utils_unittest.mm

Issue 2335483004: Limit depth of parsing of dictionaries returned by JS evaluation. (Closed)
Patch Set: Addressed comment. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/web/web_state/ui/web_view_js_utils.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #import "ios/web/web_state/ui/web_view_js_utils.h" 5 #import "ios/web/web_state/ui/web_view_js_utils.h"
6 6
7 #include "base/callback_helpers.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/mac/bind_objc_block.h"
8 #include "base/values.h" 10 #include "base/values.h"
9 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
10 12
11 namespace web { 13 namespace web {
12 14
13 // Tests that ValueResultFromWKResult converts nil value to nullptr. 15 // Tests that ValueResultFromWKResult converts nil value to nullptr.
14 TEST(WebViewJsUtilsTest, ValueResultFromUndefinedWKResult) { 16 TEST(WebViewJsUtilsTest, ValueResultFromUndefinedWKResult) {
15 EXPECT_FALSE(ValueResultFromWKResult(nil)); 17 EXPECT_FALSE(ValueResultFromWKResult(nil));
16 } 18 }
17 19
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 TEST(WebViewJsUtilsTest, ValueResultFromNullWKResult) { 63 TEST(WebViewJsUtilsTest, ValueResultFromNullWKResult) {
62 std::unique_ptr<base::Value> value( 64 std::unique_ptr<base::Value> value(
63 web::ValueResultFromWKResult([NSNull null])); 65 web::ValueResultFromWKResult([NSNull null]));
64 EXPECT_TRUE(value); 66 EXPECT_TRUE(value);
65 EXPECT_EQ(base::Value::TYPE_NULL, value->GetType()); 67 EXPECT_EQ(base::Value::TYPE_NULL, value->GetType());
66 } 68 }
67 69
68 // Tests that ValueResultFromWKResult converts NSDictionaries to properly 70 // Tests that ValueResultFromWKResult converts NSDictionaries to properly
69 // initialized base::DictionaryValue. 71 // initialized base::DictionaryValue.
70 TEST(WebViewJsUtilsTest, ValueResultFromDictionaryWKResult) { 72 TEST(WebViewJsUtilsTest, ValueResultFromDictionaryWKResult) {
71 NSDictionary* testDictionary = 73 NSDictionary* test_dictionary =
72 @{ @"Key1" : @"Value1", 74 @{ @"Key1" : @"Value1",
73 @"Key2" : @{@"Key3" : @42} }; 75 @"Key2" : @{@"Key3" : @42} };
74 76
75 std::unique_ptr<base::Value> value( 77 std::unique_ptr<base::Value> value(
76 web::ValueResultFromWKResult(testDictionary)); 78 web::ValueResultFromWKResult(test_dictionary));
77 base::DictionaryValue* dictionary = nullptr; 79 base::DictionaryValue* dictionary = nullptr;
78 value->GetAsDictionary(&dictionary); 80 value->GetAsDictionary(&dictionary);
79 EXPECT_NE(nullptr, dictionary); 81 EXPECT_NE(nullptr, dictionary);
80 82
81 std::string value1; 83 std::string value1;
82 dictionary->GetString("Key1", &value1); 84 dictionary->GetString("Key1", &value1);
83 EXPECT_EQ("Value1", value1); 85 EXPECT_EQ("Value1", value1);
84 86
85 base::DictionaryValue const* innerDictionary = nullptr; 87 base::DictionaryValue const* inner_dictionary = nullptr;
86 dictionary->GetDictionary("Key2", &innerDictionary); 88 dictionary->GetDictionary("Key2", &inner_dictionary);
87 EXPECT_NE(nullptr, innerDictionary); 89 EXPECT_NE(nullptr, inner_dictionary);
88 90
89 double value3; 91 double value3;
90 innerDictionary->GetDouble("Key3", &value3); 92 inner_dictionary->GetDouble("Key3", &value3);
91 EXPECT_EQ(42, value3); 93 EXPECT_EQ(42, value3);
92 } 94 }
93 95
96 // Tests that an NSDictionary with a cycle does not cause infinite recursion.
97 TEST(WebViewJsUtilsTest, ValueResultFromDictionaryWithDepthCheckWKResult) {
98 // Create a dictionary with a cycle.
99 NSMutableDictionary* test_dictionary =
100 [NSMutableDictionary dictionaryWithCapacity:1];
101 NSMutableDictionary* test_dictionary_2 =
102 [NSMutableDictionary dictionaryWithCapacity:1];
103 const char* key = "key";
104 NSString* obj_c_key =
105 [NSString stringWithCString:key encoding:NSASCIIStringEncoding];
106 test_dictionary[obj_c_key] = test_dictionary_2;
107 test_dictionary_2[obj_c_key] = test_dictionary;
108
109 // Break the retain cycle so that the dictionaries are freed.
110 base::ScopedClosureRunner runner(base::BindBlock(^{
111 [test_dictionary_2 removeAllObjects];
112 }));
113
114 // Check that parsing the dictionary stopped at a depth of
115 // |kMaximumParsingRecursionDepth|.
116 std::unique_ptr<base::Value> value =
117 web::ValueResultFromWKResult(test_dictionary);
118 base::DictionaryValue* current_dictionary = nullptr;
119 base::DictionaryValue* inner_dictionary = nullptr;
120
121 value->GetAsDictionary(&current_dictionary);
122 EXPECT_NE(nullptr, current_dictionary);
123
124 for (int current_depth = 0; current_depth <= kMaximumParsingRecursionDepth;
125 current_depth++) {
126 EXPECT_NE(nullptr, current_dictionary);
127 inner_dictionary = nullptr;
128 current_dictionary->GetDictionary(key, &inner_dictionary);
129 current_dictionary = inner_dictionary;
130 }
131 EXPECT_EQ(nullptr, current_dictionary);
132 }
133
94 } // namespace web 134 } // namespace web
OLDNEW
« no previous file with comments | « ios/web/web_state/ui/web_view_js_utils.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698