File size: 3,280 Bytes
6baed57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "unity/unity.h"
#include <libxml/HTMLparser.h>
#include <libxml/xmlstring.h>
#include <string.h>
#include <stdlib.h>

/* Some libxml2 builds may not expose the prototype publicly; declare it if needed. */
#ifndef LIBXML_TEST_FOR_EXTERN_PROTOTYPE
int htmlIsScriptAttribute(const xmlChar *name);
#endif

void setUp(void) {
    /* Setup code here, or leave empty */
}

void tearDown(void) {
    /* Cleanup code here, or leave empty */
}

void test_htmlIsScriptAttribute_null_returns0(void) {
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute(NULL));
}

void test_htmlIsScriptAttribute_empty_string_returns0(void) {
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)""));
}

void test_htmlIsScriptAttribute_non_on_prefix_returns0(void) {
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)"href"));
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)"class"));
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)"data-test"));
}

void test_htmlIsScriptAttribute_known_script_attributes_return1(void) {
    TEST_ASSERT_EQUAL_INT(1, htmlIsScriptAttribute((const xmlChar *)"onclick"));
    TEST_ASSERT_EQUAL_INT(1, htmlIsScriptAttribute((const xmlChar *)"onload"));
}

void test_htmlIsScriptAttribute_case_sensitivity(void) {
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)"onClick")); /* mixed case */
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)"ONLOAD"));  /* upper case */
}

void test_htmlIsScriptAttribute_exact_match_only(void) {
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)"onclicked")); /* suffix */
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)"onclick "));  /* trailing space */
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)" onload"));   /* leading space */
}

void test_htmlIsScriptAttribute_only_on_is_not_script(void) {
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)"on"));
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)"o"));  /* short prefix */
}

void test_htmlIsScriptAttribute_on_prefix_but_not_known_returns0(void) {
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)"onfoobar")); /* not a real event */
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)"on____not_real____attr"));
}

void test_htmlIsScriptAttribute_long_nonmatch_returns0(void) {
    char buf[2048];
    memset(buf, 'x', sizeof(buf));
    buf[0] = 'o';
    buf[1] = 'n';
    buf[sizeof(buf) - 1] = '\0';
    TEST_ASSERT_EQUAL_INT(0, htmlIsScriptAttribute((const xmlChar *)buf));
}

int main(void) {
    UNITY_BEGIN();
    RUN_TEST(test_htmlIsScriptAttribute_null_returns0);
    RUN_TEST(test_htmlIsScriptAttribute_empty_string_returns0);
    RUN_TEST(test_htmlIsScriptAttribute_non_on_prefix_returns0);
    RUN_TEST(test_htmlIsScriptAttribute_known_script_attributes_return1);
    RUN_TEST(test_htmlIsScriptAttribute_case_sensitivity);
    RUN_TEST(test_htmlIsScriptAttribute_exact_match_only);
    RUN_TEST(test_htmlIsScriptAttribute_only_on_is_not_script);
    RUN_TEST(test_htmlIsScriptAttribute_on_prefix_but_not_known_returns0);
    RUN_TEST(test_htmlIsScriptAttribute_long_nonmatch_returns0);
    return UNITY_END();
}