예제 #1
0
void
test_automatic_reset()
{
  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());

  // Need to create a table to populate sqlite_master with an entry.
  (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
    "CREATE TABLE test (id INTEGER PRIMARY KEY)"
  ));

  nsCOMPtr<mozIStorageStatement> stmt;
  (void)db->CreateStatement(NS_LITERAL_CSTRING(
    "SELECT * FROM sqlite_master"
  ), getter_AddRefs(stmt));

  // Reality check - make sure we start off in the right state.
  int32_t state = -1;
  (void)stmt->GetState(&state);
  do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY);

  // Start executing the statement, which will put it into an executing state.
  {
    mozStorageStatementScoper scoper(stmt);
    bool hasMore;
    do_check_true(NS_SUCCEEDED(stmt->ExecuteStep(&hasMore)));

    // Reality check that we are executing.
    state = -1;
    (void)stmt->GetState(&state);
    do_check_true(state ==
                  mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING);
  }

  // And we should be ready again.
  state = -1;
  (void)stmt->GetState(&state);
  do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY);
}
예제 #2
0
void
test_rethrow()
{
  HandleScope handle_scope;
  TryCatch trycatch;

  Persistent<Context> context = Context::New();
  Context::Scope context_scope(context);

  Handle<String> source = String::New("oasdohuasdnlqwoi");
  Handle<Script> script = Script::Compile(source);
  {
    TryCatch innerCatcher;
    Handle<Value> result = script->Run();

    do_check_true(innerCatcher.HasCaught());

    innerCatcher.ReThrow();
  }
  context.Dispose();

  do_check_true(trycatch.HasCaught());
}
예제 #3
0
// HACK ALERT: this test fails if it runs after any of the other storage tests
// because the storage service will have been initialized and the
// do_GetService() call in ServiceInitializer::Run will succeed. So we need a
// way to force it to run before all the other storage gtests. As it happens,
// gtest has a concept of "death tests" that, among other things, run before
// all non-death tests. By merely using a "DeathTest" suffix here this becomes
// a death test -- even though it doesn't use any of the normal death test
// features -- which ensures this is the first storage test to run.
TEST(storage_service_init_background_thread_DeathTest, Test)
{
  nsCOMPtr<nsIRunnable> event = new ServiceInitializer();
  do_check_true(event);

  nsCOMPtr<nsIThread> thread;
  do_check_success(NS_NewThread(getter_AddRefs(thread)));

  do_check_success(thread->Dispatch(event, NS_DISPATCH_NORMAL));

  // Shutting down the thread will spin the event loop until all work in its
  // event queue is completed.  This will act as our thread synchronization.
  do_check_success(thread->Shutdown());
}
예제 #4
0
void
test_Utf8Value_length()
{
  HandleScope handle_scope;
  Persistent<Context> context = Context::New();
  Context::Scope context_scope(context);

  char TEST_STRING[] = "this is a UTF-8 test!  This is pi: π";
  int TEST_LENGTH = strlen(TEST_STRING);
  Handle<String> str = String::New(TEST_STRING);
  do_check_eq(str->Length(), TEST_LENGTH - 1);
  String::Utf8Value k(str);
  do_check_eq(k.length(), TEST_LENGTH);
  do_check_true(0 == strcmp(TEST_STRING, *k));
  context.Dispose();
}
예제 #5
0
void
test_obj_getprop()
{
  HandleScope handle_scope;

  Persistent<Context> context = Context::New();

  Context::Scope context_scope(context);

  Handle<Object> obj = Object::New();

  Handle<Value> k = String::New("toString");
  Handle<Value> v = obj->Get(k);

  do_check_true(v->IsFunction());

  context.Dispose();
}
void
test_AutoCommit()
{
  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());

  // Create a table in a transaction, and make sure that it exists after the
  // transaction falls out of scope.  This means the Commit was successful.
  {
    mozStorageTransaction transaction(db, PR_TRUE);
    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
      "CREATE TABLE test (id INTEGER PRIMARY KEY)"
    ));
  }

  PRBool exists = PR_FALSE;
  (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
  do_check_true(exists);
}
예제 #7
0
void
test_CString()
{
    nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());

    // Create table with a single string column.
    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
                                   "CREATE TABLE test (str STRING)"
                               ));

    // Create statements to INSERT and SELECT the string.
    nsCOMPtr<mozIStorageStatement> insert, select;
    (void)db->CreateStatement(NS_LITERAL_CSTRING(
                                  "INSERT INTO test (str) VALUES (?1)"
                              ), getter_AddRefs(insert));
    (void)db->CreateStatement(NS_LITERAL_CSTRING(
                                  "SELECT str FROM test"
                              ), getter_AddRefs(select));

    // Roundtrip a string through the table, and ensure it comes out as expected.
    static const char sCharArray[] =
        "I'm not a \xff\x00\xac\xde\xbb ASCII string!";
    nsCAutoString inserted(sCharArray, NS_ARRAY_LENGTH(sCharArray) - 1);
    do_check_true(inserted.Length() == NS_ARRAY_LENGTH(sCharArray) - 1);
    {
        mozStorageStatementScoper scoper(insert);
        bool hasResult;
        do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, inserted)));
        do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
        do_check_false(hasResult);
    }

    {
        nsCAutoString result;

        mozStorageStatementScoper scoper(select);
        bool hasResult;
        do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
        do_check_true(hasResult);
        do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));

        do_check_true(result == inserted);
    }

    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test"));
}
예제 #8
0
void
test_Name()
{
  HandleScope handle_scope;
  Handle<ObjectTemplate> templ = ObjectTemplate::New();
  Handle<FunctionTemplate> fnT = v8::FunctionTemplate::New(AddOne);
  templ->Set("AddOne", fnT);
  fnT->SetClassName(String::NewSymbol("AddOne"));

  Persistent<Context> context = Context::New(NULL, templ);
  Handle<Script> script = Script::New(String::New("AddOne.name;"));

  Context::Scope scope(context);
  Handle<Value> v = script->Run();
  do_check_true(!v.IsEmpty());
  Handle<String> s = v->ToString();
  do_check_eq(s, String::NewSymbol("AddOne"));
  context.Dispose();
}
NS_IMETHODIMP
Spinner::HandleResult(mozIStorageResultSet *aResultSet)
{
  nsCOMPtr<mozIStorageRow> row;
  do_check_true(NS_SUCCEEDED(aResultSet->GetNextRow(getter_AddRefs(row))) && row);

  do_check_eq(row->AsInt32(0), 0);
  do_check_eq(row->AsInt64(0), 0);
  do_check_eq(row->AsDouble(0), 0.0);

  PRUint32 len = 100;
  do_check_eq(row->AsSharedUTF8String(0, &len), NULL);
  do_check_eq(len, 0);
  len = 100;
  do_check_eq(row->AsSharedWString(0, &len), NULL);
  do_check_eq(len, 0);
  len = 100;
  do_check_eq(row->AsSharedBlob(0, &len), NULL);
  do_check_eq(len, 0);

  do_check_eq(row->IsNull(0), true);
  return NS_OK;
}
예제 #10
0
void
test_obj_tmplexn() {
  HandleScope scope;

  Persistent<Context> context = Context::New();
  Context::Scope context_scope(context);

  Handle<ObjectTemplate> tmpl = ObjectTemplate::New();
  tmpl->SetNamedPropertyHandler(NamedGetterThrows, NamedSetterThrows, 0, NamedDeleterThrows);
  tmpl->SetIndexedPropertyHandler(IndexedGetterThrows, IndexedSetterThrows, 0, IndexedDeleterThrows);
  Handle<Object> obj = tmpl->NewInstance();
  Local<Object> global = context->Global();
  global->Set(String::New("testobj"), obj);

  Handle<String> source = String::New("var n = 0;"
                                      "try { testobj.myprop; } catch (e) { n += e; };"
                                      "try { testobj.myprop = (n+9); } catch (e) { n += e; }"
                                      "try { delete testobj.myprop; } catch (e) { n += e; };"
                                      "try { testobj[4]; } catch (e) { n += e; };"
                                      "try { testobj[5] = (n+9); } catch (e) { n += e; }"
                                      "try { delete testobj[6]; } catch (e) { n += e; }; n");

  // Compile the source code.
  Handle<Script> script = Script::Compile(source);

  TryCatch trycatch;
  // Run the script to get the result.
  Handle<Value> result = script->Run();

  do_check_false(result.IsEmpty());
  do_check_true(result->IsInt32());
  do_check_false(trycatch.HasCaught());
  JSInt32 i = result->Int32Value();
  do_check_eq(i, 21);
  context.Dispose();
}
예제 #11
0
void
test_ASCIIString()
{
    nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());

    // Create table with a single string column.
    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
                                   "CREATE TABLE test (str STRING)"
                               ));

    // Create statements to INSERT and SELECT the string.
    nsCOMPtr<mozIStorageStatement> insert, select;
    (void)db->CreateStatement(NS_LITERAL_CSTRING(
                                  "INSERT INTO test (str) VALUES (?1)"
                              ), getter_AddRefs(insert));
    (void)db->CreateStatement(NS_LITERAL_CSTRING(
                                  "SELECT str FROM test"
                              ), getter_AddRefs(select));

    // Roundtrip a string through the table, and ensure it comes out as expected.
    nsCAutoString inserted("I'm an ASCII string");
    {
        mozStorageStatementScoper scoper(insert);
        bool hasResult;
        do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, inserted)));
        do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
        do_check_false(hasResult);
    }

    nsCAutoString result;
    {
        mozStorageStatementScoper scoper(select);
        bool hasResult;
        do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
        do_check_true(hasResult);
        do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
    }

    do_check_true(result == inserted);

    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test"));
}
예제 #12
0
void
test_BasicCall()
{
  HandleScope handle_scope;
  Handle<ObjectTemplate> templ = ObjectTemplate::New();
  Handle<FunctionTemplate> fnT = v8::FunctionTemplate::New(AddOne);
  templ->Set("AddOne", fnT);

  Persistent<Context> context = Context::New(NULL, templ);
  Local<Value> addone = context->Global()->Get(String::New("AddOne"));
  do_check_true(!addone.IsEmpty());
  do_check_true(!addone->IsUndefined());
  do_check_true(addone->IsObject());
  do_check_true(addone->IsFunction());
  Local<Function> fn = addone.As<Function>();
  do_check_eq(fn, fnT->GetFunction());
  Local<Number> n = Number::New(0.5);
  Handle<Value> args[] = { n };
  Local<Value> v = fn->Call(context->Global(), 1, args);
  do_check_true(!v.IsEmpty());
  do_check_true(v->IsNumber());
  Local<Number> n2 = v->ToNumber();
  do_check_eq(n2->Value(), 1.5);
}
예제 #13
0
static Handle<Value> ReadTestVal(Local<String> propname, const AccessorInfo &info) {
  do_check_true(!propname.IsEmpty());
  return Integer::New(test_val);
}
예제 #14
0
void
test_UTFStrings()
{
    nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());

    // Create table with a single string column.
    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
                                   "CREATE TABLE test (str STRING)"
                               ));

    // Create statements to INSERT and SELECT the string.
    nsCOMPtr<mozIStorageStatement> insert, select;
    (void)db->CreateStatement(NS_LITERAL_CSTRING(
                                  "INSERT INTO test (str) VALUES (?1)"
                              ), getter_AddRefs(insert));
    (void)db->CreateStatement(NS_LITERAL_CSTRING(
                                  "SELECT str FROM test"
                              ), getter_AddRefs(select));

    // Roundtrip a UTF8 string through the table, using UTF8 input and output.
    static const char sCharArray[] =
        "I'm a \xc3\xbb\xc3\xbc\xc3\xa2\xc3\xa4\xc3\xa7 UTF8 string!";
    nsCAutoString insertedUTF8(sCharArray, NS_ARRAY_LENGTH(sCharArray) - 1);
    do_check_true(insertedUTF8.Length() == NS_ARRAY_LENGTH(sCharArray) - 1);
    NS_ConvertUTF8toUTF16 insertedUTF16(insertedUTF8);
    do_check_true(insertedUTF8 == NS_ConvertUTF16toUTF8(insertedUTF16));
    {
        mozStorageStatementScoper scoper(insert);
        bool hasResult;
        do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, insertedUTF8)));
        do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
        do_check_false(hasResult);
    }

    {
        nsCAutoString result;

        mozStorageStatementScoper scoper(select);
        bool hasResult;
        do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
        do_check_true(hasResult);
        do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));

        do_check_true(result == insertedUTF8);
    }

    // Use UTF8 input and UTF16 output.
    {
        nsAutoString result;

        mozStorageStatementScoper scoper(select);
        bool hasResult;
        do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
        do_check_true(hasResult);
        do_check_true(NS_SUCCEEDED(select->GetString(0, result)));

        do_check_true(result == insertedUTF16);
    }

    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test"));

    // Roundtrip the same string using UTF16 input and UTF8 output.
    {
        mozStorageStatementScoper scoper(insert);
        bool hasResult;
        do_check_true(NS_SUCCEEDED(insert->BindStringByIndex(0, insertedUTF16)));
        do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
        do_check_false(hasResult);
    }

    {
        nsCAutoString result;

        mozStorageStatementScoper scoper(select);
        bool hasResult;
        do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
        do_check_true(hasResult);
        do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));

        do_check_true(result == insertedUTF8);
    }

    // Use UTF16 input and UTF16 output.
    {
        nsAutoString result;

        mozStorageStatementScoper scoper(select);
        bool hasResult;
        do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
        do_check_true(hasResult);
        do_check_true(NS_SUCCEEDED(select->GetString(0, result)));

        do_check_true(result == insertedUTF16);
    }

    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test"));
}
 NS_IMETHOD Run()
 {
   nsCOMPtr<mozIStorageService> service = getService();
   do_check_true(service);
   return NS_OK;
 }