bencode decoding is complete and fully tested on real torrent files

This commit is contained in:
mykola2312 2024-10-16 02:29:30 +03:00
parent c1dcb19f9d
commit ef86c78ba4
3 changed files with 49 additions and 0 deletions

View file

@ -98,6 +98,8 @@ public class BTree {
// advance past terminator // advance past terminator
offset++; offset++;
return dict; return dict;
} else if (type == BDecoder.BE_END) {
throw new BDecodeMalformed(data, offset, "encountered terminator where it shouldn't be");
} else { // string } else { // string
// since string does not have leading type byte, move back offset // since string does not have leading type byte, move back offset
offset--; offset--;

View file

@ -5,10 +5,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import com.mykola2312.retracker.bencode.error.BDecodeError; import com.mykola2312.retracker.bencode.error.BDecodeError;
import com.mykola2312.retracker.bencode.error.BDecodeMalformed; import com.mykola2312.retracker.bencode.error.BDecodeMalformed;
import com.mykola2312.retracker.bencode.error.BError;
public class BTreeTest { public class BTreeTest {
@Test @Test
@ -116,4 +121,46 @@ public class BTreeTest {
assertEquals(new BInteger(69), root.<BInteger>get(BType.INTEGER, "second")); assertEquals(new BInteger(69), root.<BInteger>get(BType.INTEGER, "second"));
}); });
} }
@Test
public void testNestedDict() throws BError {
final byte[] data = "d5:firstd6:secondd5:thirdi1337eeee".getBytes();
BTree tree = new BTree();
assertDoesNotThrow(() -> {
tree.decode(data);
BInteger value = tree
.asDict()
.<BDict>get(BType.DICT, "first")
.<BDict>get(BType.DICT, "second")
.<BInteger>get(BType.INTEGER, "third");
assertNotNull(value);
assertEquals(new BInteger(1337), value);
});
}
@Test
public void testTorrentFile() throws BError, IOException {
final byte[] data = Files.readAllBytes(Path.of("test", "test.torrent"));
BTree torrent = new BTree();
assertDoesNotThrow(() -> {
torrent.decode(data);
BDict root = torrent.asDict();
assertEquals(new BString("http://example.com/announce"), root.<BString>get(BType.STRING, "announce"));
assertEquals(new BString("Test Comment"), root.<BString>get(BType.STRING, "comment"));
assertEquals(new BString("qBittorrent v4.6.5"), root.<BString>get(BType.STRING, "created by"));
assertEquals(new BInteger(1729033917), root.<BString>get(BType.INTEGER, "creation date"));
BInteger pieceLength = root
.<BDict>get(BType.DICT, "info")
.<BDict>get(BType.DICT, "file tree")
.<BDict>get(BType.DICT, "random-data.bin")
.<BDict>get(BType.DICT, "")
.<BInteger>get(BType.INTEGER, "length");
assertEquals(new BInteger(16777216), pieceLength);
});
}
} }

BIN
test/test.torrent Normal file

Binary file not shown.