|
| 1 | +package com.qiniu.android; |
| 2 | + |
| 3 | +import android.test.InstrumentationTestCase; |
| 4 | +import android.test.suitebuilder.annotation.MediumTest; |
| 5 | +import android.test.suitebuilder.annotation.SmallTest; |
| 6 | + |
| 7 | +import com.qiniu.android.common.FixedZone; |
| 8 | +import com.qiniu.android.common.ServiceAddress; |
| 9 | +import com.qiniu.android.common.Zone; |
| 10 | +import com.qiniu.android.http.ResponseInfo; |
| 11 | +import com.qiniu.android.storage.Configuration; |
| 12 | +import com.qiniu.android.storage.UploadManager; |
| 13 | +import com.qiniu.android.storage.UploadOptions; |
| 14 | +import com.qiniu.android.utils.Etag; |
| 15 | + |
| 16 | +import junit.framework.Assert; |
| 17 | + |
| 18 | +import org.json.JSONObject; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +public class SyncFormUploadTest extends InstrumentationTestCase { |
| 25 | + private UploadManager uploadManager; |
| 26 | + private volatile String key; |
| 27 | + private volatile ResponseInfo info; |
| 28 | + private volatile JSONObject resp; |
| 29 | + |
| 30 | + public void setUp() throws Exception { |
| 31 | + uploadManager = new UploadManager(); |
| 32 | + } |
| 33 | + |
| 34 | + @SmallTest |
| 35 | + public void testHello() throws Throwable { |
| 36 | + final String expectKey = "你好;\"\r\n\r\n\r\n"; |
| 37 | + Map<String, String> params = new HashMap<String, String>(); |
| 38 | + params.put("x:foo", "fooval"); |
| 39 | + final UploadOptions opt = new UploadOptions(params, null, true, null, null); |
| 40 | + byte[] b = "hello".getBytes(); |
| 41 | + info = uploadManager.put(b, expectKey, TestConfig.token, opt); |
| 42 | + resp = info.response; |
| 43 | + |
| 44 | + Assert.assertTrue(info.toString(), info.isOK()); |
| 45 | + Assert.assertNotNull(info.reqId); |
| 46 | + Assert.assertNotNull(resp); |
| 47 | + |
| 48 | + String hash = resp.optString("hash"); |
| 49 | + Assert.assertEquals(hash, Etag.data(b)); |
| 50 | + Assert.assertEquals(expectKey, key = resp.optString("key")); |
| 51 | + } |
| 52 | + |
| 53 | + @SmallTest |
| 54 | + public void test0Data() throws Throwable { |
| 55 | + final String expectKey = "你好;\"\r\n\r\n\r\n"; |
| 56 | + Map<String, String> params = new HashMap<String, String>(); |
| 57 | + params.put("x:foo", "fooval"); |
| 58 | + final UploadOptions opt = new UploadOptions(params, null, true, null, null); |
| 59 | + |
| 60 | + info = uploadManager.put("".getBytes(), expectKey, TestConfig.token, opt); |
| 61 | + resp = info.response; |
| 62 | + |
| 63 | +// key = resp.optString("key"); |
| 64 | + Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode); |
| 65 | +// Assert.assertEquals(info.toString(), expectKey, key); |
| 66 | + Assert.assertFalse(info.toString(), info.isOK()); |
| 67 | + Assert.assertEquals(info.toString(), "", info.reqId); |
| 68 | + Assert.assertNull(resp); |
| 69 | + } |
| 70 | + |
| 71 | + @SmallTest |
| 72 | + public void testNoKey() throws Throwable { |
| 73 | + final String expectKey = null; |
| 74 | + Map<String, String> params = new HashMap<String, String>(); |
| 75 | + params.put("x:foo", "fooval"); |
| 76 | + final UploadOptions opt = new UploadOptions(params, null, true, null, null); |
| 77 | + info = uploadManager.put("hello".getBytes(), expectKey, TestConfig.token, opt); |
| 78 | + |
| 79 | + resp = info.response; |
| 80 | + key = resp.optString("key"); |
| 81 | + Assert.assertTrue(info.toString(), info.isOK()); |
| 82 | + |
| 83 | + Assert.assertNotNull(info.reqId); |
| 84 | + Assert.assertNotNull(resp); |
| 85 | + Assert.assertEquals("Fqr0xh3cxeii2r7eDztILNmuqUNN", resp.optString("key", "")); |
| 86 | + } |
| 87 | + |
| 88 | + @SmallTest |
| 89 | + public void testInvalidToken() throws Throwable { |
| 90 | + final String expectKey = "你好"; |
| 91 | + info = uploadManager.put("hello".getBytes(), expectKey, "invalid", null); |
| 92 | + |
| 93 | + resp = info.response; |
| 94 | +// key = resp.optString("key"); |
| 95 | +// Assert.assertEquals(info.toString(), expectKey, key); |
| 96 | + Assert.assertEquals(info.toString(), ResponseInfo.InvalidToken, info.statusCode); |
| 97 | + Assert.assertNotNull(info.reqId); |
| 98 | + Assert.assertNull(resp); |
| 99 | + } |
| 100 | + |
| 101 | + @SmallTest |
| 102 | + public void testNoData() throws Throwable { |
| 103 | + final String expectKey = "你好"; |
| 104 | + |
| 105 | + info = uploadManager.put((byte[]) null, expectKey, "invalid", null); |
| 106 | + |
| 107 | + resp = info.response; |
| 108 | + Assert.assertEquals(info.toString(), ResponseInfo.InvalidArgument, |
| 109 | + info.statusCode); |
| 110 | + Assert.assertNull(resp); |
| 111 | + } |
| 112 | + |
| 113 | + @SmallTest |
| 114 | + public void testNoToken() throws Throwable { |
| 115 | + final String expectKey = "你好"; |
| 116 | + info = uploadManager.put(new byte[1], expectKey, null, null); |
| 117 | + |
| 118 | + resp = info.response; |
| 119 | + Assert.assertEquals(info.toString(), ResponseInfo.InvalidArgument, info.statusCode); |
| 120 | + Assert.assertNull(resp); |
| 121 | + } |
| 122 | + |
| 123 | + @SmallTest |
| 124 | + public void testEmptyToken() throws Throwable { |
| 125 | + final String expectKey = "你好"; |
| 126 | + info = uploadManager.put(new byte[1], expectKey, "", null); |
| 127 | + |
| 128 | + resp = info.response; |
| 129 | + Assert.assertEquals(info.toString(), ResponseInfo.InvalidArgument, |
| 130 | + info.statusCode); |
| 131 | + Assert.assertNull(resp); |
| 132 | + } |
| 133 | + |
| 134 | + @MediumTest |
| 135 | + public void testFile() throws Throwable { |
| 136 | + final String expectKey = "世/界"; |
| 137 | + final File f = TempFile.createFile(1); |
| 138 | + Map<String, String> params = new HashMap<String, String>(); |
| 139 | + params.put("x:foo", "fooval"); |
| 140 | + final UploadOptions opt = new UploadOptions(params, null, true, null, null); |
| 141 | + info = uploadManager.put(f, expectKey, TestConfig.token, opt); |
| 142 | + |
| 143 | + resp = info.response; |
| 144 | + key = resp.optString("key"); |
| 145 | + Assert.assertEquals(info.toString(), expectKey, key); |
| 146 | + Assert.assertTrue(info.toString(), info.isOK()); |
| 147 | + //上传策略含空格 \"fname\":\" $(fname) \" |
| 148 | + Assert.assertEquals(f.getName(), resp.optString("fname", "res doesn't include the FNAME").trim()); |
| 149 | + Assert.assertNotNull(info.reqId); |
| 150 | + Assert.assertNotNull(resp); |
| 151 | + String hash = resp.getString("hash"); |
| 152 | + Assert.assertEquals(hash, Etag.file(f)); |
| 153 | + TempFile.remove(f); |
| 154 | + } |
| 155 | + |
| 156 | + @MediumTest |
| 157 | + public void test0File() throws Throwable { |
| 158 | + final String expectKey = "世/界"; |
| 159 | + final File f = TempFile.createFile(0); |
| 160 | + Map<String, String> params = new HashMap<String, String>(); |
| 161 | + params.put("x:foo", "fooval"); |
| 162 | + final UploadOptions opt = new UploadOptions(params, null, true, null, null); |
| 163 | + info = uploadManager.put(f, expectKey, TestConfig.token, opt); |
| 164 | + |
| 165 | + resp = info.response; |
| 166 | + Assert.assertEquals(f.toString(), 0, f.length()); |
| 167 | + Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode); |
| 168 | + Assert.assertNull(resp); |
| 169 | + Assert.assertFalse(info.toString(), info.isOK()); |
| 170 | + Assert.assertEquals(info.toString(), "", info.reqId); |
| 171 | + TempFile.remove(f); |
| 172 | + } |
| 173 | + |
| 174 | + @SmallTest |
| 175 | + public void testNoComplete() { |
| 176 | + info = uploadManager.put(new byte[0], null, null, null); |
| 177 | + Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode); |
| 178 | + |
| 179 | + info = uploadManager.put("", null, null, null); |
| 180 | + Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode); |
| 181 | + } |
| 182 | + |
| 183 | + @SmallTest |
| 184 | + public void testIpBack() throws Throwable { |
| 185 | + ServiceAddress s = new ServiceAddress("http://upwelcome.qiniu.com", Zone.zone0.upHost("").backupIps); |
| 186 | + Zone z = new FixedZone(s, Zone.zone0.upHostBackup("")); |
| 187 | + Configuration c = new Configuration.Builder() |
| 188 | + .zone(z) |
| 189 | + .build(); |
| 190 | + |
| 191 | + UploadManager uploadManager2 = new UploadManager(c); |
| 192 | + final String expectKey = "你好;\"\r\n\r\n\r\n"; |
| 193 | + Map<String, String> params = new HashMap<String, String>(); |
| 194 | + params.put("x:foo", "fooval"); |
| 195 | + final UploadOptions opt = new UploadOptions(params, null, true, null, null); |
| 196 | + |
| 197 | + info = uploadManager2.put("hello".getBytes(), expectKey, TestConfig.token, opt); |
| 198 | + |
| 199 | + Assert.assertTrue(info.toString(), info.isOK()); |
| 200 | + Assert.assertNotNull(info.reqId); |
| 201 | + resp = info.response; |
| 202 | + Assert.assertNotNull(resp); |
| 203 | + key = resp.optString("key"); |
| 204 | + Assert.assertEquals(info.toString(), expectKey, key); |
| 205 | + } |
| 206 | + |
| 207 | + @SmallTest |
| 208 | + public void testPortBackup() throws Throwable { |
| 209 | + ServiceAddress s = new ServiceAddress("http://upload.qiniu.com:9999", null); |
| 210 | + Zone z = new FixedZone(s, Zone.zone0.upHostBackup("")); |
| 211 | + Configuration c = new Configuration.Builder() |
| 212 | + .zone(z) |
| 213 | + .build(); |
| 214 | + UploadManager uploadManager2 = new UploadManager(c); |
| 215 | + final String expectKey = "你好;\"\r\n\r\n\r\n"; |
| 216 | + Map<String, String> params = new HashMap<String, String>(); |
| 217 | + params.put("x:foo", "fooval"); |
| 218 | + final UploadOptions opt = new UploadOptions(params, null, true, null, null); |
| 219 | + |
| 220 | + info = uploadManager2.put("hello".getBytes(), expectKey, TestConfig.token, opt); |
| 221 | + |
| 222 | + Assert.assertTrue(info.toString(), info.isOK()); |
| 223 | + Assert.assertNotNull(info.reqId); |
| 224 | + resp = info.response; |
| 225 | + Assert.assertNotNull(resp); |
| 226 | + key = resp.optString("key"); |
| 227 | + Assert.assertEquals(info.toString(), expectKey, key); |
| 228 | + } |
| 229 | + |
| 230 | + @SmallTest |
| 231 | + public void testDnsHijacking() throws Throwable { |
| 232 | + ServiceAddress s = new ServiceAddress("http://uphijacktest.qiniu.com", Zone.zone0.upHost("").backupIps); |
| 233 | + Zone z = new FixedZone(s, Zone.zone0.upHostBackup("")); |
| 234 | + Configuration c = new Configuration.Builder() |
| 235 | + .zone(z) |
| 236 | + .build(); |
| 237 | + UploadManager uploadManager2 = new UploadManager(c); |
| 238 | + final String expectKey = "你好;\"\r\n\r\n\r\n"; |
| 239 | + Map<String, String> params = new HashMap<String, String>(); |
| 240 | + params.put("x:foo", "fooval"); |
| 241 | + final UploadOptions opt = new UploadOptions(params, null, true, null, null); |
| 242 | + |
| 243 | + info = uploadManager2.put("hello".getBytes(), expectKey, TestConfig.token, opt); |
| 244 | + |
| 245 | + resp = info.response; |
| 246 | + Assert.assertTrue(info.toString(), info.isOK()); |
| 247 | + Assert.assertNotNull(info.reqId); |
| 248 | + Assert.assertNotNull(resp); |
| 249 | + } |
| 250 | + |
| 251 | + @SmallTest |
| 252 | + public void testHttps() throws Throwable { |
| 253 | + |
| 254 | + final String expectKey = "你好;\"\r\n\r\n\r\n"; |
| 255 | + Map<String, String> params = new HashMap<String, String>(); |
| 256 | + params.put("x:foo", "fooval"); |
| 257 | + final UploadOptions opt = new UploadOptions(params, null, true, null, null); |
| 258 | + ServiceAddress s = new ServiceAddress("https://up.qbox.me", null); |
| 259 | + Zone z = new FixedZone(s, Zone.zone0.upHostBackup("")); |
| 260 | + Configuration c = new Configuration.Builder() |
| 261 | + .zone(z) |
| 262 | + .build(); |
| 263 | + UploadManager uploadManager2 = new UploadManager(c); |
| 264 | + info = uploadManager2.put("hello".getBytes(), expectKey, TestConfig.token, opt); |
| 265 | + |
| 266 | + resp = info.response; |
| 267 | + key = resp.optString("key"); |
| 268 | + Assert.assertEquals(info.toString(), expectKey, key); |
| 269 | + Assert.assertTrue(info.toString(), info.isOK()); |
| 270 | + Assert.assertNotNull(info.reqId); |
| 271 | + Assert.assertNotNull(resp); |
| 272 | + } |
| 273 | +} |
0 commit comments