toby / serde-bencode

Serde backed Bencode encoding/decoding library for Rust.
MIT License
65 stars 16 forks source link

Add test for full torrent file deserialization #32

Closed josecelano closed 1 year ago

josecelano commented 1 year ago

I'm using this library to deserialize a torrent file like in this repo example.

For some reason I do not know yet, it's not working with some torrents when I use the library in my project, but it's not a library issue because it's working as you can see in this PR.

This is the bug I'm trying to fix:

https://github.com/torrust/torrust-index-backend/issues/266

I've added a test for full torrent deserialization, which, I suppose, it's the most common use for this package. I have included the torrents I have problems with.

FYI, one test is failing:

---- ser_de_flattened_adjacently_tagged_enum stdout ----
bytes: "d7:contentd5:tokeni456ee2:idi123e4:type7:Requeste"
thread 'ser_de_flattened_adjacently_tagged_enum' panicked at tests/tests.rs:39:41:
called `Result::unwrap()` on an `Err` value: InvalidType("Invalid Type: byte array (expected: `string or map`)")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
josecelano commented 1 year ago

I was able to reproduce the bug with a test:

$ cargo test deserialize_full_torrent_files
    Finished test [unoptimized + debuginfo] target(s) in 0.00s
     Running unittests src/lib.rs (target/debug/deps/serde_bencode-ce78c065639cd56b)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

     Running tests/tests.rs (target/debug/deps/tests-6f3d7857ebd77de3)

running 1 test
test deserialize_full_torrent_files ... FAILED

failures:

---- deserialize_full_torrent_files stdout ----
Parsing torrent file: "./tests/fixtures/torrents/006-VictorYerena.txt-51f0aef3527d73ee84a0b031df648b349a01bd47.torrent"
thread 'deserialize_full_torrent_files' panicked at tests/tests.rs:558:21:
ERROR: Invalid Type: sequence (expected: `bytes`)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

failures:
    deserialize_full_torrent_files

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 40 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test tests`
josecelano commented 1 year ago

This is the line throwing the error:

https://github.com/toby/serde-bencode/blob/553adb4be4962afcf30c79fe51b1ec191546c7b9/src/error.rs#L65

    fn invalid_type(unexp: Unexpected, exp: &dyn Expected) -> Self {
        Error::InvalidType(format!("Invalid Type: {} (expected: `{}`)", unexp, exp))
    }

And it seems the problem is there is no implementation for this function:

https://github.com/serde-rs/serde/blob/master/serde/src/de/mod.rs#L1641-L1647

    /// The input contains a sequence of elements.
    ///
    /// The default implementation fails with a type error.
    fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        let _ = seq;
        Err(Error::invalid_type(Unexpected::Seq, &self))
    }
josecelano commented 1 year ago

Do you happen to know if this repo is actively maintained? cc @toby @chpio