Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions library/src/androidTest/java/com/qiniu/android/FormUploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Assert.assertTrue(info.isOK());
Assert.assertNotNull(info.reqId);
Assert.assertNotNull(resp);
Assert.assertEquals("/", info.path);
}

@SmallTest
Expand Down Expand Up @@ -106,6 +107,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Assert.assertEquals(expectKey, key);
Assert.assertTrue(info.isOK());
Assert.assertNotNull(info.reqId);
Assert.assertEquals("/", info.path);
Assert.assertNotNull(resp);
Assert.assertEquals("Fqr0xh3cxeii2r7eDztILNmuqUNN", resp.optString("key", ""));
}
Expand Down Expand Up @@ -323,4 +325,90 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Assert.assertNotNull(resp);
}

@SmallTest
public void testPortBackup() throws Throwable {
Configuration c = new Configuration.Builder()
.zone(new Zone("upload.qiniu.com", Zone.zone0.upHostBackup, Zone.zone0.upIp))
.upPort(9999)
.build();
UploadManager _up = new UploadManager(c);
final String expectKey = "你好;\"\r\n\r\n\r\n";
Map<String, String> params = new HashMap<String, String>();
params.put("x:foo", "fooval");
final UploadOptions opt = new UploadOptions(params, null, true, null, null);

uploadManager.put("hello".getBytes(), expectKey, TestConfig.token, new UpCompletionHandler() {
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Log.i("qiniutest", k + rinfo);
key = k;
info = rinfo;
resp = response;
signal.countDown();
}
}, opt);


try {
signal.await(120, TimeUnit.SECONDS); // wait for callback
} catch (InterruptedException e) {
e.printStackTrace();
}
// 尝试获取info信息。
// key == null : 没进入 complete ? 什么导致的?
if (!expectKey.equals(key)) {
//此处通不过, travis 会打印信息
Assert.assertEquals("", info);
}
if (info == null || !info.isOK()) {
//此处通不过, travis 会打印信息
Assert.assertEquals("", info);
}
Assert.assertEquals(expectKey, key);
Assert.assertTrue(info.isOK());
Assert.assertNotNull(info.reqId);
Assert.assertNotNull(resp);
}

@SmallTest
public void testDnsHijacking() throws Throwable {
Configuration c = new Configuration.Builder()
.zone(new Zone("uphijacktest.qiniu.com", Zone.zone0.upHostBackup, Zone.zone0.upIp))
.build();
UploadManager _up = new UploadManager(c);
final String expectKey = "你好;\"\r\n\r\n\r\n";
Map<String, String> params = new HashMap<String, String>();
params.put("x:foo", "fooval");
final UploadOptions opt = new UploadOptions(params, null, true, null, null);

uploadManager.put("hello".getBytes(), expectKey, TestConfig.token, new UpCompletionHandler() {
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Log.i("qiniutest", k + rinfo);
key = k;
info = rinfo;
resp = response;
signal.countDown();
}
}, opt);


try {
signal.await(120, TimeUnit.SECONDS); // wait for callback
} catch (InterruptedException e) {
e.printStackTrace();
}
// 尝试获取info信息。
// key == null : 没进入 complete ? 什么导致的?
if (!expectKey.equals(key)) {
//此处通不过, travis 会打印信息
Assert.assertEquals("", info);
}
if (info == null || !info.isOK()) {
//此处通不过, travis 会打印信息
Assert.assertEquals("", info);
}
Assert.assertEquals(expectKey, key);
Assert.assertTrue(info.isOK());
Assert.assertNotNull(info.reqId);
Assert.assertNotNull(resp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import android.util.Log;

import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.Zone;

import junit.framework.Assert;

Expand All @@ -26,7 +28,8 @@ public class ResumeUploadTest extends InstrumentationTestCase {
private volatile JSONObject resp;

public void setUp() throws Exception {
uploadManager = new UploadManager();
Configuration config = new Configuration.Builder().upPort(9999).build();
uploadManager = new UploadManager(config);
}

private void template(int size) throws Throwable {
Expand Down Expand Up @@ -104,6 +107,47 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
TempFile.remove(f);
}

private void templateHijack(int size) throws Throwable {
final String expectKey = "r=" + size + "k";
final File f = TempFile.createFile(size);

Configuration c = new Configuration.Builder()
.zone(new Zone("uphijacktest.qiniu.com", Zone.zone0.upHostBackup, Zone.zone0.upIp))
.build();
UploadManager uploadManager = new UploadManager(c);

uploadManager.put(f, expectKey, TestConfig.token, new UpCompletionHandler() {
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Log.i("qiniutest", k + rinfo);
key = k;
info = rinfo;
resp = response;
signal.countDown();
}
}, null);

try {
signal.await(500, TimeUnit.SECONDS); // wait for callback
} catch (InterruptedException e) {
e.printStackTrace();
}
// 尝试获取info信息。
// key == null : 没进入 complete ? 什么导致的?
if (!expectKey.equals(key)) {
//此处通不过, travis 会打印信息
Assert.assertEquals("", info);
}
if (info == null || !info.isOK()) {
//此处通不过, travis 会打印信息
Assert.assertEquals("", info);
}
Assert.assertEquals(expectKey, key);
Assert.assertTrue(info.isOK());
Assert.assertNotNull(info.reqId);
Assert.assertNotNull(resp);
TempFile.remove(f);
}

@MediumTest
public void test600k() throws Throwable {
template(600);
Expand All @@ -119,6 +163,12 @@ public void test4M() throws Throwable {
template(1024 * 4);
}

@LargeTest
public void test2MHijack() throws Throwable {
templateHijack(1024 * 2);
}


// @LargeTest
// public void test8M1k() throws Throwable{
// template(1024*8+1);
Expand Down
14 changes: 14 additions & 0 deletions library/src/androidTest/java/com/qiniu/android/TokenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,18 @@ public void testRight() {
UpToken t = UpToken.parse(TestConfig.token);
Assert.assertNotNull(t);
}

public void testEmpty() {
UpToken t = UpToken.parse(null);
Assert.assertNull(t);

t = UpToken.parse("");
Assert.assertNull(t);
}

public void testReturnUrl() {
UpToken t = UpToken.parse("QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm:1jLiztn4plVyeB8Hie1ryO5z9uo=:eyJzY29wZSI6InB5c2RrIiwiZGVhZGxpbmUiOjE0MzM0ODM5MzYsInJldHVyblVybCI6Imh0dHA6Ly8xMjcuMC4wLjEvIn0=");
Assert.assertTrue(t.hasReturnUrl());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


public final class Constants {
public static final String VERSION = "7.0.5.1";
public static final String VERSION = "7.0.6";

public static final String UTF_8 = "utf-8";
}
39 changes: 31 additions & 8 deletions library/src/main/java/com/qiniu/android/http/HttpManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ public void postData(String url, byte[] data, Header[] headers, ProgressHandler
}

private void postEntity(String url, final HttpEntity entity, Header[] headers,
ProgressHandler progressHandler, CompletionHandler completionHandler, final boolean forceIp) {
final ProgressHandler progressHandler, final CompletionHandler completionHandler, final boolean forceIp) {
final CompletionHandler wrapper = wrap(completionHandler);
final Header[] h = reporter.appendStatHeaders(headers);

if (converter != null){
url = converter.convert(url);
}

final AsyncHttpResponseHandler originHandler = new ResponseHandler(url, wrapper, progressHandler);
ResponseHandler handler = new ResponseHandler(url, wrapper, progressHandler);
if(backUpIp == null || converter != null){
client.post(null, url, h, entity, null, originHandler);
client.post(null, url, h, entity, null, handler);
return;
}
final String url2 = url;
Expand All @@ -130,13 +130,18 @@ private void postEntity(String url, final HttpEntity entity, Header[] headers,
t.execute(new Runnable() {
@Override
public void run() {
URI uri = URI.create(url2);
String ip = Dns.getAddress(uri.getHost());
if (ip == null || ip.equals("") || forceIp) {
final URI uri = URI.create(url2);
String ip = null;
if (forceIp) {
ip = backUpIp;
}else {
ip = Dns.getAddress(uri.getHost());
if (ip == null || ip.equals("")){
ip = backUpIp;
}
}

Header[] h2 = new Header[h.length + 1];
final Header[] h2 = new Header[h.length + 1];
System.arraycopy(h, 0, h2, 0, h.length);

String newUrl = null;
Expand All @@ -146,7 +151,25 @@ public void run() {
throw new AssertionError(e);
}
h2[h.length] = new BasicHeader("Host", uri.getHost());
client.post(null, newUrl, h2, entity, null, originHandler);
final String ip2 = ip;
ResponseHandler handler2 = new ResponseHandler(url2, wrap(new CompletionHandler() {
@Override
public void complete(ResponseInfo info, JSONObject response) {
if (uri.getPort() == 80 || info.statusCode != ResponseInfo.CannotConnectToHost){
completionHandler.complete(info, response);
return;
}
String newUrl80 = null;
try {
newUrl80 = new URI(uri.getScheme(), null, ip2, 80, uri.getPath(), uri.getQuery(), null).toString();
} catch (URISyntaxException e) {
throw new AssertionError(e);
}
ResponseHandler handler3 = new ResponseHandler(newUrl80, completionHandler, progressHandler);
client.post(null, newUrl80, h2, entity, null, handler3);
}
}), progressHandler);
client.post(null, newUrl, h2, entity, null, handler2);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ public final class ResponseHandler extends AsyncHttpResponseHandler {
*/
private int port = -1;

private String path = null;

public ResponseHandler(String url, CompletionHandler completionHandler, ProgressHandler progressHandler) {
super(Looper.getMainLooper());
URI uri = null;
try {
uri = new URI(url);
this.host = uri.getHost();
this.port = uri.getPort();
this.path = uri.getPath();
} catch (URISyntaxException e) {
this.host = "N/A";
e.printStackTrace();
Expand All @@ -69,7 +72,7 @@ public ResponseHandler(String url, CompletionHandler completionHandler, Progress
}

private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers, byte[] responseBody,
String host, String ip, int port, double duration, Throwable error) {
String host, String path, String ip, int port, double duration, Throwable error) {

if (error != null && error instanceof CancellationHandler.CancellationException) {
return ResponseInfo.cancelled();
Expand Down Expand Up @@ -138,7 +141,7 @@ private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers,
}
}

return new ResponseInfo(statusCode, reqId, xlog, xvia, host, ip, port, duration, err);
return new ResponseInfo(statusCode, reqId, xlog, xvia, host, path, ip, port, duration, err);
}

private static JSONObject buildJsonResp(byte[] body) throws Exception {
Expand All @@ -156,15 +159,15 @@ public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
} catch (Exception e) {
exception = e;
}
ResponseInfo info = buildResponseInfo(statusCode, headers, null, host, ip, port, duration, exception);
ResponseInfo info = buildResponseInfo(statusCode, headers, null, host, path, ip, port, duration, exception);
Log.i("upload----success", info.toString());
completionHandler.complete(info, obj);
}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
double duration = (System.currentTimeMillis() - reqStartTime) / 1000.0;
ResponseInfo info = buildResponseInfo(statusCode, headers, responseBody, host, ip, port, duration, error);
ResponseInfo info = buildResponseInfo(statusCode, headers, responseBody, host, path, ip, port, duration, error);
Log.i("upload----failed", info.toString());
completionHandler.complete(info, null);
}
Expand Down
Loading