2021-12-17 16:39:49 +08:00
# include <tfe_utils.h>
# include "edit_element.h"
# include <sys/types.h>
# include <sys/ioctl.h>
# include <string.h>
# include <stdio.h>
# include <gtest/gtest.h>
TEST ( EditElement , Libxml_Whole_Remove_Index01 )
{
char * output = NULL ;
size_t output_sz = 0 ;
const char * input = " <html> \n \n <head> \n <title>index</title> \n </head> \n \n <body> \n <p>body</p> \n <p>hello world</p> \n <a href= \" edit.html \" >跳转</a> \n <script onerror= \" mimgError() \" src= \" https://mimg.127.net/p/freemail/lib/polyfill/es5-polyfill.js \" ></script> \n </body> \n \n </html> \n " ;
const char * user_region = " { \" rules \" :[{ \" anchor_element \" :{ \" search_scope \" : \" whole_file \" , \" contained_keyword \" : \" hello world \" }, \
\ " target_element \" :{ \" target_distance_from_matching \" :0, \" element_treatment \" : \" remove \" }}]} " ;
simple_edit_element ( user_region , input , strlen ( input ) , & output , & output_sz , 0 ) ;
EXPECT_TRUE ( output_sz > 0 ) ;
printf ( " output = %s \n " , output ) ;
EXPECT_TRUE ( NULL = = strstr ( output , " hello world " ) ) ;
free ( output ) ;
}
TEST ( EditElement , Libxml_Whole_Mark_Index01 )
{
char * output = NULL ;
size_t output_sz = 0 ;
const char * input = " <html> \n \n <head> \n <title>index</title> \n </head> \n \n <body> \n <p>body</p> \n <p>hello world</p> \n <a href= \" edit.html \" >跳转</a> \n <script onerror= \" mimgError() \" src= \" https://mimg.127.net/p/freemail/lib/polyfill/es5-polyfill.js \" ></script> \n </body> \n \n </html> \n " ;
const char * user_region = " { \" rules \" :[{ \" anchor_element \" :{ \" search_scope \" : \" whole_file \" , \" contained_keyword \" : \" hello world \" }, \
\ " target_element \" :{ \" target_distance_from_matching \" :0, \" element_treatment \" : \" mark \" }}]} " ;
simple_edit_element ( user_region , input , strlen ( input ) , & output , & output_sz , 0 ) ;
EXPECT_TRUE ( output_sz > 0 ) ;
const char * expect_output = " <html need_check= \" true \" > \n \n <head><meta http-equiv= \" Content-Type \" content= \" text/html; charset=UTF-8 \" > \n <title>index</title> \n </head> \n \n <body> \n <p>body</p> \n <p need_filter= \" true \" >hello world</p> \n <a href= \" edit.html \" >跳转</a> \n <script onerror= \" mimgError() \" src= \" https://mimg.127.net/p/freemail/lib/polyfill/es5-polyfill.js \" ></script> \n </body> \n \n </html> \n " ;
printf ( " output = %s \n " , output ) ;
EXPECT_TRUE ( 0 = = strcmp ( output , expect_output ) ) ;
free ( output ) ;
}
TEST ( EditElement , Libxml_Inside_Remove_Index01 )
{
char * output = NULL ;
size_t output_sz = 0 ;
const char * input = " <html> \n \n <head> \n <title>index</title> \n </head> \n \n <body> \n <p>body</p> \n <p class= \" LC20lb \" >hello world</p> \n <a href= \" edit.html \" > \
跳 转 < / a > \ n < script onerror = \ " mimgError() \" src= \" https://mimg.127.net/p/freemail/lib/polyfill/es5-polyfill.js \" ></script> \n </body> \n \n </html> \n " ;
const char * user_region = " { \" rules \" :[{ \" anchor_element \" :{ \" search_scope \" : \" inside_element \" , \" start_indicator \" : \" LC20lb \" , \" contained_keyword \" : \" hello world \" }, \
\ " target_element \" :{ \" target_distance_from_matching \" :0, \" element_treatment \" : \" remove \" }}]} " ;
simple_edit_element ( user_region , input , strlen ( input ) , & output , & output_sz , 0 ) ;
EXPECT_TRUE ( output_sz > 0 ) ;
printf ( " output = %s \n " , output ) ;
EXPECT_TRUE ( NULL = = strstr ( output , " hello world " ) ) ;
free ( output ) ;
}
TEST ( EditElement , Cjson_Whole_Remove_Simple )
{
char * output = NULL ;
size_t output_sz = 0 ;
const char * input = " { \" testkey \" : \" value \" , \" verifyList \" :[{ \" policyType \" : \" tsg_security \" , \" verifySession \" :{ \" attributes \" :[{ \" attributeType \" : \" ip \" , \" attributeValue \" :{ \" ip \" : \" 192.168.50.37 \" }},{ \" attributeType \" : \" ip \" , \" attributeValue \" :{ \" ip \" : \" 2.5.6.7 \" }},{ \" attributeType \" : \" ip \" , \" attributeValue \" :{ \" ip \" : \" 192.168.50.37 \" }}]}}]} " ;
const char * user_region = " { \" rules \" :[{ \" anchor_element \" :{ \" search_scope \" : \" whole_file \" , \" contained_keyword \" : \" 192.168.50.37 \" }, \
\ " target_element \" :{ \" target_distance_from_matching \" :2, \" element_treatment \" : \" remove \" }}]} " ;
simple_edit_element ( user_region , input , strlen ( input ) , & output , & output_sz , 0 ) ;
EXPECT_TRUE ( output_sz > 0 ) ;
printf ( " output = %s \n " , output ) ;
EXPECT_TRUE ( NULL = = strstr ( output , " 192.168.50.37 " ) ) ;
free ( output ) ;
}
TEST ( EditElement , Cjson_Whole_mark_Simple )
{
char * output = NULL ;
size_t output_sz = 0 ;
const char * input = " { \" testkey \" : \" value \" , \" verifyList \" :[{ \" policyType \" : \" tsg_security \" , \" verifySession \" :{ \" attributes \" :[{ \" attributeType \" : \" ip \" , \" attributeValue \" :{ \" ip \" : \" 192.168.50.37 \" }},{ \" attributeType \" : \" ip \" , \" attributeValue \" :{ \" ip \" : \" 2.5.6.7 \" }},{ \" attributeType \" : \" ip \" , \" attributeValue \" :{ \" ip \" : \" 192.168.50.37 \" }}]}}]} " ;
const char * user_region = " { \" rules \" :[{ \" anchor_element \" :{ \" search_scope \" : \" whole_file \" , \" contained_keyword \" : \" 192.168.50.37 \" }, \
\ " target_element \" :{ \" target_distance_from_matching \" :2, \" element_treatment \" : \" mark \" }}]} " ;
simple_edit_element ( user_region , input , strlen ( input ) , & output , & output_sz , 0 ) ;
EXPECT_TRUE ( output_sz > 0 ) ;
const char * expect_output = " { \" testkey \" : \" value \" , \" verifyList \" :[{ \" policyType \" : \" tsg_security \" , \" verifySession \" :{ \" attributes \" :[{ \" attributeType \" : \" ip \" , \" attributeValue \" :{ \" ip \" : \" 192.168.50.37 \" }, \" need_filter \" :true},{ \" attributeType \" : \" ip \" , \" attributeValue \" :{ \" ip \" : \" 2.5.6.7 \" }},{ \" attributeType \" : \" ip \" , \" attributeValue \" :{ \" ip \" : \" 192.168.50.37 \" }, \" need_filter \" :true}]}}], \" need_check \" :true} " ;
printf ( " output = %s \n " , output ) ;
EXPECT_TRUE ( 0 = = strcmp ( output , expect_output ) ) ;
free ( output ) ;
}
2022-02-11 09:57:33 +08:00
TEST ( EditElement , Libxml_Inside_Remove_Facebook )
2021-12-17 16:39:49 +08:00
{
char * output = NULL ;
size_t output_sz = 0 , input_len = 0 ;
const char * filename = " ./test_data/facebook_index.html " ;
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 ) ;
const char * user_region = " { \" rules \" :[{ \" anchor_element \" :{ \" search_scope \" : \" inside_element \" , \" start_indicator \" : \" _2t-a _4pmj _2t-d \" , \" contained_keyword \" : \" Facebook \" }, \
\ " target_element \" :{ \" target_distance_from_matching \" :5, \" element_treatment \" : \" remove \" }}]} " ;
simple_edit_element ( user_region , input , input_len , & output , & output_sz , 0 ) ;
EXPECT_TRUE ( output_sz > 0 ) ;
EXPECT_TRUE ( NULL = = strstr ( output , " _2t-a _4pmj _2t-d " ) ) ;
2022-02-11 09:57:33 +08:00
free ( input ) ;
2021-12-17 16:39:49 +08:00
free ( output ) ;
}
2022-03-11 10:26:56 +08:00
TEST ( EditElement , Libxml_Whole_Remove_Google_Test )
{
char * output = NULL ;
size_t output_sz = 0 , input_len = 0 ;
const char * filename = " ./test_data/google_search_gtest_cnblog.html " ;
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 ) ;
const char * user_region = " { \" rules \" :[{ \" anchor_element \" :{ \" search_scope \" : \" whole_file \" , \" start_indicator \" : \" g \" , \" contained_keyword \" : \" thrift-service-framework/src/test/java/com/cnblogs/yjmyzz at master \" }, \
\ " target_element \" :{ \" target_distance_from_matching \" :4, \" element_treatment \" : \" remove \" }}]} " ;
simple_edit_element ( user_region , input , input_len , & output , & output_sz , 0 ) ;
EXPECT_TRUE ( output_sz > 0 ) ;
EXPECT_TRUE ( NULL = = strstr ( output , " thrift-service-framework/src/test/java/com/cnblogs/yjmyzz at master " ) ) ;
free ( input ) ;
free ( output ) ;
}
TEST ( EditElement , Cjson_Inside_Mark_NO_Match )
{
char * output = NULL ;
size_t output_sz = 0 ;
const char * input = " { \" A \" : \" B \" , \" C \" :[ \" D \" , \" E \" ], \" F \" :{ \" G \" :{ \" H \" : \" I \" }}, \" J \" :{ \" K \" :[{ \" L \" : \" M \" }], \" N \" : \" Q \" }} \n \n { \" data \" :{ \" user \" :{ \" id \" : \" 1 \" , \" units \" : \
{ \ " edges \" :[{ \" eligible \" :true, \" story \" :{ \" message \" :{ \" text \" : \" Firefox 3.6 with System \\ uff0c \\ u7f16 \\ u8f91 \" }}}]}}}} " ;
const char * user_region = " { \" rules \" :[{ \" anchor_element \" :{ \" search_scope \" : \" inside_element \" , \" start_indicator \" : \" error_message \" , \" contained_keyword \" : \" Firefox 3.6 with System \" }, \
\ " target_element \" :{ \" target_distance_from_matching \" :1, \" element_treatment \" : \" mark \" }}]} " ;
simple_edit_element ( user_region , input , strlen ( input ) , & output , & output_sz , 0 ) ;
EXPECT_TRUE ( output_sz = = 0 ) ;
free ( output ) ;
}
TEST ( EditElement , Cjson_Inside_Mark_Match )
{
char * output = NULL ;
size_t output_sz = 0 ;
const char * input = " { \" A \" : \" B \" , \" C \" :[ \" D \" , \" E \" ], \" F \" :{ \" G \" :{ \" H \" : \" I \" }}, \" J \" :{ \" K \" :[{ \" L \" : \" M \" }], \" N \" : \" Q \" }} \n \n { \" data \" :{ \" user \" :{ \" id \" : \" 1 \" , \" units \" : \
{ \ " edges \" :[{ \" eligible \" :true, \" story \" :{ \" message \" :{ \" text \" : \" Firefox 3.6 with System \\ uff0c \\ u7f16 \\ u8f91 \" }}}]}}}} " ;
const char * user_region = " { \" rules \" :[{ \" anchor_element \" :{ \" search_scope \" : \" inside_element \" , \" start_indicator \" : \" message \" , \" contained_keyword \" : \" Firefox 3.6 with System \" }, \
\ " target_element \" :{ \" target_distance_from_matching \" :1, \" element_treatment \" : \" mark \" }}]} " ;
simple_edit_element ( user_region , input , strlen ( input ) , & output , & output_sz , 0 ) ;
EXPECT_TRUE ( output_sz > 0 ) ;
EXPECT_TRUE ( NULL ! = strstr ( output , " need_filter " ) ) ;
EXPECT_TRUE ( NULL ! = strstr ( output , " need_check " ) ) ;
free ( output ) ;
}
TEST ( EditElement , LibXML_Inside_Mark_Match )
{
char * output = NULL ;
size_t output_sz = 0 ;
const char * input = " <!DOCTYPE html> \n <!-- saved from url=(0038)https://www.facebook.com --> \n <html id= \" facebook \" > \n \
< head > \ n < title > Facebook < / title > \ n < head > \ n < body > \ n < div class = \ " f530mmz5 \ " ><div class= \" c1et5uql \" >Firefox 3.6 with System</div></div> \n </body> " ;
const char * user_region = " { \" rules \" :[{ \" anchor_element \" :{ \" search_scope \" : \" inside_element \" , \" start_indicator \" : \" f530mmz5 \" , \" contained_keyword \" : \" Firefox 3.6 with System \" }, \
\ " target_element \" :{ \" target_distance_from_matching \" :1, \" element_treatment \" : \" mark \" }}]} " ;
simple_edit_element ( user_region , input , strlen ( input ) , & output , & output_sz , 0 ) ;
EXPECT_TRUE ( output_sz > 0 ) ;
EXPECT_TRUE ( NULL ! = strstr ( output , " need_filter " ) ) ;
EXPECT_TRUE ( NULL ! = strstr ( output , " need_check " ) ) ;
free ( output ) ;
}
2021-12-17 16:39:49 +08:00
int main ( int argc , char * * argv )
{
: : testing : : InitGoogleTest ( & argc , argv ) ;
return RUN_ALL_TESTS ( ) ;
}