#include <tfe_utils.h>
#include "pattern_replace.h"

#include <sys/types.h>//fstat
#include <sys/ioctl.h>
#include <string.h>
#include <stdio.h>
#include <gtest/gtest.h>


TEST(PatternReplace, Grouping1)
{
	const char* find="(?<name1>John)|(?<name2>李梅梅)|(?<name3>Jake)";
	const char* replacement="${name1:+Joseph:${name2:+王桃花:Keith}}";
	const char* input="John loves 李梅梅, 李梅梅 loves Jake and Jake doesn't care about John and 李梅梅.";
	__attribute__((unused)) const char* expect="Joseph loves 王桃花, 王桃花 loves Keith and Keith doesn't care about Joseph and 王桃花.";
	char* output=NULL;
	size_t output_sz=0;

	simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
	EXPECT_TRUE(output_sz>0);
//	EXPECT_STREQ(output, expect);
	printf("%s\n", output);
	free(output);
	return;
}
TEST(PatternReplace, Grouping2)
{
	const char* find="(?<=中央)(?<ac1>空调)|(?<=家用)(?<ac2>空调)";
	const char* replacement="${ac1:+Air conditioner:${ac2:+立式空调}}";
	const char* input="中央空调，家用空调，只有第一个空调会被替换。";
	char* output=NULL;
	size_t output_sz=0;

	simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
	EXPECT_TRUE(output_sz>0);
	EXPECT_TRUE(NULL==strstr(output, "中央空调"));
	EXPECT_TRUE(NULL!=strstr(output, "中央Air conditioner"));
	EXPECT_TRUE(NULL!=strstr(output, "家用立式空调"));

//	EXPECT_STREQ(output, expect);
	printf("%s\n", output);
	free(output);
	return;
}


TEST(PatternReplace, Non_Zero_Terminated)
{
	const char* filename="./test_data/facebook_index.html";
	char fn_replaced[256];
	const char* find="添加手机号";
	const char* replacement="Add a Mobile Phone Number";

	FILE* fp=NULL;
	struct stat file_info;	
	stat(filename, &file_info);
	size_t input_sz=file_info.st_size;
	
	fp=fopen(filename,"r");
	ASSERT_FALSE(fp==NULL);
	if(fp==NULL)
	{
		return;
	}
	char* input=(char*)malloc(input_sz);
	fread(input,1,input_sz,fp);
	fclose(fp);

	char* output=NULL;
	size_t output_sz=0;
	simple_replace(find, replacement, input, input_sz, &output, &output_sz, 1);

	EXPECT_TRUE(output_sz>0);
	EXPECT_TRUE(NULL==memmem(output, output_sz, find, strlen(find)));
	EXPECT_TRUE(NULL!=memmem(output, output_sz, replacement, strlen(replacement)));
	snprintf(fn_replaced,sizeof(fn_replaced), "%s.replaced", filename);
	fp=fopen(fn_replaced, "w");
	fwrite(output, 1,output_sz, fp);
	fclose(fp);
	free(output);
	return;
}

TEST(PatternReplace, ASCII)
{
	const char* find="James";
	const char* replacement="John";
	const char* input="James, where are you? Alice is calling you.";
	char* output=NULL;
	size_t output_sz=0;

	simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
	EXPECT_TRUE(output_sz>0);
	EXPECT_TRUE(NULL==strstr(output, find));
	EXPECT_TRUE(NULL!=strstr(output, replacement));

//	printf("%s\n", output);
	free(output);
	return;
}

TEST(PatternReplace, UTF8)
{
	const char* find="视频";
	const char* replacement="短片";
	const char* input="欢迎来到 Facebook开始添加好友吧！他们的视频、照片和帖子都会显示在这里。";
	char* output=NULL;
	size_t output_sz=0;

	simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
	EXPECT_TRUE(output_sz>0);
	EXPECT_TRUE(NULL==strstr(output, find));
	EXPECT_TRUE(NULL!=strstr(output, replacement));

//	printf("%s\n", output);
	free(output);
	return;
}
TEST(PatternReplace, CaseInsensitive)
{
	const char* find="(?i)Abc(?-i)视频";
	const char* replacement="ABC短片";
	const char* input="欢迎来到 Facebook开始添加好友吧！他们的aBc视频、照片和帖子都会显示在这里。";
	char* output=NULL;
	size_t output_sz=0;

	simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
	EXPECT_TRUE(output_sz>0);
	EXPECT_TRUE(NULL==strstr(output, find));
	EXPECT_TRUE(NULL!=strstr(output, replacement));

	printf("%s\n", output);
	free(output);
	return;
}

TEST(PatternReplace, CaseInsensitiveRussian)
{
	const char* find="(?i)САМРУК-КАЗЫНА(?-i)";

	const char* replacement="МММММММММ";
	const char* input="Как мы сообщали, 22 мая прошло заседание Совета по управлению Фондом национального \
		благосостояния \"Самрук-Казына\" под председательством Первого Президента Республики Казахстан – \
		Елбасы Нурсултана Назарбаева. Чуть более года назад была принята новая стратегия управления Фондом \
		на ближайшие десять лет, и председатель правления \"Самрук-Қазына\" Ахметжан Есимов докладывал Н. \
		Назарбаеву о том, как она выполняется. В распоряжении нашей редакции оказались некоторые детали,\
		о которых не сообщалось в СМИ ранее.";
	char* output=NULL;
	size_t output_sz=0;

	simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
	EXPECT_TRUE(output_sz>0);
	EXPECT_TRUE(NULL==strstr(output, find));
	EXPECT_TRUE(NULL!=strstr(output, replacement));

	free(output);
	return;
}

TEST(PatternReplace, QueryAdd)
{
	const char * find = "(?<=\\?|^|&)q=([^&|^#]*)(?=&|$)";
	const char* replacement="q=find";
	const char* input="https://cn.bing.com/search?ei=pQnxXPS-LPSGr7wP3u6usAY&q=test&oq=test&gs_l=psy-ab.3..0i131i67j0l8j0i131.26791.27227..27885...0.0..0.235.683.0j3j1......0....1..gws-wiz.......0i71j0i67.klHdqBPS88k";
	char* output=NULL;
	size_t output_sz=0;

	simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
	EXPECT_TRUE(output_sz>0);
	EXPECT_TRUE(NULL==strstr(output, find));
	EXPECT_TRUE(NULL!=strstr(output, replacement));

	printf("%s\n", output);
	free(output);

	return;
}

TEST(PatternReplace, UrlReplace)
{
	int i=0, n_rule=0;
	char replace_with[TFE_SYMBOL_MAX]={0};
	struct replace_rule *rule;
	char *rewrite_uri=NULL;

	const char *rd_url = "http://www.example.com/query?pageid=12345&policy_id={{tsg_policy_id}}&user_id={{tsg_subscriber_id}}&source_ip={{tsg_client_ip}}";

	if(strstr(rd_url, "tsg_policy_id")==NULL && strstr(rd_url, "tsg_subscriber_id")==NULL && strstr(rd_url, "tsg_client_ip")==NULL)
	{
		return;
	}

	rule = ALLOC(struct replace_rule, 3);
	n_rule=0;
	rule[n_rule].zone = kZoneRequestUri;
	rule[n_rule].find = tfe_strdup("{{tsg_policy_id}}");
	snprintf(replace_with, TFE_SYMBOL_MAX, "%d", 23);
	rule[n_rule].replace_with = tfe_strdup(replace_with);

	n_rule=1;
	memset(replace_with, TFE_SYMBOL_MAX, 0);
	rule[n_rule].zone = kZoneRequestUri;
	rule[n_rule].find = tfe_strdup("{{tsg_subscriber_id}}");
	rule[n_rule].replace_with = tfe_strdup("te&st01");

	n_rule=2;
	memset(replace_with, TFE_SYMBOL_MAX, 0);
	rule[n_rule].zone = kZoneRequestUri;
	rule[n_rule].find = tfe_strdup("{{tsg_client_ip}}");
	rule[n_rule].replace_with = tfe_strdup("192.168.50.71");

	size_t rewrite_uri_sz = execute_replace_rule(rd_url, strlen(rd_url), kZoneRequestUri, rule, n_rule+1, &rewrite_uri, 1);

	for(i=0; i<n_rule+1; i++)
	{
		FREE(&(rule[i].find));
		FREE(&(rule[i].replace_with));
	}
	FREE(&rule);

	if(rewrite_uri_sz > 0 && rewrite_uri != NULL)
	{
		printf("rewrite_uri = %s\n", rewrite_uri);
		free(rewrite_uri);
	}
	else
	{
		printf("rd_url = %s\n", rd_url);
	}
	return;
}

TEST(PatternReplace, QueryDel)
{
	const char * find = "(?<=\\?|^|&)sk=([^&|^#]*)(&|$)";
	const char* replacement="";
	const char* input="https://cn.bing.com/&search?q=find&qs=n&form=QBLH&sp=-1&pq=find&sk=";

	char* output=NULL;
	size_t output_sz=0;

	simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
	EXPECT_TRUE(output_sz>0);
	EXPECT_TRUE(NULL==strstr(output, find));
	EXPECT_TRUE(NULL!=strstr(output, replacement));

	printf("%s\n", output);
	free(output);
	return;
}

TEST(PatternInsert, CSS)
{
	const char* filename="./test_data/index_of__centos.html";
	const char* custom = "h1,h2{color: red;}ul.tabmain a{color: green;}";
	char *input=NULL, *output=NULL;
	size_t output_sz=0, input_sz = 0;

	input = tfe_read_file(filename, &input_sz);
	EXPECT_TRUE(input_sz>0);

    output_sz = insert_string(input, input_sz, NULL, custom, "css", &output);

	EXPECT_TRUE(output_sz>0);
	EXPECT_TRUE(NULL!=strstr(output, custom));
	free(output);
	free(input);
}

TEST(PatternInsert, AfterBody)
{
	const char* filename="./test_data/index_of__centos.html";
	const char* custom = "var now=new Date();var year=now.getYear()+1900;var month=now.getMonth()+1;var date=now.getDate();var day=now.getDay();\
						  var time=\"curtime\"+year+\"year\"+month+\"month\"+date+\"date\"+week;alert(time);";
	char *input=NULL, *output=NULL;
	size_t output_sz=0, input_sz = 0;

	input = tfe_read_file(filename, &input_sz);
	EXPECT_TRUE(input_sz>0);
    output_sz = insert_string(input, input_sz, "after-page-load", custom, "js", &output);

	EXPECT_TRUE(output_sz>0);
	EXPECT_TRUE(NULL!=strstr(output, custom));
	free(input);
	free(output);
}

TEST(PatternInsert, BeforeBody)
{
	const char* filename="./test_data/index_of__centos.html";
	const char* custom = "var now=new Date();var year=now.getYear()+1900;var month=now.getMonth()+1;var date=now.getDate();var day=now.getDay();\
						  var time=\"curtime\"+year+\"year\"+month+\"month\"+date+\"date\"+week;alert(time);";
	char *input=NULL, *output=NULL;
	size_t output_sz=0, input_sz = 0;

	input = tfe_read_file(filename, &input_sz);
	EXPECT_TRUE(input_sz>0);
    output_sz = insert_string(input, input_sz, "before-page-load", custom, "js", &output);

	EXPECT_TRUE(output_sz>0);
	EXPECT_TRUE(NULL!=strstr(output, custom));
	free(input);
	free(output);
}

int main(int argc, char ** argv)
{

	::testing::InitGoogleTest(&argc, argv);	
	return RUN_ALL_TESTS();
}

