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

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

Powered by Google App Engine
This is Rietveld 408576698