Skip to content

Commit dc1d7a6

Browse files
committed
merge
2 parents d69d7ca + 30b344b commit dc1d7a6

File tree

8 files changed

+571
-14
lines changed

8 files changed

+571
-14
lines changed
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
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.syncPut(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.syncPut("".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.syncPut("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.syncPut("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.syncPut((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.syncPut(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.syncPut(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.syncPut(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.syncPut(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.syncPut(new byte[0], null, null, null);
177+
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
178+
179+
info = uploadManager.syncPut("", 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.syncPut("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.syncPut("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.syncPut("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.syncPut("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+
}

library/src/androidTest/java/com/qiniu/android/TestFileRecorder.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
import com.qiniu.android.storage.UploadOptions;
1313
import com.qiniu.android.storage.persistent.FileRecorder;
1414
import com.qiniu.android.utils.Etag;
15-
import com.qiniu.android.utils.UrlSafeBase64;
1615

1716
import junit.framework.Assert;
1817

1918
import org.json.JSONObject;
2019

2120
import java.io.File;
2221
import java.io.IOException;
22+
import java.security.MessageDigest;
2323
import java.util.Date;
2424
import java.util.HashMap;
2525
import java.util.Map;
@@ -175,7 +175,7 @@ public void testLastModify() throws IOException {
175175
fr.set(key, data);
176176
byte[] data2 = fr.get(key);
177177

178-
File recoderFile = new File(folder, UrlSafeBase64.encodeToString(key));
178+
File recoderFile = new File(folder, hash(key));
179179

180180
long m1 = recoderFile.lastModified();
181181

@@ -187,10 +187,12 @@ public void testLastModify() throws IOException {
187187
assertEquals(3, data2.length);
188188
assertEquals('8', data2[1]);
189189

190+
// 让记录文件过期,两天
190191
recoderFile.setLastModified(new Date().getTime() - 1000 * 3600 * 48 - 2300);
191192

192193
long m2 = recoderFile.lastModified();
193194

195+
// 过期后,记录数据作废
194196
byte[] data3 = fr.get(key);
195197

196198
assertNull(data3);
@@ -205,4 +207,21 @@ public void testLastModify() throws IOException {
205207
long m4 = recoderFile.lastModified();
206208
assertTrue(m4 > m1);
207209
}
210+
211+
// copy from FileRecorder.
212+
private static String hash(String base) {
213+
try {
214+
MessageDigest digest = MessageDigest.getInstance("SHA-1");
215+
byte[] hash = digest.digest(base.getBytes());
216+
StringBuffer hexString = new StringBuffer();
217+
218+
for (int i = 0; i < hash.length; i++) {
219+
hexString.append(Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1));
220+
}
221+
return hexString.toString();
222+
} catch (Exception ex) {
223+
ex.printStackTrace();
224+
}
225+
return null;
226+
}
208227
}

library/src/main/AndroidManifest.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
<manifest
2-
package="com.qiniu.android"
1+
<manifest package="com.qiniu.android"
32
xmlns:android="http://schemas.android.com/apk/res/android">
43

5-
<uses-permission android:name="android.permission.INTERNET"/>
6-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
7-
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
4+
<uses-permission android:name="android.permission.INTERNET" />
5+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
87

98
<application android:label="@string/app_name"></application>
109

0 commit comments

Comments
 (0)