OssUtilTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Core\OssException;
  4. use OSS\Core\OssUtil;
  5. class OssUtilTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testIsChinese()
  8. {
  9. $this->assertEquals(OssUtil::chkChinese("hello,world"), 0);
  10. $str = '你好,这里是卖咖啡!';
  11. $strGBK = OssUtil::encodePath($str);
  12. $this->assertEquals(OssUtil::chkChinese($str), 1);
  13. $this->assertEquals(OssUtil::chkChinese($strGBK), 1);
  14. }
  15. public function testIsGB2312()
  16. {
  17. $str = '你好,这里是卖咖啡!';
  18. $this->assertFalse(OssUtil::isGb2312($str));
  19. }
  20. public function testCheckChar()
  21. {
  22. $str = '你好,这里是卖咖啡!';
  23. $this->assertFalse(OssUtil::checkChar($str));
  24. $this->assertTrue(OssUtil::checkChar(iconv("UTF-8", "GB2312//IGNORE", $str)));
  25. }
  26. public function testIsIpFormat()
  27. {
  28. $this->assertTrue(OssUtil::isIPFormat("10.101.160.147"));
  29. $this->assertTrue(OssUtil::isIPFormat("12.12.12.34"));
  30. $this->assertTrue(OssUtil::isIPFormat("12.12.12.12"));
  31. $this->assertTrue(OssUtil::isIPFormat("255.255.255.255"));
  32. $this->assertTrue(OssUtil::isIPFormat("0.1.1.1"));
  33. $this->assertFalse(OssUtil::isIPFormat("0.1.1.x"));
  34. $this->assertFalse(OssUtil::isIPFormat("0.1.1.256"));
  35. $this->assertFalse(OssUtil::isIPFormat("256.1.1.1"));
  36. $this->assertFalse(OssUtil::isIPFormat("0.1.1.0.1"));
  37. $this->assertTrue(OssUtil::isIPFormat("10.10.10.10:123"));
  38. }
  39. public function testToQueryString()
  40. {
  41. $option = array("a" => "b");
  42. $this->assertEquals('a=b', OssUtil::toQueryString($option));
  43. }
  44. public function testSReplace()
  45. {
  46. $str = "<>&'\"";
  47. $this->assertEquals("&amp;lt;&amp;gt;&amp;&apos;&quot;", OssUtil::sReplace($str));
  48. }
  49. public function testCheckChinese()
  50. {
  51. $str = '你好,这里是卖咖啡!';
  52. $this->assertEquals(OssUtil::chkChinese($str), 1);
  53. if (OssUtil::isWin()) {
  54. $strGB = OssUtil::encodePath($str);
  55. $this->assertEquals($str, iconv("GB2312", "UTF-8", $strGB));
  56. }
  57. }
  58. public function testValidateOption()
  59. {
  60. $option = 'string';
  61. try {
  62. OssUtil::validateOptions($option);
  63. $this->assertFalse(true);
  64. } catch (OssException $e) {
  65. $this->assertEquals("string:option must be array", $e->getMessage());
  66. }
  67. $option = null;
  68. try {
  69. OssUtil::validateOptions($option);
  70. $this->assertTrue(true);
  71. } catch (OssException $e) {
  72. $this->assertFalse(true);
  73. }
  74. }
  75. public function testCreateDeleteObjectsXmlBody()
  76. {
  77. $xml = <<<BBBB
  78. <?xml version="1.0" encoding="utf-8"?><Delete><Quiet>true</Quiet><Object><Key>obj1</Key></Object></Delete>
  79. BBBB;
  80. $a = array('obj1');
  81. $this->assertEquals($xml, $this->cleanXml(OssUtil::createDeleteObjectsXmlBody($a, 'true')));
  82. }
  83. public function testCreateCompleteMultipartUploadXmlBody()
  84. {
  85. $xml = <<<BBBB
  86. <?xml version="1.0" encoding="utf-8"?><CompleteMultipartUpload><Part><PartNumber>2</PartNumber><ETag>xx</ETag></Part></CompleteMultipartUpload>
  87. BBBB;
  88. $a = array(array("PartNumber" => 2, "ETag" => "xx"));
  89. $this->assertEquals($this->cleanXml(OssUtil::createCompleteMultipartUploadXmlBody($a)), $xml);
  90. }
  91. public function testValidateBucket()
  92. {
  93. $this->assertTrue(OssUtil::validateBucket("xxx"));
  94. $this->assertFalse(OssUtil::validateBucket("XXXqwe123"));
  95. $this->assertFalse(OssUtil::validateBucket("XX"));
  96. $this->assertFalse(OssUtil::validateBucket("/X"));
  97. $this->assertFalse(OssUtil::validateBucket(""));
  98. }
  99. public function testValidateObject()
  100. {
  101. $this->assertTrue(OssUtil::validateObject("xxx"));
  102. $this->assertTrue(OssUtil::validateObject("xxx23"));
  103. $this->assertTrue(OssUtil::validateObject("12321-xxx"));
  104. $this->assertTrue(OssUtil::validateObject("x"));
  105. $this->assertFalse(OssUtil::validateObject("/aa"));
  106. $this->assertFalse(OssUtil::validateObject("\\aa"));
  107. $this->assertFalse(OssUtil::validateObject(""));
  108. }
  109. public function testStartWith()
  110. {
  111. $this->assertTrue(OssUtil::startsWith("xxab", "xx"), true);
  112. }
  113. public function testReadDir()
  114. {
  115. $list = OssUtil::readDir("./src", ".|..|.svn|.git", true);
  116. $this->assertNotNull($list);
  117. }
  118. public function testIsWin()
  119. {
  120. //$this->assertTrue(OssUtil::isWin());
  121. }
  122. public function testGetMd5SumForFile()
  123. {
  124. $this->assertEquals(OssUtil::getMd5SumForFile(__FILE__, 0, filesize(__FILE__) - 1), base64_encode(md5(file_get_contents(__FILE__), true)));
  125. }
  126. public function testGenerateFile()
  127. {
  128. $path = __DIR__ . DIRECTORY_SEPARATOR . "generatedFile.txt";
  129. OssUtil::generateFile($path, 1024 * 1024);
  130. $this->assertEquals(filesize($path), 1024 * 1024);
  131. unlink($path);
  132. }
  133. public function testThrowOssExceptionWithMessageIfEmpty()
  134. {
  135. $null = null;
  136. try {
  137. OssUtil::throwOssExceptionWithMessageIfEmpty($null, "xx");
  138. $this->assertTrue(false);
  139. } catch (OssException $e) {
  140. $this->assertEquals('xx', $e->getMessage());
  141. }
  142. }
  143. public function testThrowOssExceptionWithMessageIfEmpty2()
  144. {
  145. $null = "";
  146. try {
  147. OssUtil::throwOssExceptionWithMessageIfEmpty($null, "xx");
  148. $this->assertTrue(false);
  149. } catch (OssException $e) {
  150. $this->assertEquals('xx', $e->getMessage());
  151. }
  152. }
  153. public function testValidContent()
  154. {
  155. $null = "";
  156. try {
  157. OssUtil::validateContent($null);
  158. $this->assertTrue(false);
  159. } catch (OssException $e) {
  160. $this->assertEquals('http body content is invalid', $e->getMessage());
  161. }
  162. $notnull = "x";
  163. try {
  164. OssUtil::validateContent($notnull);
  165. $this->assertTrue(true);
  166. } catch (OssException $e) {
  167. $this->assertEquals('http body content is invalid', $e->getMessage());
  168. }
  169. }
  170. public function testThrowOssExceptionWithMessageIfEmpty3()
  171. {
  172. $null = "xx";
  173. try {
  174. OssUtil::throwOssExceptionWithMessageIfEmpty($null, "xx");
  175. $this->assertTrue(True);
  176. } catch (OssException $e) {
  177. $this->assertTrue(false);
  178. }
  179. }
  180. private function cleanXml($xml)
  181. {
  182. return str_replace("\n", "", str_replace("\r", "", $xml));
  183. }
  184. }