Exemplo n.º 1
0
MojErr MojDbPermissionTest::testKindPermissions(MojDb& db)
{
	MojObject kind;
	MojErr err = kind.fromJson(MojTestKind1);
	MojTestErrCheck(err);

	// new kind, mismatched owner and caller
	MojDbReq req(false);
	err = req.domain(_T("com.bar"));
	MojTestErrCheck(err);
	err = db.putKind(kind, MojDb::FlagNone, req);
	MojTestErrExpected(err, MojErrDbPermissionDenied);
	// new kind, matched owner and caller
	err = req.domain(_T("com.foo"));
	MojTestErrCheck(err);
	err = db.putKind(kind, MojDb::FlagNone, req);
	MojTestErrCheck(err);
	// existing kind, matched owner and caller
	err = db.putKind(kind, MojDb::FlagNone, req);
	MojTestErrCheck(err);
	// existing kind, mismatched owner and caller
	err = req.domain(_T("com.bar"));
	MojTestErrCheck(err);
	err = db.putKind(kind, MojDb::FlagNone, req);
	MojTestErrExpected(err, MojErrDbPermissionDenied);
	// delKind, mismatched owner and caller
	MojString id;
	err = id.assign(_T("PermissionTest:1"));
	MojTestErrCheck(err);
	bool found = false;
	err = db.delKind(id, found, MojDb::FlagNone, req);
	MojTestErrExpected(err, MojErrDbPermissionDenied);

	return MojErrNone;
}
Exemplo n.º 2
0
MojErr MojDbQuotaTest::testEnforce(MojDb& db)
{
	// set quota size to current usage
	MojInt64 quotaUsage1 = 0;
	MojErr err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage1);
	MojTestErrCheck(err);
	MojObject obj;
	err = obj.putString(_T("owner"), _T("com.foo.bar"));
	MojErrCheck(err);
	err = obj.putInt(_T("size"), quotaUsage1);
	MojErrCheck(err);
	err = db.putQuotas(&obj, &obj + 1);
	MojErrCheck(err);
	err = put(db, MojTestKind1Objects[3]);
	MojTestErrExpected(err, MojErrDbQuotaExceeded);
	// Try to delete the kind
	MojString kindStr;
	err = kindStr.assign(_T("Test:1"));
	MojTestErrCheck(err);
	bool found = false;
	err = db.delKind(kindStr, found);

    //The delete should be failure, because it contain sub kind "Test2:1"
    MojTestErrExpected(err,MojErrDbKindHasSubKinds); 
    MojTestAssert(!found); 

	return MojErrNone;
}
Exemplo n.º 3
0
MojErr MojDbQuotaTest::testEnforce(MojDb& db)
{
	// set quota size to current usage
	MojInt64 quotaUsage1 = 0;
	MojErr err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage1);
	MojTestErrCheck(err);
	MojObject obj;
	err = obj.putString(_T("owner"), _T("com.foo.bar"));
	MojErrCheck(err);
	err = obj.putInt(_T("size"), quotaUsage1);
	MojErrCheck(err);
	err = db.putQuotas(&obj, &obj + 1);
	MojErrCheck(err);
	err = put(db, MojTestKind1Objects[3]);
	MojTestErrExpected(err, MojErrDbQuotaExceeded);
	// make sure we can delete the kind
	MojString kindStr;
	err = kindStr.assign(_T("Test:1"));
	MojTestErrCheck(err);
	bool found = false;
	err = db.delKind(kindStr, found);
	MojTestErrCheck(err);
	MojTestAssert(found);

	return MojErrNone;
}
Exemplo n.º 4
0
MojErr MojDbPurgeTest::delKindTest(MojDb& db)
{

	// start from scratch - purge everything
	MojUInt32 count = 0;
	MojErr err = db.purge(count, 0);
	MojTestErrCheck(err);
	MojTestAssert(count > 0);

	// purge again, make sure nothing is left
	err = db.purge(count, 0);
	MojTestErrCheck(err);
	MojTestAssert(count == 0);

	// make sure at least 2 objects exist
	MojDbQuery q;
	err = q.from(_T("PurgeTest:1"));
	MojTestErrCheck(err);
	MojDbCursor cursor;
	err = db.find(q, cursor);
	MojTestErrCheck(err);

	MojUInt32 objCount;
	err = cursor.count(objCount);
	MojTestErrCheck(err);
	err = cursor.close();
	MojTestErrCheck(err);

	if (objCount <= 1) {
		for (int i = objCount; i < 2; i++) {
			MojObject obj;
			err = obj.fromJson(MojTestObjStr1);
			MojTestErrCheck(err);
			err = db.put(obj);
			MojTestErrCheck(err);
			objCount++;
		}
	}

	// delete half the objects
	q.limit(objCount / 2);

	MojUInt32 delCount;
	err = db.del(q, delCount);
	MojTestErrCheck(err);

	// delete the kind
	MojString idStr;
	err = idStr.assign(_T("PurgeTest:1"));
	MojTestErrCheck(err);
	MojObject id(idStr);
	bool found;
	err = db.delKind(id, found);
	MojTestErrCheck(err);
	MojTestAssert(found);

	/*MojUInt32 deletedObjCount;
	err = q.where(_T("_del"), MojDbQuery::OpEq, true);
	MojTestErrCheck(err);
	q.includeDeleted(true);
	err = db.find(q, cursor);
	MojTestErrCheck(err);
	err = cursor.count(deletedObjCount);
	MojTestErrCheck(err);
	err = cursor.close();
	MojTestErrCheck(err);

	// now all the objects should be deleted
	MojTestAssert(deletedObjCount == objCount);*/

	// purge now
	err = db.purge(count, 0);
	MojTestErrCheck(err);

	return MojErrNone;
}
Exemplo n.º 5
0
MojErr MojDbQuotaTest::testUsage(MojDb& db)
{
	// put quota
	MojObject obj;
	MojErr err = obj.fromJson(_T("{\"owner\":\"com.foo.bar\",\"size\":1000}"));
	MojErrCheck(err);
	err = db.putQuotas(&obj, &obj + 1);
	MojErrCheck(err);
	// empty
	MojInt64 kindUsage = 0;
	err = getKindUsage(db, _T("Test:1"), kindUsage);
	MojTestErrCheck(err);
	MojTestAssert(kindUsage == 0);
	MojInt64 quotaUsage = 0;
	err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage);
	MojTestErrCheck(err);
	MojTestAssert(quotaUsage == 0);
	// new obj
	err = put(db, MojTestKind1Objects[0]);
	MojTestErrCheck(err);
	MojInt64 kindUsage1 = 0;
	err = getKindUsage(db, _T("Test:1"), kindUsage1);
	MojTestErrCheck(err);
	MojTestAssert(kindUsage1 > 0);
	MojInt64 quotaUsage1 = 0;
	err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage1);
	MojTestErrCheck(err);
	MojTestAssert(quotaUsage1 > 0);
	// add prop to existing obj
	err = obj.fromJson(MojTestKind1Objects[0]);
	MojTestErrCheck(err);
	err = obj.put(_T("bar"), 2);
	MojTestErrCheck(err);
	err = db.put(obj, MojDb::FlagForce);
	MojTestErrCheck(err);
	MojInt64 kindUsage2 = 0;
	err = getKindUsage(db, _T("Test:1"), kindUsage2);
	MojTestErrCheck(err);
	MojTestAssert(kindUsage2 > kindUsage1);
	MojInt64 quotaUsage2 = 0;
	err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage2);
	MojTestErrCheck(err);
	MojTestAssert(quotaUsage2 > quotaUsage1);
	// add 2nd obj
	err = put(db, MojTestKind1Objects[1]);
	MojTestErrCheck(err);
	MojInt64 kindUsage3 = 0;
	err = getKindUsage(db, _T("Test:1"), kindUsage3);
	MojTestErrCheck(err);
	MojTestAssert(kindUsage3 > kindUsage2);
	MojInt64 quotaUsage3 = 0;
	err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage3);
	MojTestErrCheck(err);
	MojTestAssert(quotaUsage3 > quotaUsage2);
	// del first obj
	bool found = false;
	err = db.del(1, found, MojDb::FlagPurge);
	MojTestErrCheck(err);
	MojTestAssert(found);
	MojInt64 kindUsage4 = 0;
	err = getKindUsage(db, _T("Test:1"), kindUsage4);
	MojTestErrCheck(err);
	MojTestAssert(kindUsage4 == kindUsage3 - kindUsage2);
	MojInt64 quotaUsage4 = 0;
	err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage4);
	MojTestErrCheck(err);
	MojTestAssert(quotaUsage4 == quotaUsage3 - quotaUsage2);
	// add index
	err = obj.fromJson(MojTestKind1Str2);
	MojTestErrCheck(err);
	err = db.putKind(obj);
	MojTestErrCheck(err);
	MojInt64 kindUsage5 = 0;
	err = getKindUsage(db, _T("Test:1"), kindUsage5);
	MojTestErrCheck(err);
	MojTestAssert(kindUsage5 > kindUsage4);
	MojInt64 quotaUsage5 = 0;
	err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage5);
	MojTestErrCheck(err);
	MojTestAssert(quotaUsage5 > quotaUsage4);
	// update locale
	err = db.updateLocale(_T("FR_fr"));
	MojTestErrCheck(err);
	err = db.updateLocale(_T("EN_us"));
	MojTestErrCheck(err);
	MojInt64 kindUsage6 = 0;
	err = getKindUsage(db, _T("Test:1"), kindUsage6);
	MojTestErrCheck(err);
	MojTestAssert(kindUsage6 == kindUsage5);
	MojInt64 quotaUsage6 = 0;
	err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage6);
	MojTestErrCheck(err);
	MojTestAssert(quotaUsage6 == quotaUsage5);
	// drop index
	err = obj.fromJson(MojTestKind1Str1);
	MojTestErrCheck(err);
	err = db.putKind(obj);
	MojTestErrCheck(err);
	MojInt64 kindUsage7 = 0;
	err = getKindUsage(db, _T("Test:1"), kindUsage7);
	MojTestErrCheck(err);
	MojTestAssert(kindUsage7 == kindUsage4);
	MojInt64 quotaUsage7 = 0;
	err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage7);
	MojTestErrCheck(err);
	MojTestAssert(quotaUsage7 == quotaUsage4);
	// drop kind
	MojString kindStr;
	err = kindStr.assign(_T("Test:1"));
	MojTestErrCheck(err);
	err = db.delKind(kindStr, found);
	MojTestErrCheck(err);
	MojTestAssert(found);
	MojInt64 kindUsage8 = 0;
	err = getKindUsage(db, _T("Test:1"), kindUsage8);
	MojTestErrCheck(err);
	MojTestAssert(kindUsage8 == 0);
	MojInt64 quotaUsage8 = 0;
	err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage8);
	MojTestErrCheck(err);
	MojTestAssert(quotaUsage8 == 0);
	err = db.quotaStats(obj);
	MojErrCheck(err);

	return MojErrNone;
}