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

Side by Side Diff: ppapi/tests/test_var.cc

Issue 6995083: Proxy PPB_Var, fix o-o-p string var id tracking. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated copyright header Created 9 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « ppapi/tests/test_var.h ('k') | ppapi/tests/test_var_deprecated.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ppapi/tests/test_var.h"
6
7 #include <string.h>
8
9 #include <limits>
10
11 #include "base/basictypes.h"
12 #include "ppapi/c/dev/ppb_testing_dev.h"
13 #include "ppapi/c/pp_var.h"
14 #include "ppapi/c/ppb_var.h"
15 #include "ppapi/cpp/instance.h"
16 #include "ppapi/cpp/module.h"
17 #include "ppapi/cpp/var.h"
18 #include "ppapi/tests/testing_instance.h"
19
20 namespace {
21
22 uint32_t kInvalidLength = static_cast<uint32_t>(-1);
23
24 } // namespace
25
26 REGISTER_TEST_CASE(Var);
27
28 bool TestVar::Init() {
29 var_interface_ = reinterpret_cast<const PPB_Var*>(
30 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE));
31 return var_interface_ && InitTestingInterface();
32 }
33
34 void TestVar::RunTest() {
35 RUN_TEST(BasicString);
36 RUN_TEST(InvalidAndEmpty);
37 RUN_TEST(InvalidUtf8);
38 RUN_TEST(NullInputInUtf8Conversion);
39 RUN_TEST(ValidUtf8);
40 RUN_TEST(Utf8WithEmbeddedNulls);
41 RUN_TEST(VarToUtf8ForWrongType);
42 }
43
44 std::string TestVar::TestBasicString() {
45 uint32_t before_object = testing_interface_->GetLiveObjectsForInstance(
46 instance_->pp_instance());
47 {
48 const char kStr[] = "Hello";
49 const uint32_t kStrLen(arraysize(kStr) - 1);
50 PP_Var str = var_interface_->VarFromUtf8(pp::Module::Get()->pp_module(),
51 kStr, kStrLen);
52 ASSERT_EQ(PP_VARTYPE_STRING, str.type);
53
54 // Reading back the string should work.
55 uint32_t len = 0;
56 const char* result = var_interface_->VarToUtf8(str, &len);
57 ASSERT_EQ(kStrLen, len);
58 ASSERT_EQ(0, strncmp(kStr, result, kStrLen));
59
60 // Destroy the string, readback should now fail.
61 var_interface_->Release(str);
62 result = var_interface_->VarToUtf8(str, &len);
63 ASSERT_EQ(0, len);
64 ASSERT_EQ(NULL, result);
65 }
66
67 // Make sure nothing leaked.
68 ASSERT_TRUE(testing_interface_->GetLiveObjectsForInstance(
69 instance_->pp_instance()) == before_object);
70
71 PASS();
72 }
73
74 std::string TestVar::TestInvalidAndEmpty() {
75 PP_Var invalid_string;
76 invalid_string.type = PP_VARTYPE_STRING;
77 invalid_string.value.as_id = 31415926;
78
79 // Invalid strings should give NULL as the return value.
80 uint32_t len = std::numeric_limits<uint32_t>::max();
81 const char* result = var_interface_->VarToUtf8(invalid_string, &len);
82 ASSERT_EQ(0, len);
83 ASSERT_EQ(NULL, result);
84
85 // Same with vars that are not strings.
86 len = std::numeric_limits<uint32_t>::max();
87 pp::Var int_var(42);
88 result = var_interface_->VarToUtf8(int_var.pp_var(), &len);
89 ASSERT_EQ(0, len);
90 ASSERT_EQ(NULL, result);
91
92 // Empty strings should return non-NULL.
93 pp::Var empty_string("");
94 len = std::numeric_limits<uint32_t>::max();
95 result = var_interface_->VarToUtf8(empty_string.pp_var(), &len);
96 ASSERT_EQ(0, len);
97 ASSERT_NE(NULL, result);
98
99 PASS();
100 }
101
102 std::string TestVar::TestInvalidUtf8() {
103 // utf8じゃăȘい (japanese for "is not utf8") in shift-jis encoding.
104 static const char kSjisString[] = "utf8\x82\xb6\x82\xe1\x82\xc8\x82\xa2";
105 pp::Var sjis(kSjisString);
106 if (!sjis.is_null())
107 return "Non-UTF8 string was permitted erroneously.";
108
109 PASS();
110 }
111
112 std::string TestVar::TestNullInputInUtf8Conversion() {
113 // This test talks directly to the C interface to access edge cases that
114 // cannot be exercised via the C++ interface.
115 PP_Var converted_string;
116
117 // 0-length string should not dereference input string, and should produce
118 // an empty string.
119 converted_string = var_interface_->VarFromUtf8(
120 pp::Module::Get()->pp_module(), NULL, 0);
121 if (converted_string.type != PP_VARTYPE_STRING) {
122 return "Expected 0 length to return empty string.";
123 }
124
125 // Now convert it back.
126 uint32_t length = kInvalidLength;
127 const char* result = NULL;
128 result = var_interface_->VarToUtf8(converted_string, &length);
129 if (length != 0) {
130 return "Expected 0 length string on conversion.";
131 }
132 if (result == NULL) {
133 return "Expected a non-null result for 0-lengthed string from VarToUtf8.";
134 }
135
136 // Should not crash, and make an empty string.
137 const char* null_string = NULL;
138 pp::Var null_var(null_string);
139 if (!null_var.is_string() || null_var.AsString() != "") {
140 return "Expected NULL input to make an empty string Var.";
141 }
142
143 PASS();
144 }
145
146 std::string TestVar::TestValidUtf8() {
147 // From UTF8 string -> PP_Var.
148 // Chinese for "I am utf8."
149 static const char kValidUtf8[] = "\xe6\x88\x91\xe6\x98\xafutf8.";
150 pp::Var converted_string(kValidUtf8);
151
152 if (converted_string.is_null())
153 return "Unable to convert valid utf8 to var.";
154
155 // Since we're already here, test PP_Var back to UTF8 string.
156 std::string returned_string = converted_string.AsString();
157
158 // We need to check against 1 less than sizeof because the resulting string
159 // is technically not NULL terminated by API design.
160 if (returned_string.size() != sizeof(kValidUtf8) - 1) {
161 return "Unable to convert utf8 string back from var.";
162 }
163 if (returned_string != kValidUtf8) {
164 return "String mismatches on conversion back from PP_Var.";
165 }
166
167 PASS();
168 }
169
170 std::string TestVar::TestUtf8WithEmbeddedNulls() {
171 // From UTF8 string with embedded nulls -> PP_Var.
172 // Chinese for "also utf8."
173 static const char kUtf8WithEmbededNull[] = "\xe6\xb9\x9f\xe6\x98\xaf\0utf8.";
174 std::string orig_string(kUtf8WithEmbededNull,
175 sizeof(kUtf8WithEmbededNull) -1);
176 pp::Var converted_string(orig_string);
177
178 if (converted_string.is_null())
179 return "Unable to convert utf8 with embedded nulls to var.";
180
181 // Since we're already here, test PP_Var back to UTF8 string.
182 std::string returned_string = converted_string.AsString();
183
184 if (returned_string.size() != orig_string.size()) {
185 return "Unable to convert utf8 with embedded nulls back from var.";
186 }
187 if (returned_string != orig_string) {
188 return "String mismatches on conversion back from PP_Var.";
189 }
190
191 PASS();
192 }
193
194 std::string TestVar::TestVarToUtf8ForWrongType() {
195 uint32_t length = kInvalidLength;
196 const char* result = NULL;
197 result = var_interface_->VarToUtf8(PP_MakeUndefined(), &length);
198 if (length != 0) {
199 return "Expected 0 on string conversion from Void var.";
200 }
201 if (result != NULL) {
202 return "Expected NULL on string conversion from Void var.";
203 }
204
205 length = kInvalidLength;
206 result = NULL;
207 result = var_interface_->VarToUtf8(PP_MakeNull(), &length);
208 if (length != 0) {
209 return "Expected 0 on string conversion from Null var.";
210 }
211 if (result != NULL) {
212 return "Expected NULL on string conversion from Null var.";
213 }
214
215 length = kInvalidLength;
216 result = NULL;
217 result = var_interface_->VarToUtf8(PP_MakeBool(PP_TRUE), &length);
218 if (length != 0) {
219 return "Expected 0 on string conversion from Bool var.";
220 }
221 if (result != NULL) {
222 return "Expected NULL on string conversion from Bool var.";
223 }
224
225 length = kInvalidLength;
226 result = NULL;
227 result = var_interface_->VarToUtf8(PP_MakeInt32(1), &length);
228 if (length != 0) {
229 return "Expected 0 on string conversion from Int32 var.";
230 }
231 if (result != NULL) {
232 return "Expected NULL on string conversion from Int32 var.";
233 }
234
235 length = kInvalidLength;
236 result = NULL;
237 result = var_interface_->VarToUtf8(PP_MakeDouble(1.0), &length);
238 if (length != 0) {
239 return "Expected 0 on string conversion from Double var.";
240 }
241 if (result != NULL) {
242 return "Expected NULL on string conversion from Double var.";
243 }
244
245 PASS();
246 }
247
OLDNEW
« no previous file with comments | « ppapi/tests/test_var.h ('k') | ppapi/tests/test_var_deprecated.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698