Permalink
Browse files

Add baseline Metafile unit tests.

1 parent 100161f commit 03d1dd776577bcdfb3fa8216452ffabd82d32188 @abahgat abahgat committed Jun 17, 2015
View
@@ -1,4 +1,6 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<groupId>org.bitlet</groupId>
<modelVersion>4.0.0</modelVersion>
<artifactId>wetorrent</artifactId>
@@ -26,6 +28,14 @@
</plugin>
</plugins>
</build>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ <version>4.11</version>
+ </dependency>
+ </dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
@@ -58,7 +58,6 @@ public Metafile(InputStream is) throws IOException, NoSuchAlgorithmException {
rootDictionary = (SortedMap) getRootElement();
info = (SortedMap) rootDictionary.get(ByteBuffer.wrap("info".getBytes()));
-
pieceLength = (Long) info.get(ByteBuffer.wrap("piece length".getBytes()));
byte[] piecesByteString = ((ByteBuffer) info.get(ByteBuffer.wrap("pieces".getBytes()))).array();
@@ -68,10 +67,8 @@ public Metafile(InputStream is) throws IOException, NoSuchAlgorithmException {
piecesSha.add(sha1);
}
-
name = new String(((ByteBuffer) info.get(ByteBuffer.wrap("name".getBytes()))).array());
-
length = (Long) info.get(ByteBuffer.wrap("length".getBytes()));
List files = (List) info.get(ByteBuffer.wrap("files".getBytes()));
@@ -180,14 +177,11 @@ public void computeInfoSha1() throws NoSuchAlgorithmException, IOException {
Bencode bencode = new Bencode();
bencode.setRootElement(info);
-
MessageDigest md = MessageDigest.getInstance("SHA1");
-
ByteArrayOutputStream out = new ByteArrayOutputStream();
bencode.print(out);
-
md.update(ByteBuffer.wrap(out.toByteArray()));
infoSha1 = md.digest();
@@ -202,7 +196,6 @@ public String getInfoSha1Encoded() {
infoSha1Encoded = Utils.byteArrayToURLString(getInfoSha1());
}
return infoSha1Encoded;
-
}
public long getLength() {
@@ -0,0 +1,59 @@
+/*
+ * bitlet - Simple bittorrent library
+ * Copyright (C) 2008 Alessandro Bahgat Shehata, Daniele Castagna
@DCastagna
DCastagna Jun 17, 2015 Contributor

2008?

@abahgat
abahgat Jun 17, 2015 Member

Copied from another file. Must be the year we released it :P

+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.bitlet.wetorrent;
+
+import org.junit.Test;
+
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+public class MetafileTest {
+
+ private static String TEST_TORRENT_PATH = "/ubuntu-15.04-desktop-amd64.iso.torrent";
+
+ @Test
+ public void readMetafile() throws Exception {
+ InputStream stream = MetafileTest.class.getResourceAsStream(TEST_TORRENT_PATH);
+ Metafile metafile = new Metafile(stream);
+ assertEquals("http://torrent.ubuntu.com:6969/announce", metafile.getAnnounce());
+
+ List announceList = new LinkedList();
+ List firstList = new LinkedList();
+ firstList.add(ByteBuffer.wrap("http://torrent.ubuntu.com:6969/announce".getBytes()));
+ announceList.add(firstList);
+ List secondList = new LinkedList();
+ secondList.add(ByteBuffer.wrap("http://ipv6.torrent.ubuntu.com:6969/announce".getBytes()));
+ announceList.add(secondList);
+ assertEquals(announceList, metafile.getAnnounceList());
+ assertEquals("Ubuntu CD releases.ubuntu.com", metafile.getComment());
+ assertNull(metafile.getCreatedBy());
+ assertEquals(1429786237L, metafile.getCreationDate().longValue());
+ assertEquals(Collections.emptyList(), metafile.getFiles());
+ // TODO assert on metafile.getInfo());
+ assertEquals(1150844928L, metafile.getLength());
+ assertEquals("ubuntu-15.04-desktop-amd64.iso", metafile.getName());
+ assertEquals(524288L, metafile.getPieceLength().longValue());
+ assertEquals(2196, metafile.getPieces().size());
+ }
+}
Binary file not shown.

0 comments on commit 03d1dd7

Please sign in to comment.