Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed Aug 24, 2023
2 parents 64b34df + ba42d58 commit 8575171
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 127 deletions.
89 changes: 9 additions & 80 deletions internal/stage_dl_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,96 +7,26 @@ import (
)

func testDownloadFile(stageHarness *tester_utils.StageHarness) error {
torrentFile := "alpine-minirootfs-3.18.3-aarch64.tar.gz.torrent"
expectedFilename := "alpine-minirootfs-3.18.3-aarch64.tar.gz"
expectedSha1 := "e3fae2149ee08c75ced032561ea8ee5bd9b01d14"
expectedFileSize := int64(3201634)
initRandom()

logger := stageHarness.Logger
executable := stageHarness.Executable

tempDir, err := createTempDir(executable)
if err != nil {
logger.Errorf("Couldn't create temp directory")
return err
}

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

logger.Debugf("Running ./your_bittorrent.sh download -o %s %s", expectedFilename, torrentFile)
result, err := executable.Run("download", "-o", expectedFilename, torrentFile)
if err != nil {
return err
}

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

downloadedFilePath := path.Join(tempDir, expectedFilename)
if err = assertFileSize(downloadedFilePath, expectedFileSize); err != nil {
return err
}

if err = assertFileSHA1(downloadedFilePath, expectedSha1); err != nil {
return err
}

return nil
}

/*
// Uncomment to test using private tracker.
// Make sure there's an image in this repo named codecrafters.jpeg
func testDownloadFile(stageHarness *tester_utils.StageHarness) error {
fileName := "codecrafters.jpeg"
imgFilePath := path.Join(getProjectDir(), fileName)
logger := stageHarness.Logger
executable := stageHarness.Executable
t := randomTorrent()

tempDir, err := createTempDir(executable)
if err != nil {
logger.Errorf("Couldn't create temp directory")
return err
}

torrentFile := "test.torrent"
torrentFilePath := path.Join(tempDir, torrentFile)
pieceLengthBytes := 256 * 1024
expectedSha1, err := calculateSHA1(imgFilePath)
if err != nil {
return err
}
piecesStr, err := createPiecesStrFromFile(imgFilePath, pieceLengthBytes)
if err != nil {
return err
}
fileLengthBytes, err := getFileSizeBytes(imgFilePath)
if err != nil {
return err
}
torrent := TorrentFile{
Announce: "http://bittorrent-test-tracker.codecrafters.io/announce",
Info: TorrentFileInfo{
Name: fileName,
Length: int(fileLengthBytes),
Pieces: piecesStr,
PieceLength: pieceLengthBytes,
},
}
_, err = torrent.writeToFile(torrentFilePath)
if err != nil {
logger.Errorf("Couldn't write torrent file", err)
if err := copyTorrent(tempDir, t.filename); err != nil {
logger.Errorf("Couldn't copy torrent file")
return err
}

logger.Debugf("Running ./your_bittorrent.sh download -o %s %s", fileName, torrentFile)
result, err := executable.Run("download", "-o", fileName, torrentFile)
logger.Debugf("Running ./your_bittorrent.sh download -o %s %s", t.outputFilename, t.filename)
result, err := executable.Run("download", "-o", t.outputFilename, t.filename)
if err != nil {
return err
}
Expand All @@ -105,15 +35,14 @@ func testDownloadFile(stageHarness *tester_utils.StageHarness) error {
return err
}

downloadedFilePath := path.Join(tempDir, fileName)
if err = assertFileSize(downloadedFilePath, fileLengthBytes); err != nil {
downloadedFilePath := path.Join(tempDir, t.outputFilename)
if err = assertFileSize(downloadedFilePath, t.length); err != nil {
return err
}

if err = assertFileSHA1(downloadedFilePath, expectedSha1); err != nil {
if err = assertFileSHA1(downloadedFilePath, t.expectedSha1); err != nil {
return err
}

return nil
}
*/
51 changes: 40 additions & 11 deletions internal/stage_dl_piece.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
package internal

import (
"fmt"
"math/rand"
"path"

tester_utils "github.com/codecrafters-io/tester-utils"
)

// TODO: Use private tracker and own torrents
type DownloadPieceTest struct {
torrentFilename string
pieceIndex int
pieceHash string
pieceLength int64
}

var downloadPieceTests = []DownloadPieceTest{
{
torrentFilename: "congratulations.gif.torrent",
pieceIndex: 3,
pieceHash: "bded68d02de011a2b687f75b5833f46cce8e3e9c",
pieceLength: 34460,
},
{
torrentFilename: "itsworking.gif.torrent",
pieceIndex: 1,
pieceHash: "838f703cf7f6f08d1c497ed390df78f90d5f7566",
pieceLength: 262144,
},
{
torrentFilename: "codercat.gif.torrent",
pieceIndex: 0,
pieceHash: "3c34309faebf01e49c0f63c90b7edcc2259b6ad0",
pieceLength: 262144,
},
}

func testDownloadPiece(stageHarness *tester_utils.StageHarness) error {
torrentFile := "test.torrent"
pieceIndex := "0"
expectedFilename := "test-iso-piece-0"
expectedSha1 := "ddf33172599fda84f0a209a3034f79f0b8aa5e22"
expectedFileSize := int64(262144)
initRandom()

randomIndex := rand.Intn(len(downloadPieceTests))
t := downloadPieceTests[randomIndex]

logger := stageHarness.Logger
executable := stageHarness.Executable
Expand All @@ -23,13 +51,14 @@ func testDownloadPiece(stageHarness *tester_utils.StageHarness) error {
return err
}

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

logger.Debugf("Running ./your_bittorrent.sh download_piece -o %s %s %s", expectedFilename, torrentFile, pieceIndex)
result, err := executable.Run("download_piece", "-o", expectedFilename, torrentFile, pieceIndex)
expectedFilename := fmt.Sprintf("piece-%d", t.pieceIndex)
logger.Debugf("Running ./your_bittorrent.sh download_piece -o %s %s %d", expectedFilename, t.torrentFilename, t.pieceIndex)
result, err := executable.Run("download_piece", "-o", expectedFilename, t.torrentFilename, fmt.Sprintf("%d", t.pieceIndex))
if err != nil {
return err
}
Expand All @@ -39,11 +68,11 @@ func testDownloadPiece(stageHarness *tester_utils.StageHarness) error {
}

downloadedFilePath := path.Join(tempDir, expectedFilename)
if err = assertFileSize(downloadedFilePath, expectedFileSize); err != nil {
if err = assertFileSize(downloadedFilePath, t.pieceLength); err != nil {
return err
}

if err = assertFileSHA1(downloadedFilePath, expectedSha1); err != nil {
if err = assertFileSHA1(downloadedFilePath, t.pieceHash); err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions internal/stage_handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"math/rand"
"net"
"path"
"time"

tester_utils "github.com/codecrafters-io/tester-utils"
)

func testHandshake(stageHarness *tester_utils.StageHarness) error {
initRandom()

logger := stageHarness.Logger
executable := stageHarness.Executable

Expand Down Expand Up @@ -75,7 +76,6 @@ func testHandshake(stageHarness *tester_utils.StageHarness) error {
}

func randomHash() ([20]byte, error) {
rand.Seed(time.Now().UnixNano())
var hash [20]byte
if _, err := rand.Read(hash[:]); err != nil {
return [20]byte{}, err
Expand Down Expand Up @@ -115,6 +115,6 @@ func handleConnection(conn net.Conn, myPeerID [20]byte, infoHash [20]byte, logge
}

logger.Debugf("Received handshake: [infohash: %x, peer_id: %x]\n", handshake.InfoHash, handshake.PeerID)
logger.Debugf("Sending back handshake")
logger.Debugf("Sending back handshake with peer_id: %x", myPeerID)
sendHandshake(conn, handshake.InfoHash, myPeerID)
}
48 changes: 24 additions & 24 deletions internal/stage_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net"
"os"
"path"
"time"

tester_utils "github.com/codecrafters-io/tester-utils"
)
Expand All @@ -25,36 +24,38 @@ var samplePieceHashes = []string{
}

type TestTorrentInfo struct {
filename string
tracker string
length string
infohash string
filename string
outputFilename string
tracker string
infohash string
length int64
expectedSha1 string
}

var testTorrents = []TestTorrentInfo{
{
filename: "test.torrent",
tracker: "http://linuxtracker.org:2710/00000000000000000000000000000000/announce",
length: "406847488",
infohash: "6d4795dee70aeb88e03e5336ca7c9fcf0a1e206d",
filename: "codercat.gif.torrent",
outputFilename: "codercat.gif",
tracker: "http://bittorrent-test-tracker.codecrafters.io/announce",
infohash: "c77829d2a77d6516f88cd7a3de1a26abcbfab0db",
length: 2994120,
expectedSha1: "89d5dcbb92d31f040f8fe42b559fd6ec7f4d83a5",
},
{
filename: "debian-9.1.0-amd64-netinst.iso.torrent",
tracker: "http://bttracker.debian.org:6969/announce",
length: "304087040",
infohash: "fd5fdf21aef4505451861da97aa39000ed852988",
filename: "congratulations.gif.torrent",
outputFilename: "congratulations.gif",
tracker: "http://bittorrent-test-tracker.codecrafters.io/announce",
infohash: "1cad4a486798d952614c394eb15e75bec587fd08",
length: 820892,
expectedSha1: "fe3cc9002bc84c4776c3a962a717244c9cb962c0",
},
{
filename: "debian-10.8.0-amd64-netinst.iso.torrent",
tracker: "http://bttracker.debian.org:6969/announce",
length: "352321536",
infohash: "4090c3c2a394a49974dfbbf2ce7ad0db3cdeddd7",
},
{
filename: "alpine-minirootfs-3.18.3-aarch64.tar.gz.torrent",
tracker: "http://tracker.openbittorrent.com:80/announce",
length: "3201634",
infohash: "0307870997cb1ae741431cfc54f3b2dcf52e2c80",
filename: "itsworking.gif.torrent",
outputFilename: "itsworking.gif",
tracker: "http://bittorrent-test-tracker.codecrafters.io/announce",
infohash: "70edcac2611a8829ebf467a6849f5d8408d9d8f4",
length: 2549700,
expectedSha1: "683e899db9d7a38e50eb87874b62f7fdd0c14c9c",
},
}

Expand All @@ -77,7 +78,6 @@ func copyTorrent(tempDir string, torrentFilename string) error {
}

func randomTorrent() TestTorrentInfo {
rand.Seed(time.Now().UnixNano())
return testTorrents[rand.Intn(len(testTorrents))]
}

Expand Down
4 changes: 3 additions & 1 deletion internal/stage_infohash.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func testInfoHash(stageHarness *tester_utils.StageHarness) error {
initRandom()

logger := stageHarness.Logger
executable := stageHarness.Executable
torrent := randomTorrent()
Expand Down Expand Up @@ -35,7 +37,7 @@ func testInfoHash(stageHarness *tester_utils.StageHarness) error {

expected := strings.Join([]string{
fmt.Sprintf("Tracker URL: %s", torrent.tracker),
fmt.Sprintf("Length: %s", torrent.length),
fmt.Sprintf("Length: %d", torrent.length),
fmt.Sprintf("Info Hash: %s", torrent.infohash)}, "\n") + "\n"

if err = assertStdoutContains(result, expected); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/stage_parse_torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package internal

import (
"fmt"
"os"
"strings"

tester_utils "github.com/codecrafters-io/tester-utils"
)

func testParseTorrent(stageHarness *tester_utils.StageHarness) error {
initRandom()

logger := stageHarness.Logger
executable := stageHarness.Executable
torrent := randomTorrent()
Expand All @@ -20,7 +21,6 @@ func testParseTorrent(stageHarness *tester_utils.StageHarness) error {
}

if err := copyTorrent(tempDir, torrent.filename); err != nil {
logger.Errorf("TESTER DIR: %s", os.Getenv("TESTER_DIR"))
logger.Errorf("Couldn't copy torrent file")
return err
}
Expand All @@ -37,7 +37,7 @@ func testParseTorrent(stageHarness *tester_utils.StageHarness) error {

expected := strings.Join([]string{
fmt.Sprintf("Tracker URL: %s", torrent.tracker),
fmt.Sprintf("Length: %s", torrent.length)}, "\n") + "\n"
fmt.Sprintf("Length: %d", torrent.length)}, "\n") + "\n"

if err = assertStdoutContains(result, expected); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions internal/stage_peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"os"
"path"
"time"

tester_utils "github.com/codecrafters-io/tester-utils"
)
Expand Down Expand Up @@ -60,11 +59,12 @@ var discoverPeersResponses = []DiscoverPeersTestCase{
}

func randomResponse() DiscoverPeersTestCase {
rand.Seed(time.Now().UnixNano())
return discoverPeersResponses[rand.Intn(len(discoverPeersResponses))]
}

func testDiscoverPeers(stageHarness *tester_utils.StageHarness) error {
initRandom()

logger := stageHarness.Logger
executable := stageHarness.Executable

Expand Down
Loading

0 comments on commit 8575171

Please sign in to comment.