JNIEXPORT jboolean JNICALL Java_com_linkbubble_adblock_TPFilterParser_matchesTracker(JNIEnv *env, jobject obj, jstring baseHost, jstring host) { const char *nativeBaseHost = env->GetStringUTFChars(baseHost, 0); const char *nativeHost = env->GetStringUTFChars(host, 0); bool shouldBlock = tpParser.matchesTracker(nativeBaseHost, nativeHost); env->ReleaseStringUTFChars(baseHost, nativeBaseHost); env->ReleaseStringUTFChars(host, nativeHost); return shouldBlock ? JNI_TRUE : JNI_FALSE; }
/* * Class: com_linkbubble_adblock_TPFilterParser * Method: init * Signature: ()Ljava/lang/void; */ JNIEXPORT jstring JNICALL Java_com_linkbubble_adblock_TPFilterParser_findFirstPartyHosts(JNIEnv *env, jobject obj, jstring baseHost) { const char *nativeBaseHost = env->GetStringUTFChars(baseHost, 0); char *thirdPartyHosts = tpParser.findFirstPartyHosts(nativeBaseHost); env->ReleaseStringUTFChars(baseHost, nativeBaseHost); if (nullptr == thirdPartyHosts) { return env->NewString((jchar*)"", 0); } jstring result = env->NewStringUTF(thirdPartyHosts); delete []thirdPartyHosts; return result; }
/* * Class: com_linkbubble_adblock_TPFilterParser * Method: init * Signature: ()Ljava/lang/void; */ JNIEXPORT void JNICALL Java_com_linkbubble_adblock_TPFilterParser_init(JNIEnv *env, jobject obj, jbyteArray array) { jbyte *bufferPtr = env->GetByteArrayElements(array, NULL); tpParser.deserialize((char *)bufferPtr); }
TEST(parser, test1) { char* data = nullptr; for (size_t i = 0; i < 2; i++) { CTPParser parser; if (0 == i) { parser.addTracker("facebook.com"); parser.addTracker("facebook.de"); } else if (nullptr != data) { CHECK(nullptr != data); parser.deserialize(data); } CHECK(parser.matchesTracker("facebook1.com", "facebook.com")); CHECK(parser.matchesTracker("facebook1.com", "facebook.de")); CHECK(!parser.matchesTracker("facebook.com", "facebook1.com")); CHECK(!parser.matchesTracker("facebook.com", "facebook.com")); CHECK(!parser.matchesTracker("www.facebook.com", "facebook.com")); CHECK(!parser.matchesTracker("facebook.com", "www.facebook.com")); CHECK(!parser.matchesTracker("www.facebook.com", "www.facebook.com")); if (0 == i) { unsigned int totalSize = 0; data = parser.serialize(&totalSize); CHECK(nullptr != data); CHECK(0 != totalSize); } else { delete []data; data = nullptr; } } for (size_t i = 0; i < 2; i++) { CTPParser parser; if (0 == i) { parser.addTracker("facebook.com"); parser.addTracker("facebook.de"); parser.addTracker("google-analytics.com"); } else if (nullptr != data) { CHECK(nullptr != data); parser.deserialize(data); } CHECK(parser.matchesTracker("facebook.com", "subdomain.google-analytics.com")); CHECK(parser.matchesTracker("facebook.com", "google-analytics.com")); CHECK(parser.matchesTracker("facebook1.com", "facebook.com")); CHECK(parser.matchesTracker("facebook.com", "facebook.de")); CHECK(!parser.matchesTracker("facebook.com", "subdomain.google-analytics1.com")); CHECK(!parser.matchesTracker("facebook.com", "facebook.com")); if (0 == i) { unsigned int totalSize = 0; data = parser.serialize(&totalSize); CHECK(nullptr != data); CHECK(0 != totalSize); } else { delete []data; data = nullptr; } } for (size_t i = 0; i < 2; i++) { CTPParser parser; if (0 == i) { parser.addTracker("subdomain.google-analytics.com"); parser.addTracker("facebook.com"); parser.addTracker("facebook.de"); } else if (nullptr != data) { CHECK(nullptr != data); parser.deserialize(data); } CHECK(parser.matchesTracker("facebook.com", "subdomain.google-analytics.com")); CHECK(parser.matchesTracker("facebook1.com", "facebook.com")); CHECK(parser.matchesTracker("facebook.com", "facebook.de")); CHECK(!parser.matchesTracker("facebook.com", "google-analytics.com")); CHECK(!parser.matchesTracker("facebook.com", "facebook.com")); if (0 == i) { unsigned int totalSize = 0; data = parser.serialize(&totalSize); CHECK(nullptr != data); CHECK(0 != totalSize); } else { delete []data; data = nullptr; } } for (size_t i = 0; i < 2; i++) { CTPParser parser; if (0 == i) { parser.addFirstPartyHosts("facebook.com", "facebook.fr,facebook.de"); parser.addFirstPartyHosts("google.com", "2mdn.net,admeld.com"); parser.addFirstPartyHosts("subdomain.google.com", "facebook.fr,facebook.de"); } else if (nullptr != data) { CHECK(nullptr != data); parser.deserialize(data); } char* thirdPartyHostsSubDomain = parser.findFirstPartyHosts("subdomain.google.com"); CHECK(nullptr != thirdPartyHostsSubDomain); char* thirdPartyHosts = parser.findFirstPartyHosts("google.com"); CHECK(nullptr != thirdPartyHosts); if (nullptr != thirdPartyHostsSubDomain && nullptr != thirdPartyHosts) { std::string strThirdPartyHostsSubDomain = thirdPartyHostsSubDomain; std::string strThirdPartyHosts = thirdPartyHosts; CHECK(std::string::npos != strThirdPartyHostsSubDomain.find(strThirdPartyHosts)); } if (nullptr != thirdPartyHosts) { delete []thirdPartyHosts; thirdPartyHosts = nullptr; } if (nullptr != thirdPartyHostsSubDomain) { delete []thirdPartyHostsSubDomain; thirdPartyHostsSubDomain = nullptr; } thirdPartyHosts = parser.findFirstPartyHosts("facebook.com"); CHECK(nullptr != thirdPartyHosts); if (nullptr != thirdPartyHosts) { delete []thirdPartyHosts; thirdPartyHosts = nullptr; } if (0 == i) { unsigned int totalSize = 0; data = parser.serialize(&totalSize); CHECK(nullptr != data); CHECK(0 != totalSize); } else { delete []data; data = nullptr; } } }