3 * Tests, without pHPUnit by now
7 require_once('include/text.php');
9 class AntiXSSTest extends PHPUnit_Framework_TestCase {
14 public function testEscapeTags() {
15 $invalidstring='<submit type="button" onclick="alert(\'failed!\');" />';
17 $validstring=notags($invalidstring);
18 $escapedString=escape_tags($invalidstring);
20 $this->assertEquals('[submit type="button" onclick="alert(\'failed!\');" /]', $validstring);
21 $this->assertEquals("<submit type="button" onclick="alert('failed!');" />", $escapedString);
25 *autonames should be random, even length
27 public function testAutonameEven() {
28 $autoname1=autoname(10);
29 $autoname2=autoname(10);
31 $this->assertNotEquals($autoname1, $autoname2);
35 *autonames should be random, odd length
37 public function testAutonameOdd() {
38 $autoname1=autoname(9);
39 $autoname2=autoname(9);
41 $this->assertNotEquals($autoname1, $autoname2);
45 * try to fail autonames
47 public function testAutonameNoLength() {
48 $autoname1=autoname(0);
49 $this->assertEquals(0, count($autoname1));
52 public function testAutonameNegativeLength() {
53 $autoname1=autoname(-23);
54 $this->assertEquals(0, count($autoname1));
57 // public function testAutonameMaxLength() {
58 // $autoname2=autoname(PHP_INT_MAX);
59 // $this->assertEquals(PHP_INT_MAX, count($autoname2));
62 public function testAutonameLength1() {
63 $autoname3=autoname(1);
64 $this->assertEquals(1, count($autoname3));
70 public function testXmlify() {
71 $text="<tag>I want to break\n this!11!<?hard?></tag>";
72 $xml=xmlify($text); //test whether it actually may be part of a xml document
73 $retext=unxmlify($text);
75 $this->assertEquals($text, $retext);
79 * test hex2bin and reverse
82 public function testHex2Bin() {
83 $this->assertEquals(-3, hex2bin(bin2hex(-3)));
84 $this->assertEquals(0, hex2bin(bin2hex(0)));
85 $this->assertEquals(12, hex2bin(bin2hex(12)));
86 $this->assertEquals(PHP_INT_MAX, hex2bin(bin2hex(PHP_INT_MAX)));
92 public function testExpandAclNormal() {
94 $this->assertEquals(array(1, 2, 3), expand_acl($text));
97 public function testExpandAclBigNumber() {
98 $text="<1><279012><15>";
99 $this->assertEquals(array(1, 279012, 15), expand_acl($text));
102 public function testExpandAclString() {
103 $text="<1><279012><tt>"; //maybe that's invalid
104 $this->assertEquals(array(1, 279012, 'tt'), expand_acl($text));
107 public function testExpandAclSpace() {
108 $text="<1><279 012><32>"; //maybe that's invalid
109 $this->assertEquals(array(1, "279 012", "32"), expand_acl($text));
112 public function testExpandAclEmpty() {
113 $text=""; //maybe that's invalid
114 $this->assertEquals(array(), expand_acl($text));
117 public function testExpandAclNoBrackets() {
118 $text="According to documentation, that's invalid. "; //should be invalid
119 $this->assertEquals(array(), expand_acl($text));
122 public function testExpandAclJustOneBracket1() {
123 $text="<Another invalid string"; //should be invalid
124 $this->assertEquals(array(), expand_acl($text));
127 public function testExpandAclJustOneBracket2() {
128 $text="Another invalid> string"; //should be invalid
129 $this->assertEquals(array(), expand_acl($text));
132 public function testExpandAclCloseOnly() {
133 $text="Another> invalid> string>"; //should be invalid
134 $this->assertEquals(array(), expand_acl($text));
137 public function testExpandAclOpenOnly() {
138 $text="<Another< invalid string<"; //should be invalid
139 $this->assertEquals(array(), expand_acl($text));
142 public function testExpandAclNoMatching1() {
143 $text="<Another<> invalid <string>"; //should be invalid
144 $this->assertEquals(array(), expand_acl($text));
147 public function testExpandAclNoMatching2() {
149 $this->assertEquals(array(), expand_acl($text));
153 * test attribute contains
155 public function testAttributeContains1() {
156 $testAttr="class1 notclass2 class3";
157 $this->assertTrue(attribute_contains($testAttr, "class3"));
158 $this->assertFalse(attribute_contains($testAttr, "class2"));
162 * test attribute contains
164 public function testAttributeContains2() {
165 $testAttr="class1 not-class2 class3";
166 $this->assertTrue(attribute_contains($testAttr, "class3"));
167 $this->assertFalse(attribute_contains($testAttr, "class2"));
170 public function testAttributeContainsEmpty() {
172 $this->assertFalse(attribute_contains($testAttr, "class2"));
175 public function testAttributeContainsSpecialChars() {
176 $testAttr="--... %\$รค() /(=?}";
177 $this->assertFalse(attribute_contains($testAttr, "class2"));
183 public function testGetTags() {
184 $text="hi @Mike, I'm just writing #test_cases, "
185 ." so @somebody@friendica.com may change #things. Of course I "
186 ."look for a lot of #pitfalls, like #tags at the end of a sentence "
187 ."@comment. I hope noone forgets about @fullstops.because that might"
188 ." break #things. @Mike@campino@friendica.eu is also #nice, isn't it? "
189 ."Now, add a @first_last tag. ";
190 //check whether this are all variants (no, auto-stuff is missing).
192 $tags=get_tags($text);
194 $this->assertEquals("@Mike", $tags[0]);
195 $this->assertEquals("#test_cases", $tags[1]);
196 $this->assertEquals("@somebody@friendica.com", $tags[2]);
197 $this->assertEquals("#things", $tags[3]);
198 $this->assertEquals("#pitfalls", $tags[4]);
199 $this->assertEquals("#tags", $tags[5]);
200 $this->assertEquals("@comment", $tags[6]);
201 $this->assertEquals("@fullstops", $tags[7]);
202 $this->assertEquals("#things", $tags[8]);
203 $this->assertEquals("@Mike", $tags[9]);
204 $this->assertEquals("@campino@friendica.eu", $tags[10]);
205 $this->assertEquals("#nice", $tags[11]);
206 $this->assertEquals("@first_last", $tags[12]);
209 public function testGetTagsEmpty() {
211 $this->assertEquals(0, count($tags));
213 //function qp, quick and dirty??
215 //get_contact_block, bis Zeile 538