Stage 5 parse torrent file

when i run program with default sample_torrent i get that:
./your_bittorrent.sh info sample.torrent
announce: htp://bittorrent-test-tracker.codecrafters.io/announce
length: 92063
but when i run codecrafters test i get
/
******** LOG ********
Initiating test run…

:zap: This is a turbo test run.

Running tests. Logs should appear shortly…

[tester::#OW9] Running tests for Stage #OW9 (Parse torrent file)
[tester::#OW9] Running ./your_bittorrent.sh info /tmp/torrents1016607044/itsworking.gif.torrent
[tester::#OW9] Application didn’t terminate successfully without errors. Expected 0 as exit code, got: -1
[tester::#OW9] Test failed (try setting ‘debug: true’ in your codecrafters.yml to see more details)

View our article on debugging test failures: How do I debug test failures? - CodeCrafters

with DEBUG mode no changes

i havent this path in my /tmp/ and i think script don’t create a file for this path therefore i have error, and can’t go further

code: GitHub - Default-Anon/simple_torrent

can i auto complete the test with errors ,can you help me?

@Default-Anon can you try adding debug logs to figure out why your program is exiting with a non-zero exit code?

Docs here: How do I debug test failures? - CodeCrafters.

Also, you can find the files that the tester uses here: bittorrent-tester/torrents at main · codecrafters-io/bittorrent-tester · GitHub.

The answer to this is no, you can’t pass a stage unless you pass the tests

[pc@pc-default codecrafters-bittorrent-c]$ ./your_bittorrent.sh info codercat.gif.torrent
[your_program] torrent file content write to buffer
decoded is 0x5b76c8833ec0
[your program] decoded text is init, decoded type is 4
announce: htt*p://bittorrent-test-tracker.codecrafters.io/announce
[your_program] announce tag printed successfully
length: 2994120
[your program] length tag from dictionary which value of key info printed successfully

[pc@pc-default codecrafters-bittorrent-c]$ ./your_bittorrent.sh info congratulations.gif.torrent
[your_program] torrent file content write to buffer
decoded is 0x575178d43ec0
[your program] decoded text is init, decoded type is 4
announce: ht*tp://bittorrent-test-tracker.codecrafters.io/announce
[your_program] announce tag printed successfully
length: 820892
[your program] length tag from dictionary which value of key info printed successfully

[pc@pc-default codecrafters-bittorrent-c]$ ./your_bittorrent.sh info itsworking.gif.torrent
[your_program] torrent file content write to buffer
decoded is 0x5c3496961ec0
[your program] decoded text is init, decoded type is 4
announce: ht*p://bittorrent-test-tracker.codecrafters.io/announce
[your_program] announce tag printed successfully
length: 2549700
[your program] length tag from dictionary which value of key info printed successfully

and codecrafters test only [tester::#OW9]
Running tests for Stage #OW9 (Parse torrent file)
[tester::#OW9] Running ./your_bittorrent.sh info /tmp/torrents3200416602/itsworking.gif.torrent
[your_program] [your_program] torrent file content write to buffer
[tester::#OW9] Application didn’t terminate successfully without errors. Expected 0 as exit code, got: -1
[tester::#OW9] Test failed

its very strange, test ended after writing to local buffer succesfully, i want to catch what he do but he don’t even going to next function decode_bencode, no logs no anything, just get me exception

i test this file and all work good, you can see what i get in my logs, i dont understand it’s normal when i get right answers and program work ordinary but fucking test work very awful.source code of script which control that is:
stage_parse_torrent.go
package internal

import (
“fmt”
“os”
“path”
“strings”

"github.com/codecrafters-io/tester-utils/test_case_harness"

)

func testParseTorrent(stageHarness *test_case_harness.TestCaseHarness) error {
initRandom()

logger := stageHarness.Logger
executable := stageHarness.Executable
torrent := randomTorrent()

tempDir, err := os.MkdirTemp("", "torrents")
if err != nil {
	return err
}

if err := copyTorrent(tempDir, torrent.filename); err != nil {
	logger.Errorln("Couldn't copy torrent file")
	return err
}

torrentPath := path.Join(tempDir, torrent.filename)

logger.Infof("Running ./your_bittorrent.sh info %s", torrentPath)
result, err := executable.Run("info", torrentPath)
if err != nil {
	return err
}

if err = assertExitCode(result, 0); err != nil {
	return err
}

expectedTrackerURLValue := fmt.Sprintf("Tracker URL: %s", torrent.tracker)
expectedLengthValue := fmt.Sprintf("Length: %d", torrent.length)

logger.Debugf("Checking for tracker URL (%v)", expectedTrackerURLValue)

if err = assertStdoutContains(result, expectedTrackerURLValue); err != nil {
	actual := string(result.Stdout)
	if strings.Contains(actual, fmt.Sprintf("Tracker URL:%s", torrent.tracker)) {
		logger.Errorln("There needs to be a space character after Tracker URL:")
	}
	return err
}

logger.Successf("Tracker URL is correct")

logger.Debugf("Checking for length (%v)", expectedLengthValue)

if err = assertStdoutContains(result, expectedLengthValue); err != nil {
	actual := string(result.Stdout)
	if strings.Contains(actual, fmt.Sprintf("Length:%d", torrent.length)) {
		logger.Errorln("There needs to be a space character after Length:")
	}
	return err
}

logger.Successf("Length is correct")

return nil

}
i don’t know golang, but if it can be static path, not dynamic in temp folder, maybe i can resolve my trouble, i don’t expected this shit. If you can’t help me okey, but pls write about it and close topic. Very dissapointed

@Default-Anon I can confirm that the logs issue is indeed a problem, you can fix this by adding these two lines to the top of main:

We’re working on adding this all starter code this week.

With those lines added, I was able to add debug statements and get proper logs:

Haven’t looked into the actual code yet, but hopefully this fixes the frustrating debug logs problem at least and let’s you move forward.

yeah it’s more better and now i can see where stucked more precisely,
/* LOGS NOW *\

 Running tests. Logs should appear shortly...

[tester::#OW9] Running tests for Stage #OW9 (Parse torrent file)
[tester::#OW9] Running ./your_bittorrent.sh info /tmp/torrents986248331/congratu
lations.gif.torrent
[your_program] [your_program] starting
[your_program] [your_program] buf is writed
[your_program] [your program] type is returned its a 4
[your_program] [your program] decode_dict is started
[your_program] [your program] type is returned its a 2
[your_program] [your program] decode_string is started
[your_program] [your program] str is announce
[your_program] [your_program] dict_key value is announce
[your_program] [your program] type is returned its a 2
[your_program] [your program] decode_string is started
[your_program] [your program] str is http://bittorrent-test-tracker.codecrafters
.io/announce
[your_program] [your program] dict value type is 2
[your_program] [your program] type is returned its a 2
[your_program] [your program] decode_string is started
[your_program] [your program] str is created by
[your_program] [your_program] dict_key value is created by
[your_program] [your program] type is returned its a 2
[your_program] [your program] decode_string is started
[your_program] [your program] str is mktorrent 1.1
[your_program] [your program] dict value type is 2
[your_program] [your program] type is returned its a 2
[your_program] [your program] decode_string is started
[your_program] [your program] str is info
[your_program] [your_program] dict_key value is info
[your_program] [your program] type is returned its a 4
[your_program] [your program] decode_dict is started
[your_program] [your program] type is returned its a 2
[your_program] [your program] decode_string is started
[your_program] [your program] str is length
[your_program] [your_program] dict_key value is length
[your_program] [your program] type is returned its a 1
[your_program] [your program] decode_integer is started
[your_program] [your program] dict value type is 1
[your_program] [your program] type is returned its a 2
[your_program] [your program] decode_string is started
[your_program] [your program] str is name
[your_program] [your_program] dict_key value is name
[your_program] [your program] type is returned its a 2
[your_program] [your program] decode_string is started
[your_program] [your program] str is congratulations.gif
[your_program] [your program] dict value type is 2
[your_program] [your program] type is returned its a 2
[your_program] [your program] decode_string is started
[your_program] [your program] str is piece length
[your_program] [your_program] dict_key value is piece length
[your_program] [your program] type is returned its a 1
[your_program] [your program] decode_integer is started
[your_program] [your program] dict value type is 1
[your_program] [your program] type is returned its a 2
[your_program] [your program] decode_string is started
[your_program] [your program] str is pieces
[your_program] [your_program] dict_key value is pieces
[tester::#OW9] Application didn't terminate successfully without errors. Expecte
d 0 as exit code, got: -1
[tester::#OW9] Test failed (try setting 'debug: true' in your codecrafters.yml t
o see more details)

and now i see what its stucked in bencode.c:208 right before calling realloc,after that error -1. Again i check with this torrent file in my local folder all is good

this with local torrent file like above with name codercat.gif.torrent which i download from bittorrent-test

[pc@pc-asus codecrafters-bittorrent-c]$ ./your_bittorrent.sh info codercat.gif
.torrent 
[your_program] starting
[your_program] buf is writed
[your program] type is returned its a 4
[your program] decode_dict is started
[your program] type is returned its a 2
[your program] decode_string is started
[your program] str is announce
[your_program] dict_key value is announce
[your program] type is returned its a 2
[your program] decode_string is started
[your program] str is http://bittorrent-test-tracker.codecrafters.io/announce
[your program] dict value type is 2
[your program] type is returned its a 2
[your program] decode_string is started
[your program] str is created by
[your_program] dict_key value is created by
[your program] type is returned its a 2
[your program] decode_string is started
[your program] str is mktorrent 1.1
[your program] dict value type is 2
[your program] type is returned its a 2
[your program] decode_string is started
[your program] str is info
[your_program] dict_key value is info
[your program] type is returned its a 4
[your program] decode_dict is started
[your program] type is returned its a 2
[your program] decode_string is started
[your program] str is length
[your_program] dict_key value is length
[your program] type is returned its a 1
[your program] decode_integer is started
[your program] dict value type is 1
[your program] type is returned its a 2
[your program] decode_string is started
[your program] str is name
[your_program] dict_key value is name
[your program] type is returned its a 2
[your program] decode_string is started
[your program] str is codercat.gif
[your program] dict value type is 2
[your program] type is returned its a 2
[your program] decode_string is started
[your program] str is piece length
[your_program] dict_key value is piece length
[your program] type is returned its a 1
[your program] decode_integer is started
[your program] dict value type is 1
[your program] type is returned its a 2
[your program] decode_string is started
[your program] str is pieces
[your_program] dict_key value is pieces
[your program] type is returned its a 2
[your program] decode_string is started
[your program] str is <40���c
                             �~��%�jиQ�.��7?�g�DB�VɊ
[your program] dict value type is 2
[your program] dict value type is 4
[your_program] decoded successfully and 0x5d511a861ab0
[your program] decoded type is 4 and 420 size
announce: http://bittorrent-test-tracker.codecrafters.io/announce
length: 2994120

Ah, thanks for adding detailed logs - this helps!

I’m not a C expert so my analysis here might be wrong, shouldn’t the value for realloc be multiplying by sizeof for the specific type?

I applied this diff:

Removed the debug logs and got tests to this point:

(The values are correct, the output format just needs to be adjusted to match what the tester expects)

Thank you sir, i am sorry for that, i was sure what trouble in the tests not my code, many thanks again

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.