Skip to content

Commit

Permalink
example of reading invalid keys from a bdecode_node (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml authored Oct 23, 2024
1 parent ea090ff commit 3864c2a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ jobs:
run: |
cp build/libtorrent4j.dylib .
./gradlew test
- name: coverage
if: github.ref == 'refs/heads/master'
run: ./gradlew codacyCoverage
env:
CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
# - name: coverage
# if: github.ref == 'refs/heads/master'
# run: ./gradlew codacyCoverage
# env:
# CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
- uses: actions/upload-artifact@v4
with:
name: libtorrent4j-macos-cmake
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[![Windows](https://github.com/aldenml/libtorrent4j/workflows/Windows/badge.svg)](https://github.com/aldenml/libtorrent4j/actions?query=workflow%3AWindows)
[![Android](https://github.com/aldenml/libtorrent4j/workflows/Android/badge.svg)](https://github.com/aldenml/libtorrent4j/actions?query=workflow%3AAndroid)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/5dda1f00528e4d93864eb8694c702bba)](https://app.codacy.com/manual/aldenml/libtorrent4j/dashboard)
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/5dda1f00528e4d93864eb8694c702bba)](https://app.codacy.com/manual/aldenml/libtorrent4j/dashboard)
[![Maven Central](https://img.shields.io/maven-central/v/org.libtorrent4j/libtorrent4j.svg?label=maven)](https://search.maven.org/search?q=g:%22org.libtorrent4j%22%20AND%20a:%22libtorrent4j%22)

A swig Java interface for [libtorrent](https://github.com/arvidn/libtorrent).
Expand Down
37 changes: 35 additions & 2 deletions src/test/java/org/libtorrent4j/BDecodeReadTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
/*
* Copyright (c) 2018-2024, Alden Torres
*
* Licensed under the terms of the MIT license.
* Copy of the license at https://opensource.org/licenses/MIT
*/
package org.libtorrent4j;

import org.junit.Test;
import org.libtorrent4j.swig.bdecode_node;
import org.libtorrent4j.swig.byte_vector;
import org.libtorrent4j.swig.error_code;
import org.libtorrent4j.swig.torrent_info;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

/**
* @author gubatron
Expand All @@ -34,4 +39,32 @@ public void testRead() throws IOException {

assertEquals("failed to create torrent info: " + ec.message(), ret, 0);
}

/**
* To address <a href="https://github.com/aldenml/libtorrent4j/pull/244">Fix SIGSEGV in bdecode_node if the pointer is null</a>
*/
@Test
public void testReadNone() throws IOException {
byte[] data = Utils.resourceBytes("test5.torrent");

byte_vector buffer = Vectors.bytes2byte_vector(data);
bdecode_node e = new bdecode_node();
error_code ec = new error_code();
int ret = bdecode_node.bdecode(buffer, e, ec);

assertEquals("failed to decode torrent: " + ec.message(), ret, 0);

bdecode_node n1 = e.dict_find_ex("info");
assertEquals(n1.type(), bdecode_node.type_t.dict_t);
// { 'length': , 'name': , 'piece length': , 'pieces': }
assertEquals(n1.dict_size(), 4);

bdecode_node n2 = e.dict_find_list_ex("any-key");
assertEquals(n2.type(), bdecode_node.type_t.none_t);
// if (n2.type() != bdecode_node.type_t.none_t)
// assertEquals(n2.list_size(), <size>);

ec.clear();
buffer.clear(); // prevents GC
}
}

0 comments on commit 3864c2a

Please sign in to comment.