OssUtilTest.php 6.9 KB

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