Esempio n. 1
0
func TestACKSegment(t *testing.T) {
	assert := assert.On(t)

	seg := &AckSegment{
		Conv:            1,
		ReceivingWindow: 2,
		ReceivingNext:   3,
		Timestamp:       10,
		NumberList:      []uint32{1, 3, 5, 7, 9},
	}

	nBytes := seg.ByteSize()
	bytes := make([]byte, nBytes)
	seg.Bytes()(bytes)

	assert.Int(len(bytes)).Equals(nBytes)

	iseg, _ := ReadSegment(bytes)
	seg2 := iseg.(*AckSegment)
	assert.Uint16(seg2.Conv).Equals(seg.Conv)
	assert.Uint32(seg2.ReceivingWindow).Equals(seg.ReceivingWindow)
	assert.Uint32(seg2.ReceivingNext).Equals(seg.ReceivingNext)
	assert.Int(len(seg2.NumberList)).Equals(len(seg.NumberList))
	assert.Uint32(seg2.Timestamp).Equals(seg.Timestamp)
	for i, number := range seg2.NumberList {
		assert.Uint32(number).Equals(seg.NumberList[i])
	}
}
Esempio n. 2
0
func TestCmdSegment(t *testing.T) {
	assert := assert.On(t)

	seg := &CmdOnlySegment{
		Conv:         1,
		Cmd:          CommandPing,
		Option:       SegmentOptionClose,
		SendingNext:  11,
		ReceivinNext: 13,
		PeerRTO:      15,
	}

	nBytes := seg.ByteSize()
	bytes := make([]byte, nBytes)
	seg.Bytes()(bytes)

	assert.Int(len(bytes)).Equals(nBytes)

	iseg, _ := ReadSegment(bytes)
	seg2 := iseg.(*CmdOnlySegment)
	assert.Uint16(seg2.Conv).Equals(seg.Conv)
	assert.Byte(byte(seg2.Command())).Equals(byte(seg.Command()))
	assert.Byte(byte(seg2.Option)).Equals(byte(seg.Option))
	assert.Uint32(seg2.SendingNext).Equals(seg.SendingNext)
	assert.Uint32(seg2.ReceivinNext).Equals(seg.ReceivinNext)
	assert.Uint32(seg2.PeerRTO).Equals(seg.PeerRTO)
}
Esempio n. 3
0
func Test1ByteDataSegment(t *testing.T) {
	assert := assert.On(t)

	b := buf.NewLocal(512)
	b.AppendBytes('a')
	seg := &DataSegment{
		Conv:        1,
		Timestamp:   3,
		Number:      4,
		SendingNext: 5,
		Data:        b,
	}

	nBytes := seg.ByteSize()
	bytes := make([]byte, nBytes)
	seg.Bytes()(bytes)

	assert.Int(len(bytes)).Equals(nBytes)

	iseg, _ := ReadSegment(bytes)
	seg2 := iseg.(*DataSegment)
	assert.Uint16(seg2.Conv).Equals(seg.Conv)
	assert.Uint32(seg2.Timestamp).Equals(seg.Timestamp)
	assert.Uint32(seg2.SendingNext).Equals(seg.SendingNext)
	assert.Uint32(seg2.Number).Equals(seg.Number)
	assert.Bytes(seg2.Data.Bytes()).Equals(seg.Data.Bytes())
}
Esempio n. 4
0
func TestSendingWindow(t *testing.T) {
	assert := assert.On(t)

	window := NewSendingWindow(5, nil, nil)
	window.Push(0, []byte{})
	window.Push(1, []byte{})
	window.Push(2, []byte{})
	assert.Int(window.Len()).Equals(3)

	window.Remove(1)
	assert.Int(window.Len()).Equals(3)
	assert.Uint32(window.FirstNumber()).Equals(0)

	window.Remove(0)
	assert.Int(window.Len()).Equals(1)
	assert.Uint32(window.FirstNumber()).Equals(2)

	window.Remove(0)
	assert.Int(window.Len()).Equals(0)

	window.Push(4, []byte{})
	assert.Int(window.Len()).Equals(1)
	assert.Uint32(window.FirstNumber()).Equals(4)

	window.Push(5, []byte{})
	assert.Int(window.Len()).Equals(2)

	window.Remove(1)
	assert.Int(window.Len()).Equals(2)

	window.Remove(0)
	assert.Int(window.Len()).Equals(0)
}
Esempio n. 5
0
func TestACKSegment(t *testing.T) {
	assert := assert.On(t)

	seg := &AckSegment{
		Conv:            1,
		ReceivingWindow: 2,
		ReceivingNext:   3,
		Timestamp:       10,
		Count:           5,
		NumberList:      []uint32{1, 3, 5, 7, 9},
	}

	nBytes := seg.ByteSize()
	bytes := seg.Bytes(nil)

	assert.Int(len(bytes)).Equals(nBytes)

	iseg, _ := ReadSegment(bytes)
	seg2 := iseg.(*AckSegment)
	assert.Uint16(seg2.Conv).Equals(seg.Conv)
	assert.Uint32(seg2.ReceivingWindow).Equals(seg.ReceivingWindow)
	assert.Uint32(seg2.ReceivingNext).Equals(seg.ReceivingNext)
	assert.Byte(seg2.Count).Equals(seg.Count)
	assert.Uint32(seg2.Timestamp).Equals(seg.Timestamp)
	for i := byte(0); i < seg2.Count; i++ {
		assert.Uint32(seg2.NumberList[i]).Equals(seg.NumberList[i])
	}
}
Esempio n. 6
0
func TestStringPairPort(t *testing.T) {
	assert := assert.On(t)

	var portRange PortRange
	err := json.Unmarshal([]byte("\"1234-5678\""), &portRange)
	assert.Error(err).IsNil()

	assert.Uint32(portRange.From).Equals(1234)
	assert.Uint32(portRange.To).Equals(5678)
}
Esempio n. 7
0
func TestIntPort(t *testing.T) {
	assert := assert.On(t)

	var portRange PortRange
	err := json.Unmarshal([]byte("1234"), &portRange)
	assert.Error(err).IsNil()

	assert.Uint32(portRange.From).Equals(1234)
	assert.Uint32(portRange.To).Equals(1234)
}
Esempio n. 8
0
func TestTimeOutSettings(t *testing.T) {
	assert := assert.On(t)

	reader := NewTimeOutReader(8, nil)
	assert.Uint32(reader.GetTimeOut()).Equals(8)
	reader.SetTimeOut(8) // no op
	assert.Uint32(reader.GetTimeOut()).Equals(8)
	reader.SetTimeOut(9)
	assert.Uint32(reader.GetTimeOut()).Equals(9)
}
Esempio n. 9
0
func TestSocksInboundConfig(t *testing.T) {
	assert := assert.On(t)

	rawJson := `{
    "auth": "password",
    "accounts": [
      {
        "user": "my-username",
        "pass": "my-password"
      }
    ],
    "udp": false,
    "ip": "127.0.0.1",
    "timeout": 5
  }`

	config := new(SocksServerConfig)
	err := json.Unmarshal([]byte(rawJson), &config)
	assert.Error(err).IsNil()

	message, err := config.Build()
	assert.Error(err).IsNil()

	iConfig, err := message.GetInstance()
	assert.Error(err).IsNil()

	socksConfig := iConfig.(*socks.ServerConfig)
	assert.Bool(socksConfig.AuthType == socks.AuthType_PASSWORD).IsTrue()
	assert.Int(len(socksConfig.Accounts)).Equals(1)
	assert.String(socksConfig.Accounts["my-username"]).Equals("my-password")
	assert.Bool(socksConfig.UdpEnabled).IsFalse()
	assert.Address(socksConfig.Address.AsAddress()).Equals(net.LocalHostIP)
	assert.Uint32(socksConfig.Timeout).Equals(5)
}
Esempio n. 10
0
func TestServerList(t *testing.T) {
	assert := assert.On(t)

	list := NewServerList()
	list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(1)), AlwaysValid()))
	assert.Uint32(list.Size()).Equals(1)
	list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(2)), BeforeTime(time.Now().Add(time.Second))))
	assert.Uint32(list.Size()).Equals(2)

	server := list.GetServer(1)
	assert.Port(server.Destination().Port).Equals(2)
	time.Sleep(2 * time.Second)
	server = list.GetServer(1)
	assert.Pointer(server).IsNil()

	server = list.GetServer(0)
	assert.Port(server.Destination().Port).Equals(1)
}
Esempio n. 11
0
func TestSendingWindow(t *testing.T) {
	assert := assert.On(t)

	window := NewSendingWindow(5, nil, nil)
	window.Push(&DataSegment{
		Number: 0,
	})
	window.Push(&DataSegment{
		Number: 1,
	})
	window.Push(&DataSegment{
		Number: 2,
	})
	assert.Int(window.Len()).Equals(3)

	window.Remove(1)
	assert.Int(window.Len()).Equals(3)
	assert.Uint32(window.First().Number).Equals(0)

	window.Remove(0)
	assert.Int(window.Len()).Equals(1)
	assert.Uint32(window.First().Number).Equals(2)

	window.Remove(0)
	assert.Int(window.Len()).Equals(0)

	window.Push(&DataSegment{
		Number: 4,
	})
	assert.Int(window.Len()).Equals(1)
	assert.Uint32(window.First().Number).Equals(4)

	window.Push(&DataSegment{
		Number: 5,
	})
	assert.Int(window.Len()).Equals(2)

	window.Remove(1)
	assert.Int(window.Len()).Equals(2)

	window.Remove(0)
	assert.Int(window.Len()).Equals(0)
}
Esempio n. 12
0
func TestDataSegment(t *testing.T) {
	assert := assert.On(t)

	seg := &DataSegment{
		Conv:        1,
		Timestamp:   3,
		Number:      4,
		SendingNext: 5,
		Data:        alloc.NewLocalBuffer(512).Clear().Append([]byte{'a', 'b', 'c', 'd'}),
	}

	nBytes := seg.ByteSize()
	bytes := seg.Bytes(nil)

	assert.Int(len(bytes)).Equals(nBytes)

	iseg, _ := ReadSegment(bytes)
	seg2 := iseg.(*DataSegment)
	assert.Uint16(seg2.Conv).Equals(seg.Conv)
	assert.Uint32(seg2.Timestamp).Equals(seg.Timestamp)
	assert.Uint32(seg2.SendingNext).Equals(seg.SendingNext)
	assert.Uint32(seg2.Number).Equals(seg.Number)
	assert.Bytes(seg2.Data.Value).Equals(seg.Data.Value)
}