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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: ppapi/tests/test_var.cc
diff --git a/ppapi/tests/test_var.cc b/ppapi/tests/test_var.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0b4d4ddbe5687170ae9ec060c1f6022ab64415b2
--- /dev/null
+++ b/ppapi/tests/test_var.cc
@@ -0,0 +1,246 @@
+// Copyright (c) 2011 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.h"
+
+#include <string.h>
+
+#include <limits>
+
+#include "ppapi/c/dev/ppb_testing_dev.h"
+#include "ppapi/c/pp_var.h"
+#include "ppapi/c/ppb_var.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+#include "ppapi/cpp/var.h"
+#include "ppapi/tests/testing_instance.h"
+
+namespace {
+
+uint32_t kInvalidLength = static_cast<uint32_t>(-1);
+
+} // namespace
+
+REGISTER_TEST_CASE(Var);
+
+bool TestVar::Init() {
+ 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)
+ pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE));
+ return var_interface_ && InitTestingInterface();
+}
+
+void TestVar::RunTest() {
+ RUN_TEST(BasicString);
+ RUN_TEST(InvalidAndEmpty);
+ RUN_TEST(InvalidUtf8);
+ RUN_TEST(NullInputInUtf8Conversion);
+ RUN_TEST(ValidUtf8);
+ RUN_TEST(Utf8WithEmbeddedNulls);
+ RUN_TEST(VarToUtf8ForWrongType);
+}
+
+std::string TestVar::TestBasicString() {
+ uint32_t before_object = testing_interface_->GetLiveObjectsForInstance(
+ instance_->pp_instance());
+ {
+ const uint32_t kStrLen = 5;
+ 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)
+ 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_->GetLiveObjectsForInstance(
+ instance_->pp_instance()) == before_object);
+
+ PASS();
+}
+
+std::string TestVar::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);
+
+ PASS();
+}
+
+std::string TestVar::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.";
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
+
+ PASS();
+}
+
+std::string TestVar::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.";
+ }
+
+ PASS();
+}
+
+std::string TestVar::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.";
+ }
+
+ PASS();
+}
+
+std::string TestVar::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.";
+ }
+
+ PASS();
+}
+
+std::string TestVar::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.";
+ }
+
+ PASS();
+}
+

Powered by Google App Engine
This is Rietveld 408576698