1 /*
2  * entities_test.c
3  *
4  * Copyright (c) 2012-2021
5  *
6  * Source code released under the GPL version 2
7  */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "entities.h"
11 
12 char progname[] = "entities_test";
13 char version[] = "0.1";
14 
15 int
test_decimal_entities1(void)16 test_decimal_entities1( void )
17 {
18 	unsigned int i, answer, pos_in;
19 	int failed = 0, err, unicode = 0;
20 	char buf[512];
21 	for ( i=0; i<10000; ++i ) {
22 		pos_in = 0;
23 		err = 0;
24 		sprintf( buf, "&#%u;*", i );
25 		answer = decode_entity( buf, &pos_in, &unicode, &err );
26 		if ( err ) {
27 			failed = 1;
28 			printf("%s: Error test_decimal_entities received "
29 				"error, sent '%s' returned %u\n",
30 				progname, buf, answer );
31 		}
32 		if ( answer!=i ) {
33 			failed = 1;
34 			printf("%s: Error test_decimal_entities mismatch, "
35 				"sent '%s' returned %u\n",
36 				progname, buf, answer );
37 		}
38 		if ( buf[pos_in]!='*' ) {
39 			failed = 1;
40 			printf("%s: Error test_decimal_entities bad ending pos,"
41 				"sent '%s' returned pointer to '%s'\n",
42 				progname, buf, &(buf[pos_in]) );
43 		}
44 	}
45 	return failed;
46 }
47 
48 int
test_decimal_entities2(void)49 test_decimal_entities2( void )
50 {
51 	unsigned int i, answer, pos_in;
52 	int failed = 0, err, unicode = 0;
53 	char buf[512];
54 	for ( i=0; i<10000; ++i ) {
55 		pos_in = 1;
56 		err = 0;
57 		sprintf( buf, "&#%u;*", i );
58 		answer = decode_entity( buf, &pos_in, &unicode, &err );
59 		if ( !err ) {
60 			failed = 1;
61 			printf("%s: Error test_decimal_entities should have "
62 				"received error, sent '%s' returned %u\n",
63 				progname, &(buf[1]), answer );
64 		}
65 	}
66 	for ( i=0; i<1000; ++i ) {
67 		pos_in = 0;
68 		err = 0;
69 		sprintf( buf, "&#%u ;", i );
70 		answer = decode_entity( buf, &pos_in, &unicode, &err );
71 		if ( !err ) {
72 			failed = 1;
73 			printf("%s: Error test_decimal_entities should have "
74 				"received error, sent '%s' returned %u\n",
75 				progname, buf, answer );
76 		}
77 	}
78 	return failed;
79 }
80 
81 int
test_hex_entities(void)82 test_hex_entities( void )
83 {
84 	unsigned int i, answer, pos_in;
85 	int failed = 0, err, unicode = 0;
86 	char buf[512];
87 	for ( i=0; i<10000; ++i ) {
88 		pos_in = 0;
89 		err = 0;
90 		sprintf( buf, "&#x%x;*", i );
91 		answer = decode_entity( buf, &pos_in, &unicode, &err );
92 		if ( err ) {
93 			failed = 1;
94 			printf("%s: Error test_hex_entities received error, "
95 				"sent '%s' = %u returned %u\n",
96 				progname, buf, i, answer );
97 		}
98 		if ( answer!=i ) {
99 			failed = 1;
100 			printf("%s: Error test_hex_entities mismatch, "
101 				"sent '%s' = %u returned %u\n",
102 				progname, buf, i, answer );
103 		}
104 		if ( buf[pos_in]!='*' ) {
105 			failed = 1;
106 			printf("%s: Error test_decimal_entities bad ending pos,"
107 				"sent '%s' returned pointer to '%s'\n",
108 				progname, buf, &(buf[pos_in]) );
109 		}
110 	}
111 	return failed;
112 }
113 
114 int
main(int argc,char * argv[])115 main( int argc, char *argv[] )
116 {
117 	int failed = 0;
118 	failed += test_decimal_entities1();
119 	failed += test_decimal_entities2();
120 	failed += test_hex_entities();
121 	if ( !failed ) {
122 		printf( "%s: PASSED\n", progname );
123 		return EXIT_SUCCESS;
124 	} else {
125 		printf( "%s: FAILED\n", progname );
126 		return EXIT_FAILURE;
127 	}
128 }
129 
130