summaryrefslogtreecommitdiff
path: root/plugin/business/pangu-http/src/test_edit_element.cpp
blob: 58938464bbae5c447840758282a0304bb997cd30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#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);
}

TEST(EditElement, Libxml_Inside_Remove_Facebook)
{
	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"));
	free(input);
	free(output);
}

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);
}

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

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