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

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: 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
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* testDictionary =
Eugene But (OOO till 7-30) 2016/09/12 16:12:00 Since you touching this file, could you please s/t
jif 2016/09/13 09:41:10 Done.
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(testDictionary));
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* innerDictionary = nullptr;
Eugene But (OOO till 7-30) 2016/09/12 16:12:00 And s/innerDictionary/inner_dictionary
jif 2016/09/13 09:41:10 Done.
86 dictionary->GetDictionary("Key2", &innerDictionary); 88 dictionary->GetDictionary("Key2", &innerDictionary);
87 EXPECT_NE(nullptr, innerDictionary); 89 EXPECT_NE(nullptr, innerDictionary);
88 90
89 double value3; 91 double value3;
90 innerDictionary->GetDouble("Key3", &value3); 92 innerDictionary->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 // Creates a dictionary with a cycle.
Eugene But (OOO till 7-30) 2016/09/12 16:12:00 NIT: s/Creates/Create We use imperative form for
jif 2016/09/13 09:41:10 Done. Thanks for the reminder :)
99 NSMutableDictionary* testDictionary =
Eugene But (OOO till 7-30) 2016/09/12 16:12:00 s/testDictionary/test_dictionary Also obj_c_key,
jif 2016/09/13 09:41:09 Done.
100 [NSMutableDictionary dictionaryWithCapacity:1];
101 NSMutableDictionary* testDictionary2 =
102 [NSMutableDictionary dictionaryWithCapacity:1];
103 const char* key = "key";
104 NSString* objCKey =
105 [NSString stringWithCString:key encoding:NSASCIIStringEncoding];
106 [testDictionary setObject:testDictionary2 forKey:objCKey];
Eugene But (OOO till 7-30) 2016/09/12 16:12:00 Do you want to use subscript?: test_dictionary[o
jif 2016/09/13 09:41:09 Done.
107 [testDictionary2 setObject:testDictionary forKey:objCKey];
108 // Breaks the retain cycle so that the dictionaries are freed.
109 base::ScopedClosureRunner runner(base::BindBlock(^{
110 [testDictionary2 removeAllObjects];
111 }));
112
113 // Checks that parsing the dictionary stopped at a depth of
114 // |kMaximumParsingRecursionDepth|.
115 std::unique_ptr<base::Value> value =
116 web::ValueResultFromWKResult(testDictionary);
117 base::DictionaryValue* currentDictionary = nullptr;
118 base::DictionaryValue* innerDictionary = nullptr;
119
120 value->GetAsDictionary(&currentDictionary);
121 EXPECT_NE(nullptr, currentDictionary);
122
123 int currentDepth = 0;
124 while (currentDepth <= kMaximumParsingRecursionDepth) {
Eugene But (OOO till 7-30) 2016/09/12 16:12:00 Should this be a for loop?: for (int current_depth
Eugene But (OOO till 7-30) 2016/09/12 16:12:00 How about using very large number instead of kMaxi
jif 2016/09/13 09:41:09 If someone changes kMaximumParsingRecursionDepth t
jif 2016/09/13 09:41:10 Done.
125 EXPECT_NE(nullptr, currentDictionary);
126 innerDictionary = nullptr;
127 currentDictionary->GetDictionary(key, &innerDictionary);
128 currentDictionary = innerDictionary;
129 currentDepth++;
130 }
131 EXPECT_EQ(nullptr, currentDictionary);
132 }
133
94 } // namespace web 134 } // namespace web
OLDNEW
« ios/web/web_state/ui/web_view_js_utils.mm ('K') | « 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