Skip to content

Commit 1b6141a

Browse files
committed
dns hijacking test
1 parent 5cc405e commit 1b6141a

File tree

5 files changed

+104
-6
lines changed

5 files changed

+104
-6
lines changed

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
325325

326326
@SmallTest
327327
public void testPortBackup() throws Throwable {
328-
329328
Configuration c = new Configuration.Builder()
330329
.zone(new Zone("upload.qiniu.com", Zone.zone0.upHostBackup, Zone.zone0.upIp))
331330
.upPort(9999)
@@ -347,6 +346,49 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
347346
}, opt);
348347

349348

349+
try {
350+
signal.await(120, TimeUnit.SECONDS); // wait for callback
351+
} catch (InterruptedException e) {
352+
e.printStackTrace();
353+
}
354+
// 尝试获取info信息。
355+
// key == null : 没进入 complete ? 什么导致的?
356+
if (!expectKey.equals(key)) {
357+
//此处通不过, travis 会打印信息
358+
Assert.assertEquals("", info);
359+
}
360+
if (info == null || !info.isOK()) {
361+
//此处通不过, travis 会打印信息
362+
Assert.assertEquals("", info);
363+
}
364+
Assert.assertEquals(expectKey, key);
365+
Assert.assertTrue(info.isOK());
366+
Assert.assertNotNull(info.reqId);
367+
Assert.assertNotNull(resp);
368+
}
369+
370+
@SmallTest
371+
public void testDnsHijacking() throws Throwable {
372+
Configuration c = new Configuration.Builder()
373+
.zone(new Zone("uphijacktest.qiniu.com", Zone.zone0.upHostBackup, Zone.zone0.upIp))
374+
.build();
375+
UploadManager _up = new UploadManager(c);
376+
final String expectKey = "你好;\"\r\n\r\n\r\n";
377+
Map<String, String> params = new HashMap<String, String>();
378+
params.put("x:foo", "fooval");
379+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
380+
381+
uploadManager.put("hello".getBytes(), expectKey, TestConfig.token, new UpCompletionHandler() {
382+
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
383+
Log.i("qiniutest", k + rinfo);
384+
key = k;
385+
info = rinfo;
386+
resp = response;
387+
signal.countDown();
388+
}
389+
}, opt);
390+
391+
350392
try {
351393
signal.await(120, TimeUnit.SECONDS); // wait for callback
352394
} catch (InterruptedException e) {

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.qiniu.android.storage.Configuration;
1010
import com.qiniu.android.storage.UpCompletionHandler;
1111
import com.qiniu.android.storage.UploadManager;
12+
import com.qiniu.android.storage.Zone;
1213

1314
import junit.framework.Assert;
1415

@@ -106,6 +107,47 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
106107
TempFile.remove(f);
107108
}
108109

110+
private void templateHijack(int size) throws Throwable {
111+
final String expectKey = "r=" + size + "k";
112+
final File f = TempFile.createFile(size);
113+
114+
Configuration c = new Configuration.Builder()
115+
.zone(new Zone("uphijacktest.qiniu.com", Zone.zone0.upHostBackup, Zone.zone0.upIp))
116+
.build();
117+
UploadManager uploadManager = new UploadManager(c);
118+
119+
uploadManager.put(f, expectKey, TestConfig.token, new UpCompletionHandler() {
120+
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
121+
Log.i("qiniutest", k + rinfo);
122+
key = k;
123+
info = rinfo;
124+
resp = response;
125+
signal.countDown();
126+
}
127+
}, null);
128+
129+
try {
130+
signal.await(500, TimeUnit.SECONDS); // wait for callback
131+
} catch (InterruptedException e) {
132+
e.printStackTrace();
133+
}
134+
// 尝试获取info信息。
135+
// key == null : 没进入 complete ? 什么导致的?
136+
if (!expectKey.equals(key)) {
137+
//此处通不过, travis 会打印信息
138+
Assert.assertEquals("", info);
139+
}
140+
if (info == null || !info.isOK()) {
141+
//此处通不过, travis 会打印信息
142+
Assert.assertEquals("", info);
143+
}
144+
Assert.assertEquals(expectKey, key);
145+
Assert.assertTrue(info.isOK());
146+
Assert.assertNotNull(info.reqId);
147+
Assert.assertNotNull(resp);
148+
TempFile.remove(f);
149+
}
150+
109151
@MediumTest
110152
public void test600k() throws Throwable {
111153
template(600);
@@ -121,6 +163,12 @@ public void test4M() throws Throwable {
121163
template(1024 * 4);
122164
}
123165

166+
@LargeTest
167+
public void test2MHijack() throws Throwable {
168+
templateHijack(1024 * 2);
169+
}
170+
171+
124172
// @LargeTest
125173
// public void test8M1k() throws Throwable{
126174
// template(1024*8+1);

library/src/main/java/com/qiniu/android/http/ResponseInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public boolean needRetry() {
118118
}
119119

120120
public boolean isNotQiniu() {
121-
return statusCode < 500 && statusCode >= 200 && reqId != null;
121+
return statusCode < 500 && statusCode >= 200 && reqId == null;
122122
}
123123

124124
public String toString() {

library/src/main/java/com/qiniu/android/storage/FormUploader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void complete(ResponseInfo info, JSONObject response) {
109109
} else if(options.cancellationSignal.isCancelled()){
110110
ResponseInfo i = ResponseInfo.cancelled();
111111
completionHandler.complete(key, i, null);
112-
} else if (info.needRetry()) {
112+
} else if (info.needRetry() || (info.isNotQiniu() && !token.hasReturnUrl())) {
113113
CompletionHandler retried = new CompletionHandler() {
114114
@Override
115115
public void complete(ResponseInfo info, JSONObject response) {

library/src/main/java/com/qiniu/android/storage/ResumeUploader.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ public void complete(ResponseInfo info, JSONObject response) {
186186
return;
187187
}
188188

189-
if (info.needRetry() && retried < config.retryMax) {
189+
if (isNotQiniu(info)){
190+
forceIp = true;
191+
}
192+
193+
if (isNotQiniu(info) || (info.needRetry() && retried < config.retryMax)) {
190194
nextTask(offset, retried + 1, host);
191195
return;
192196
}
@@ -222,10 +226,10 @@ public void complete(ResponseInfo info, JSONObject response) {
222226
nextTask((offset / Configuration.BLOCK_SIZE) * Configuration.BLOCK_SIZE, retried, host);
223227
return;
224228
}
225-
if (info.isNotQiniu() && !token.hasReturnUrl()){
229+
if (isNotQiniu(info)){
226230
forceIp = true;
227231
}
228-
if (retried >= config.retryMax || !info.needRetry()) {
232+
if (!isNotQiniu(info) && (retried >= config.retryMax || !info.needRetry())) {
229233
completionHandler.complete(key, info, null);
230234
return;
231235
}
@@ -318,4 +322,8 @@ private void record(int offset) {
318322
size, offset, modifyTime, StringUtils.jsonJoin(contexts));
319323
config.recorder.set(recorderKey, data.getBytes());
320324
}
325+
326+
private boolean isNotQiniu(ResponseInfo info){
327+
return info.isNotQiniu() && !token.hasReturnUrl();
328+
}
321329
}

0 commit comments

Comments
 (0)