...

Source file src/github.com/concurrency-8/parser/parser_test.go

Documentation: github.com/concurrency-8/parser

     1  package parser
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"io/ioutil"
     6  	"math"
     7  	"math/rand"
     8  	"os"
     9  	"regexp"
    10  	"testing"
    11  )
    12  
    13  func getTorrentFiles() ([]os.FileInfo, error) {
    14  	files, err := ioutil.ReadDir("../test_torrents")
    15  	return files, err
    16  }
    17  
    18  func TestParseFromFile(t *testing.T) {
    19  	files, err := getTorrentFiles()
    20  	assert.Nil(t, err, "opening \"test_torrents\" folder failed.")
    21  
    22  	for _, file := range files {
    23  		torfile, err := ParseFromFile("../test_torrents/" + file.Name())
    24  		// check for err!=nil
    25  		assert.Nil(t, err, "Parsing from file failed.")
    26  		// Check for non-empty announce lists
    27  		assert.NotEmpty(t, torfile.Announce, "Empty \"Announce\" list.")
    28  		// There must be atleast one file.
    29  		assert.NotEmpty(t, torfile.Files, "Empty \"File\" list")
    30  		// Length of each file should be positive.
    31  		for _, torsubfile := range torfile.Files {
    32  			assert.True(t, torsubfile.Length > 0, "Negative length for file %s in torrent %s", torsubfile, file.Name())
    33  			assert.NotEmpty(t, torsubfile.Path, "Empty Path for file %s", torsubfile)
    34  		}
    35  		// InfoHash size should be 20 bytes.
    36  		assert.Len(t, torfile.InfoHash, 20, "Corrupt Info Hash file found.")
    37  		// Announce list should consist of valid URLs, i.e. starting with either udp, http or https or wss
    38  		for _, url := range torfile.Announce {
    39  			assert.Regexp(t, regexp.MustCompile("udp://*|http://*|https://*|wss://*"), url, "%s doesn't match any valid tracker format for file %s.", url, file.Name())
    40  		}
    41  		assert.NotEmpty(t, torfile.Length, "Torrent shows empty length.")
    42  
    43  	}
    44  
    45  }
    46  
    47  // TestPieceLen tests PieceLen
    48  func TestPieceLen(t *testing.T) {
    49  	torrent := TorrentFile{}
    50  	torrent.Length = uint64(rand.Intn(100000000))
    51  	torrent.PieceLength = 65536
    52  	lastPieceIndex := uint32(len(torrent.Piece)/20 - 1)
    53  	lastPieceLen := uint32(torrent.Length % uint64(torrent.PieceLength))
    54  
    55  	if lastPieceLen == 0 {
    56  		lastPieceLen = torrent.PieceLength
    57  	}
    58  
    59  	for i := 0; i < 2; i++ {
    60  		index := uint32(rand.Intn(int(2 * lastPieceIndex)))
    61  		length, err := PieceLen(torrent, index)
    62  		if index < lastPieceIndex {
    63  			assert.Equal(t, err, nil, "Error not nil")
    64  			assert.Equal(t, length, torrent.PieceLength, "Piece Length not equal")
    65  		} else if index == lastPieceIndex {
    66  			assert.Equal(t, err, nil, "Error not nil")
    67  			assert.Equal(t, length, lastPieceLen, "Piece Length not equal")
    68  		} else {
    69  			assert.NotEqual(t, err, nil, "For large index length still exits")
    70  		}
    71  	}
    72  }
    73  
    74  // TestBlocksPerPiece tests BlocksPerPiece
    75  func TestBlocksPerPiece(t *testing.T) {
    76  	torrent := TorrentFile{}
    77  	torrent.Length = uint64(rand.Intn(100000000))
    78  	torrent.PieceLength = 65536
    79  	lastPieceIndex := uint32(math.Ceil(float64(torrent.Length/uint64(torrent.PieceLength)))) - 1
    80  
    81  	for i := 0; i < 20; i++ {
    82  		index := uint32(rand.Intn(int(lastPieceIndex)))
    83  		length, err := PieceLen(torrent, index)
    84  		assert.Equal(t, err, nil, "Error not nil")
    85  		blocks, err := BlocksPerPiece(torrent, index)
    86  		assert.Equal(t, err, nil, "Error not nil")
    87  		assert.Equal(t, blocks, uint32(math.Ceil(float64(length)/float64(BLOCK_LEN))), "Block Length not equal")
    88  
    89  	}
    90  }
    91  
    92  // TestBlockLen tests BlockLen
    93  func TestBlockLen(t *testing.T) {
    94  	torrent := TorrentFile{}
    95  	torrent.Length = uint64(rand.Intn(100000000))
    96  	torrent.PieceLength = 65536
    97  	lastPieceIndex := uint32(math.Ceil(float64(torrent.Length/uint64(torrent.PieceLength)))) - 1
    98  	pieceIndex := uint32(rand.Intn(int(lastPieceIndex)))
    99  
   100  	pieceLength, err := PieceLen(torrent, pieceIndex)
   101  	assert.Equal(t, err, nil, "Error not nil")
   102  	lastBlockLength := pieceLength % BLOCK_LEN
   103  	lastBlockIndex := uint32(math.Ceil(float64(pieceLength)/float64(BLOCK_LEN))) - 1
   104  
   105  	if lastBlockLength == 0 {
   106  		lastBlockLength = BLOCK_LEN
   107  	}
   108  
   109  	for i := 0; i < 20; i++ {
   110  		index := uint32(rand.Intn(int(2 * lastBlockIndex)))
   111  		length, err := BlockLen(torrent, pieceIndex, index)
   112  		if index < lastBlockIndex {
   113  			assert.Equal(t, err, nil, "Error not nil")
   114  			assert.Equal(t, length, BLOCK_LEN, "Block Length not equal")
   115  		} else if index == lastBlockIndex {
   116  			assert.Equal(t, err, nil, "Error not nil")
   117  			assert.Equal(t, length, lastBlockLength, "Block Length not equal")
   118  		} else {
   119  			assert.NotEqual(t, err, nil, "For large index length still exits")
   120  		}
   121  	}
   122  
   123  }
   124  

View as plain text