Skip to content
Snippets Groups Projects
Commit 1e66b4eb authored by Guy Zana's avatar Guy Zana
Browse files

tests: make the TCPDownloadFile test a bit more stressful

now it downloads a ~200MB file and validating md5 on it.
parent cc6ff2f9
No related branches found
No related tags found
No related merge requests found
package com.cloudius.cli.tests;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.security.MessageDigest;
import com.cloudius.util.MD5;
public class TCPDownloadFile implements Test {
public static final String _url = "http://mirror.isoc.org.il/pub/fedora/releases/18/Fedora/x86_64/iso/Fedora-18-x86_64-netinst.iso";
public static final int _chunk_size = 0x10000;
public static final String _expected_md5 = "227acebbc5392a4600349ae0c2d0ffcf";
@Override
public boolean run() {
try {
URL website = new URL("http://212.58.244.66/");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("/tmp/bbc.co.uk.html");
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
fos.close();
URL url = new URL(_url);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
int rc = conn.getResponseCode();
if (rc != HttpURLConnection.HTTP_OK) {
System.out.format("error: HTTP response code %d, not 200\n", rc);
return (false);
}
int totlen = conn.getContentLength();
InputStream is = conn.getInputStream();
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
System.out.println("Begining file download...");
int bytes=0;
int total_bytes = 0;
byte[] buf = new byte[_chunk_size];
do {
bytes = is.read(buf);
total_bytes += bytes;
System.out.format("\rDownloaded %.2f%%",
((float)total_bytes / (float)totlen)*100);
if (bytes > 0) {
m.update(buf, 0, bytes);
}
} while (bytes != -1);
System.out.println("\nFinished downloading");
is.close();
conn.disconnect();
byte[] digest = m.digest();
String dl_md5 = MD5.toHex(digest);
System.out.format("Downloaded md5: %s\n", dl_md5);
System.out.format("Expected md5: %s\n", _expected_md5);
if (!dl_md5.equals(_expected_md5)) {
System.out.println("error: md5 does not match!");
return (false);
} else {
System.out.println("success: md5 match!");
}
} catch (Exception ex) {
ex.printStackTrace();
return (false);
}
return (true);
}
......
127.0.0.1 localhost osv.local
212.58.244.66 www.bbc.co.uk
192.115.211.70 mirror.isoc.org.il
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment