258 lines
8.2 KiB
C++
258 lines
8.2 KiB
C++
#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);
|
||
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);
|
||
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);
|
||
|
||
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);
|
||
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);
|
||
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);
|
||
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);
|
||
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);
|
||
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, 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);
|
||
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 = execute_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 = execute_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 = execute_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();
|
||
}
|
||
|