...

Source file src/github.com/concurrency-8/tracker/utils_test.go

Documentation: github.com/concurrency-8/tracker

     1  // These are tests for the functions in the file tracker/utils.go
     2  // Run these tests with `go test` in the package directory
     3  
     4  package tracker
     5  
     6  import (
     7  	"bufio"
     8  	"bytes"
     9  	"encoding/binary"
    10  	"fmt"
    11  	"github.com/stretchr/testify/assert"
    12  	"io"
    13  	"math/rand"
    14  	"testing"
    15  )
    16  
    17  func getErrorMsg(varName, functionName string) string {
    18  	return varName + ": not set properly in " + functionName + ". Tip: You might want to check if your network allows torrenting!"
    19  }
    20  
    21  func TestBuildConnReq(t *testing.T) {
    22  	fmt.Print("Testing tracker/utils.go : buildConnReq(): ")
    23  	req := buildConnReq()
    24  	errorMessage := "Invalid Connection Request for tracker"
    25  	assert.Equal(t, req[:12], []byte{0x00, 0x00, 0x04, 0x17, 0x27, 0x10, 0x19, 0x80, 0x00, 0x00, 0x00, 0x00}, errorMessage)
    26  	assert.NotEqual(t, req[:12], []byte{0x01, 0x00, 0x04, 0x17, 0x27, 0x10, 0x19, 0x80, 0x00, 0x00, 0x00, 0x00}, errorMessage)
    27  
    28  	fmt.Println("PASS")
    29  }
    30  
    31  func TestRespType(t *testing.T) {
    32  
    33  	fmt.Print("Testing tracker/utils.go : respType(): ")
    34  
    35  	var mockResponseBuf bytes.Buffer
    36  	writer := bufio.NewWriter(&mockResponseBuf)
    37  
    38  	// Mock response contains only action - announce
    39  	binary.Write(writer, binary.BigEndian, uint32(1))
    40  	writer.Flush()
    41  	assert.Equal(t, respType(mockResponseBuf), "announce", "Unable to detect \"announce\" response when action=1")
    42  
    43  	// Mock connect response
    44  	mockResponseBuf = GetMockConnectResponseBuf(rand.Uint32(), rand.Uint64())
    45  	assert.Equal(t, respType(mockResponseBuf), "connect", "Unable to detect \"connect\" response when action=0")
    46  
    47  	mockResponseBuf.Reset()
    48  
    49  	// Mock response has 16 bytes, first 4 bytes show action - announce
    50  	binary.Write(writer, binary.BigEndian, uint32(1)) // 4 bytes written
    51  	for i := 0; i < 3; i++ {                          // Next 12 bytes = 3 uint32
    52  		binary.Write(writer, binary.BigEndian, uint32(rand.Uint32()))
    53  	}
    54  	writer.Flush()
    55  
    56  	assert.Equal(t, respType(mockResponseBuf), "announce", "Unable to detect \"announce\" response when action=1")
    57  
    58  	fmt.Println("PASS")
    59  }
    60  
    61  func TestParseConnResp(t *testing.T) {
    62  
    63  	fmt.Print("Testing tracker/utils.go : parseConnResp(): ")
    64  	trID := rand.Uint32()
    65  	connID := rand.Uint64()
    66  	mockConnRespBuf := GetMockConnectResponseBuf(trID, connID)
    67  
    68  	mockConnResp := parseConnResp(mockConnRespBuf) // Object of type ConnectResponse
    69  
    70  	assert.Equal(t, mockConnResp.Action, uint32(0), "Action for connect response must be uint32(0)")
    71  	assert.Equal(t, mockConnResp.TransactionID, trID, "Unable to detect transactionID in connection response")
    72  	assert.Equal(t, mockConnResp.ConnectionID, connID, "Unable to detect connectionID in connection response")
    73  	fmt.Println("PASS")
    74  }
    75  
    76  // func GetRandomTorrent() parser.TorrentFile {
    77  // 	return parser.TorrentFile{}
    78  // }
    79  
    80  func TestBuildAnnounceReq(t *testing.T) {
    81  	fmt.Print("Testing tracker/utils.go : buildAnnounceReq(): ")
    82  
    83  	connID := rand.Uint64()
    84  	report := GetRandomClientReport()
    85  
    86  	announceReqBuf, _ := buildAnnounceReq(connID, report)
    87  
    88  	var announceReqReader io.Reader = bytes.NewReader(announceReqBuf.Bytes())
    89  
    90  	// Temporary variables to store data read from generated buffer
    91  	var tempUint64 uint64
    92  	var tempUint32 uint32
    93  	var temp20ByteArr [20]byte
    94  	var tempInt32 int32
    95  	var tempUint16 uint16
    96  
    97  	errorMsg := func(varName string) string {
    98  		return varName + ": not set properly in Announce Request"
    99  	}
   100  
   101  	// connectionID
   102  	binary.Read(announceReqReader, binary.BigEndian, &tempUint64)
   103  	assert.Equal(t, connID, tempUint64, errorMsg("connectionID"))
   104  
   105  	// action
   106  	binary.Read(announceReqReader, binary.BigEndian, &tempUint32)
   107  	assert.Equal(t, uint32(1), tempUint32, errorMsg("action"))
   108  
   109  	// Cannot check for transactionID
   110  	binary.Read(announceReqReader, binary.BigEndian, &tempUint32)
   111  
   112  	// InfoHash
   113  	binary.Read(announceReqReader, binary.BigEndian, &temp20ByteArr)
   114  	var infoHash [20]byte
   115  
   116  	copy(infoHash[:], report.TorrentFile.InfoHash)
   117  	assert.Equal(t, infoHash, temp20ByteArr, errorMsg("torrent.InfoHash"))
   118  
   119  	// Cannot check for peerID
   120  	binary.Read(announceReqReader, binary.BigEndian, &temp20ByteArr)
   121  
   122  	// downloaded
   123  	binary.Read(announceReqReader, binary.BigEndian, &tempUint64)
   124  	assert.Equal(t, uint64(0), tempUint64, errorMsg("downloaded"))
   125  
   126  	// left
   127  	binary.Read(announceReqReader, binary.BigEndian, &tempUint64)
   128  
   129  	assert.Equal(t, report.Left, tempUint64, errorMsg("left"))
   130  	// uploaded
   131  	binary.Read(announceReqReader, binary.BigEndian, &tempUint64)
   132  	assert.Equal(t, uint64(0), tempUint64, errorMsg("uploaded"))
   133  
   134  	// event
   135  	binary.Read(announceReqReader, binary.BigEndian, &tempUint32)
   136  	assert.Equal(t, uint32(0), tempUint32, errorMsg("event"))
   137  
   138  	// Ip address
   139  	binary.Read(announceReqReader, binary.BigEndian, &tempUint32)
   140  	assert.Equal(t, uint32(0), tempUint32, errorMsg("Ip address"))
   141  
   142  	// Cannot check key
   143  	binary.Read(announceReqReader, binary.BigEndian, &tempUint32)
   144  
   145  	// num want
   146  	binary.Read(announceReqReader, binary.BigEndian, &tempInt32)
   147  	assert.Equal(t, int32(-1), tempInt32, errorMsg("num_want"))
   148  
   149  	// port
   150  	binary.Read(announceReqReader, binary.BigEndian, &tempUint16)
   151  	assert.Equal(t, report.Port, tempUint16, errorMsg("port"))
   152  
   153  	fmt.Println("PASS")
   154  }
   155  
   156  func TestParseAnnounceResp(t *testing.T) {
   157  	fmt.Print("Testing tracker/utils.go : parseAnnounceResp(): ")
   158  	transactionID, interval, leechers, seeders := rand.Uint32(), rand.Uint32(), rand.Uint32(), rand.Uint32()
   159  	length := rand.Intn(5)
   160  	peers := make([]Peer, length)
   161  	for i := 0; i < length; i++ {
   162  		peers[i].IPAdress = rand.Uint32()
   163  		peers[i].Port = uint16(rand.Intn(9000) + 1000)
   164  	}
   165  
   166  	mockAnnounceResponseBuf := GetMockAnnounceResponseBuf(transactionID, interval, leechers, seeders, peers)
   167  
   168  	announceResponse := parseAnnounceResp(mockAnnounceResponseBuf)
   169  
   170  	// Checking for parsed parameters
   171  	assert.Equal(t, transactionID, announceResponse.TransactionID, getErrorMsg("transactionID", "TestParseAnnounceResp"))
   172  	assert.Equal(t, interval, announceResponse.Interval, getErrorMsg("interval", "TestParseAnnounceResp"))
   173  	assert.Equal(t, leechers, announceResponse.Leechers, getErrorMsg("leechers", "TestParseAnnounceResponse"))
   174  	assert.Equal(t, seeders, announceResponse.Seeders, getErrorMsg("seeders", "TestParseAnnounceResponse"))
   175  
   176  	// Checking: Number of peers received is same
   177  	assert.Equal(t, len(peers), len(announceResponse.Peers), getErrorMsg("LengthNotEqual", "TestParseAnnounceResponse"))
   178  
   179  	// Checking: every peer created is parsed
   180  	for i := 0; i < length; i++ {
   181  		assert.Equal(t, peers[i].IPAdress, announceResponse.Peers[i].IPAdress, getErrorMsg("IPMismatchInPeers", "TestParseAnnounceResponse"))
   182  		assert.Equal(t, peers[i].Port, announceResponse.Peers[i].Port, getErrorMsg("PortMismatchInPeers", "TestParseAnnounceResponse"))
   183  
   184  	}
   185  
   186  	fmt.Println("PASS")
   187  }
   188  
   189  /*
   190  func TestGetPeers(t *testing.T) {
   191  
   192  	fmt.Print("Testing tracker/utils.go : GetPeers(): ")
   193  
   194  	for _, torrentfileName := range parser.GetTorrentFileList() {
   195  		//torrentfile := getRandomTorrent();
   196  		torrentfile, _ := parser.ParseFromFile(torrentfileName)
   197  		passes := false
   198  		for _, announceUrl := range torrentfile.Announce {
   199  
   200  			u, err := url.Parse(announceUrl)
   201  			if err != nil {
   202  				fmt.Println("\nWarning:", err)
   203  				continue
   204  			}
   205  
   206  			clientReport := GetClientStatusReport(torrentfile, 6881)
   207  
   208  			_, err = GetPeers(u, clientReport)
   209  			if err != nil {
   210  				fmt.Println("\nWarning:", err)
   211  				continue
   212  			}
   213  			passes = true
   214  			break
   215  		}
   216  		assert.Equal(t, true, passes, getErrorMsg("torrentfile", "TestGetPeers"))
   217  	}
   218  	fmt.Println("PASS")
   219  } */
   220  

View as plain text