コード例 #1
0
ファイル: packet_test.go プロジェクト: himanshuo/tftp
func TestBytesToAckPacket(t *testing.T) {
	for i, test := range testAckPacket {
		ret := packet.ToPacket(test.bitVersion)
		if !reflect.DeepEqual(ret, test.packetVersion) {
			t.Errorf("Failed Test %d: AckPacket: toPacket(%v)=%v DESIRED: %v", i, test.bitVersion, ret, test.packetVersion)
		}
	}
}

//test ErrorPacket conversion to bytes
var testErrorPacket = []struct {
	packetVersion packet.ErrorPacket
	bitVersion    []byte
	err           bool
}{
	{packet.NewErrorPacket(uint16(0), "a"), stringToBytes("\x00" + string(packet.ERROR) + "\x00\x00" + "a" + "\x00"), false},                           //0
	{packet.NewErrorPacket(uint16(1), "a"), stringToBytes("\x00" + string(packet.ERROR) + "\x00\x01" + "a" + "\x00"), false},                           //1
	{packet.NewErrorPacket(uint16(2), "a"), stringToBytes("\x00" + string(packet.ERROR) + "\x00\x02" + "a" + "\x00"), false},                           //2
	{packet.NewErrorPacket(uint16(3), "a"), stringToBytes("\x00" + string(packet.ERROR) + "\x00\x03" + "a" + "\x00"), false},                           //3
	{packet.NewErrorPacket(uint16(4), "a"), stringToBytes("\x00" + string(packet.ERROR) + "\x00\x04" + "a" + "\x00"), false},                           //4
	{packet.NewErrorPacket(uint16(5), "a"), stringToBytes("\x00" + string(packet.ERROR) + "\x00\x05" + "a" + "\x00"), false},                           //5
	{packet.NewErrorPacket(uint16(6), "a"), stringToBytes("\x00" + string(packet.ERROR) + "\x00\x06" + "a" + "\x00"), false},                           //6
	{packet.NewErrorPacket(uint16(7), "a"), stringToBytes("\x00" + string(packet.ERROR) + "\x00\x07" + "a" + "\x00"), false},                           //7
	{packet.NewErrorPacket(uint16(2), string(ALLBYTES)), stringToBytes("\x00" + string(packet.ERROR) + "\x00\x02" + string(ALLBYTES) + "\x00"), false}, //max bytes error string
	{packet.NewErrorPacket(uint16(2), ""), stringToBytes("\x00" + string(packet.ERROR) + "\x00\x02\x00"), false},                                       //empty error string

}

func TestErrorPacket(t *testing.T) {
	for i, test := range testErrorPacket {
		ret := test.packetVersion.ToBytes()
コード例 #2
0
ファイル: transport.go プロジェクト: himanshuo/tftp
func SendErrorPacket(errNum uint16, conn *net.UDPConn, clientAddr *net.UDPAddr) {
	errorPacket := packet.NewErrorPacket(errNum, packet.ErrorCodes[errNum])
	conn.WriteToUDP(errorPacket.ToBytes(), clientAddr)
}