Source

gotftp / gotftp_test.go

package main

import (
	"bytes"
	"testing"
)

func TestChunk(t *testing.T) {
	c := new(TftpChunk)
	c.id = 31
	c.data = []byte{1, 2, 3, 4, 5}

	// test packet length
	if c.Len() != 5 {
		t.Fail()
	}

	p := c.Packet()
	// test opcode
	if toUint16(p[0], p[1]) != DATA {
		t.Fail()
	}
	// test id
	if toUint16(p[2], p[3]) != 31 {
		t.Fail()
	}
	// test data
	if bytes.Compare(p[4:], c.data) != 0 {
		t.Fail()
	}
}

func TestFile(t *testing.T) {
	data := bytes.NewBuffer([]byte{1, 2, 3})
	f, err := NewTftpFile(data, DEFAULT_CHUNK_SIZE)
	if err != nil {
		t.Fail()
	}
	if len(f.chunks) != 1 {
		t.Fail()
	}
	if f.chunks[0].Len() != 3 {
		t.Fail()
	}
}

func Test4KFile(t *testing.T) {
	block := make([]byte, 4096)
	for i := 0; i < 4096; i++ {
		block[i] = byte(i & 0xff)
	}
	data := bytes.NewBuffer(block)
	f, err := NewTftpFile(data, DEFAULT_CHUNK_SIZE)
	if err != nil {
		t.Fail()
	}
	if len(f.chunks) != 9 {
		t.Fail()
	}
	if f.chunks[8].Len() != 0 {
		t.Fail()
	}

	f.AckChunk(0)
	f.AckChunk(2)
	f.AckChunk(4)
	f.AckChunk(6)

	if _, ok := f.GetNextChunk(); !ok {
		t.Fail()
	}

	f.AckChunk(1)
	f.AckChunk(3)
	f.AckChunk(5)
	f.AckChunk(7)

	if _, ok := f.GetNextChunk(); !ok {
		t.Fail()
	}

	f.AckChunk(8)
	if _, ok := f.GetNextChunk(); ok {
		t.Fail()
	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.