summaryrefslogtreecommitdiffstats
path: root/ppapi/tests/test_var_deprecated.cc
blob: b4bdee9473516dfee212766f62c017fdc0986a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright (c) 2010 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 "ppapi/tests/test_var_deprecated.h"

#include <string.h>

#include <limits>

#include "ppapi/c/pp_var.h"
#include "ppapi/c/dev/ppb_testing_dev.h"
#include "ppapi/c/dev/ppb_var_deprecated.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var.h"
#include "ppapi/tests/testing_instance.h"

static uint32_t kInvalidLength = static_cast<uint32_t>(-1);

REGISTER_TEST_CASE(VarDeprecated);

bool TestVarDeprecated::Init() {
  var_interface_ = reinterpret_cast<PPB_Var_Deprecated const*>(
      pp::Module::Get()->GetBrowserInterface(PPB_VAR_DEPRECATED_INTERFACE));
  testing_interface_ = reinterpret_cast<PPB_Testing_Dev const*>(
      pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE));
  if (!testing_interface_) {
    // Give a more helpful error message for the testing interface being gone
    // since that needs special enabling in Chrome.
    instance_->AppendError("This test needs the testing interface, which is "
        "not currently available. In Chrome, use --enable-pepper-testing when "
        "launching.");
  }
  return var_interface_ && testing_interface_;
}

void TestVarDeprecated::RunTest() {
  RUN_TEST(BasicString);
  RUN_TEST(InvalidAndEmpty);
  RUN_TEST(InvalidUtf8);
  RUN_TEST(NullInputInUtf8Conversion);
  RUN_TEST(ValidUtf8);
  RUN_TEST(Utf8WithEmbeddedNulls);
  RUN_TEST(VarToUtf8ForWrongType);
  RUN_TEST(HasPropertyAndMethod);
}

std::string TestVarDeprecated::TestBasicString() {
  uint32_t before_object = testing_interface_->GetLiveObjectCount(
      pp::Module::Get()->pp_module());
  {
    const uint32_t kStrLen = 5;
    const char kStr[kStrLen + 1] = "Hello";
    PP_Var str = var_interface_->VarFromUtf8(pp::Module::Get()->pp_module(),
                                             kStr, sizeof(kStr) - 1);
    ASSERT_EQ(PP_VARTYPE_STRING, str.type);

    // Reading back the string should work.
    uint32_t len = 0;
    const char* result = var_interface_->VarToUtf8(str, &len);
    ASSERT_EQ(kStrLen, len);
    ASSERT_EQ(0, strncmp(kStr, result, kStrLen));

    // Destroy the string, readback should now fail.
    var_interface_->Release(str);
    result = var_interface_->VarToUtf8(str, &len);
    ASSERT_EQ(0, len);
    ASSERT_EQ(NULL, result);
  }

  // Make sure nothing leaked.
  ASSERT_TRUE(testing_interface_->GetLiveObjectCount(
      pp::Module::Get()->pp_module()) == before_object);

  return std::string();
}

std::string TestVarDeprecated::TestInvalidAndEmpty() {
  PP_Var invalid_string;
  invalid_string.type = PP_VARTYPE_STRING;
  invalid_string.value.as_id = 31415926;

  // Invalid strings should give NULL as the return value.
  uint32_t len = std::numeric_limits<uint32_t>::max();
  const char* result = var_interface_->VarToUtf8(invalid_string, &len);
  ASSERT_EQ(0, len);
  ASSERT_EQ(NULL, result);

  // Same with vars that are not strings.
  len = std::numeric_limits<uint32_t>::max();
  pp::Var int_var(42);
  result = var_interface_->VarToUtf8(int_var.pp_var(), &len);
  ASSERT_EQ(0, len);
  ASSERT_EQ(NULL, result);

  // Empty strings should return non-NULL.
  pp::Var empty_string("");
  len = std::numeric_limits<uint32_t>::max();
  result = var_interface_->VarToUtf8(empty_string.pp_var(), &len);
  ASSERT_EQ(0, len);
  ASSERT_NE(NULL, result);

  return std::string();
}

std::string TestVarDeprecated::TestInvalidUtf8() {
  // utf8じゃăȘい (japanese for "is not utf8") in shift-jis encoding.
  static const char kSjisString[] = "utf8\x82\xb6\x82\xe1\x82\xc8\x82\xa2";
  pp::Var sjis(kSjisString);
  if (!sjis.is_null())
    return "Non-UTF8 string permitted.";

  return "";
}

std::string TestVarDeprecated::TestNullInputInUtf8Conversion() {
  // This test talks directly to the C interface to access edge cases that
  // cannot be exercised via the C++ interface.
  PP_Var converted_string;

  // 0-length string should not dereference input string, and should produce
  // an empty string.
  converted_string = var_interface_->VarFromUtf8(
      pp::Module::Get()->pp_module(), NULL, 0);
  if (converted_string.type != PP_VARTYPE_STRING) {
    return "Expected 0 length to return empty string.";
  }

  // Now convert it back.
  uint32_t length = kInvalidLength;
  const char* result = NULL;
  result = var_interface_->VarToUtf8(converted_string, &length);
  if (length != 0) {
    return "Expected 0 length string on conversion.";
  }
  if (result == NULL) {
    return "Expected a non-null result for 0-lengthed string from VarToUtf8.";
  }

  // Should not crash, and make an empty string.
  const char* null_string = NULL;
  pp::Var null_var(null_string);
  if (!null_var.is_string() || null_var.AsString() != "") {
    return "Expected NULL input to make an empty string Var.";
  }

  return "";
}

std::string TestVarDeprecated::TestValidUtf8() {
  // From UTF8 string -> PP_Var.
  // Chinese for "I am utf8."
  static const char kValidUtf8[] = "\xe6\x88\x91\xe6\x98\xafutf8.";
  pp::Var converted_string(kValidUtf8);

  if (converted_string.is_null())
    return "Unable to convert valid utf8 to var.";

  // Since we're already here, test PP_Var back to UTF8 string.
  std::string returned_string = converted_string.AsString();

  // We need to check against 1 less than sizeof because the resulting string
  // is technically not NULL terminated by API design.
  if (returned_string.size() != sizeof(kValidUtf8) - 1) {
    return "Unable to convert utf8 string back from var.";
  }
  if (returned_string != kValidUtf8) {
    return "String mismatches on conversion back from PP_Var.";
  }

  return "";
}

std::string TestVarDeprecated::TestUtf8WithEmbeddedNulls() {
  // From UTF8 string with embedded nulls -> PP_Var.
  // Chinese for "also utf8."
  static const char kUtf8WithEmbededNull[] = "\xe6\xb9\x9f\xe6\x98\xaf\0utf8.";
  std::string orig_string(kUtf8WithEmbededNull,
                          sizeof(kUtf8WithEmbededNull) -1);
  pp::Var converted_string(orig_string);

  if (converted_string.is_null())
    return "Unable to convert utf8 with embedded nulls to var.";

  // Since we're already here, test PP_Var back to UTF8 string.
  std::string returned_string = converted_string.AsString();

  if (returned_string.size() != orig_string.size()) {
    return "Unable to convert utf8 with embedded nulls back from var.";
  }
  if (returned_string != orig_string) {
    return "String mismatches on conversion back from PP_Var.";
  }

  return "";
}

std::string TestVarDeprecated::TestVarToUtf8ForWrongType() {
  uint32_t length = kInvalidLength;
  const char* result = NULL;
  result = var_interface_->VarToUtf8(PP_MakeUndefined(), &length);
  if (length != 0) {
    return "Expected 0 on string conversion from Void var.";
  }
  if (result != NULL) {
    return "Expected NULL on string conversion from Void var.";
  }

  length = kInvalidLength;
  result = NULL;
  result = var_interface_->VarToUtf8(PP_MakeNull(), &length);
  if (length != 0) {
    return "Expected 0 on string conversion from Null var.";
  }
  if (result != NULL) {
    return "Expected NULL on string conversion from Null var.";
  }

  length = kInvalidLength;
  result = NULL;
  result = var_interface_->VarToUtf8(PP_MakeBool(PP_TRUE), &length);
  if (length != 0) {
    return "Expected 0 on string conversion from Bool var.";
  }
  if (result != NULL) {
    return "Expected NULL on string conversion from Bool var.";
  }

  length = kInvalidLength;
  result = NULL;
  result = var_interface_->VarToUtf8(PP_MakeInt32(1), &length);
  if (length != 0) {
    return "Expected 0 on string conversion from Int32 var.";
  }
  if (result != NULL) {
    return "Expected NULL on string conversion from Int32 var.";
  }

  length = kInvalidLength;
  result = NULL;
  result = var_interface_->VarToUtf8(PP_MakeDouble(1.0), &length);
  if (length != 0) {
    return "Expected 0 on string conversion from Double var.";
  }
  if (result != NULL) {
    return "Expected NULL on string conversion from Double var.";
  }

  return "";
}

std::string TestVarDeprecated::TestHasPropertyAndMethod() {
  uint32_t before_objects = testing_interface_->GetLiveObjectCount(
      pp::Module::Get()->pp_module());
  {
    pp::Var window = instance_->GetWindowObject();
    ASSERT_TRUE(window.is_object());

    // Regular property.
    pp::Var exception;
    ASSERT_TRUE(window.HasProperty("scrollX", &exception));
    ASSERT_TRUE(exception.is_undefined());
    ASSERT_FALSE(window.HasMethod("scrollX", &exception));
    ASSERT_TRUE(exception.is_undefined());

    // Regular method (also counts as HasProperty).
    ASSERT_TRUE(window.HasProperty("find", &exception));
    ASSERT_TRUE(exception.is_undefined());
    ASSERT_TRUE(window.HasMethod("find", &exception));
    ASSERT_TRUE(exception.is_undefined());

    // Nonexistant ones should return false and not set the exception.
    ASSERT_FALSE(window.HasProperty("superEvilBit", &exception));
    ASSERT_TRUE(exception.is_undefined());
    ASSERT_FALSE(window.HasMethod("superEvilBit", &exception));
    ASSERT_TRUE(exception.is_undefined());

    // Check exception and return false on invalid property name.
    ASSERT_FALSE(window.HasProperty(3.14159, &exception));
    ASSERT_FALSE(exception.is_undefined());
    exception = pp::Var();

    exception = pp::Var();
    ASSERT_FALSE(window.HasMethod(3.14159, &exception));
    ASSERT_FALSE(exception.is_undefined());

    // Try to use something not an object.
    exception = pp::Var();
    pp::Var string_object("asdf");
    ASSERT_FALSE(string_object.HasProperty("find", &exception));
    ASSERT_FALSE(exception.is_undefined());
    exception = pp::Var();
    ASSERT_FALSE(string_object.HasMethod("find", &exception));
    ASSERT_FALSE(exception.is_undefined());

    // Try to use an invalid object (need to use the C API).
    PP_Var invalid_object;
    invalid_object.type = PP_VARTYPE_OBJECT;
    invalid_object.value.as_id = static_cast<int64_t>(-1234567);
    PP_Var exception2 = PP_MakeUndefined();
    ASSERT_FALSE(var_interface_->HasProperty(invalid_object,
                                             pp::Var("find").pp_var(),
                                             &exception2));
    ASSERT_NE(PP_VARTYPE_UNDEFINED, exception2.type);
    var_interface_->Release(exception2);

    exception2 = PP_MakeUndefined();
    ASSERT_FALSE(var_interface_->HasMethod(invalid_object,
                                           pp::Var("find").pp_var(),
                                           &exception2));
    ASSERT_NE(PP_VARTYPE_UNDEFINED, exception2.type);
    var_interface_->Release(exception2);

    // Get a valid property/method when the exception is set returns false.
    exception = pp::Var("Bad something-or-other exception");
    ASSERT_FALSE(window.HasProperty("find", &exception));
    ASSERT_FALSE(exception.is_undefined());
    ASSERT_FALSE(window.HasMethod("find", &exception));
    ASSERT_FALSE(exception.is_undefined());
  }

  // Make sure nothing leaked.
  ASSERT_TRUE(testing_interface_->GetLiveObjectCount(
      pp::Module::Get()->pp_module()) == before_objects);

  return std::string();
}