當前位置: 首頁>>代碼示例>>Golang>>正文


Golang buf.New函數代碼示例

本文整理匯總了Golang中v2ray/com/core/common/buf.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: NewReader

// NewReader creates a new BufferedReader based on an io.Reader.
func NewReader(rawReader io.Reader) *BufferedReader {
	return &BufferedReader{
		reader: rawReader,
		buffer: buf.New(),
		cached: true,
	}
}
開發者ID:v2ray,項目名稱:v2ray-core,代碼行數:8,代碼來源:reader.go

示例2: TestSwitchAccount

func TestSwitchAccount(t *testing.T) {
	assert := assert.On(t)

	sa := &protocol.CommandSwitchAccount{
		Port:     1234,
		ID:       uuid.New(),
		AlterIds: 1024,
		Level:    128,
		ValidMin: 16,
	}

	buffer := buf.New()
	err := MarshalCommand(sa, buffer)
	assert.Error(err).IsNil()

	cmd, err := UnmarshalCommand(1, buffer.BytesFrom(2))
	assert.Error(err).IsNil()

	sa2, ok := cmd.(*protocol.CommandSwitchAccount)
	assert.Bool(ok).IsTrue()
	assert.Pointer(sa.Host).IsNil()
	assert.Pointer(sa2.Host).IsNil()
	assert.Port(sa.Port).Equals(sa2.Port)
	assert.String(sa.ID.String()).Equals(sa2.ID.String())
	assert.Uint16(sa.AlterIds).Equals(sa2.AlterIds)
	assert.Byte(byte(sa.Level)).Equals(byte(sa2.Level))
	assert.Byte(sa.ValidMin).Equals(sa2.ValidMin)
}
開發者ID:v2ray,項目名稱:v2ray-core,代碼行數:28,代碼來源:commands_test.go

示例3: TestBufferedReader

func TestBufferedReader(t *testing.T) {
	assert := assert.On(t)

	content := buf.New()
	content.AppendSupplier(buf.ReadFrom(rand.Reader))

	len := content.Len()

	reader := NewReader(content)
	assert.Bool(reader.Cached()).IsTrue()

	payload := make([]byte, 16)

	nBytes, err := reader.Read(payload)
	assert.Int(nBytes).Equals(16)
	assert.Error(err).IsNil()

	len2 := content.Len()
	assert.Int(len - len2).GreaterThan(16)

	nBytes, err = reader.Read(payload)
	assert.Int(nBytes).Equals(16)
	assert.Error(err).IsNil()

	assert.Int(content.Len()).Equals(len2)
}
開發者ID:v2ray,項目名稱:v2ray-core,代碼行數:26,代碼來源:reader_test.go

示例4: Read

func (v *ChunkReader) Read() (*buf.Buffer, error) {
	buffer := buf.New()
	if err := buffer.AppendSupplier(buf.ReadFullFrom(v.reader, 2)); err != nil {
		buffer.Release()
		return nil, err
	}
	// There is a potential buffer overflow here. Large buffer is 64K bytes,
	// while uin16 + 10 will be more than that
	length := serial.BytesToUint16(buffer.BytesTo(2)) + AuthSize
	if length > buf.Size {
		// Theoretically the size of a chunk is 64K, but most Shadowsocks implementations used <4K buffer.
		buffer.Release()
		buffer = buf.NewLocal(int(length) + 128)
	}

	buffer.Clear()
	if err := buffer.AppendSupplier(buf.ReadFullFrom(v.reader, int(length))); err != nil {
		buffer.Release()
		return nil, err
	}

	authBytes := buffer.BytesTo(AuthSize)
	payload := buffer.BytesFrom(AuthSize)

	actualAuthBytes := make([]byte, AuthSize)
	v.auth.Authenticate(payload)(actualAuthBytes)
	if !bytes.Equal(authBytes, actualAuthBytes) {
		buffer.Release()
		return nil, errors.New("Shadowsocks|AuthenticationReader: Invalid auth.")
	}
	buffer.SliceFrom(AuthSize)

	return buffer, nil
}
開發者ID:v2ray,項目名稱:v2ray-core,代碼行數:34,代碼來源:ota.go

示例5: TestWrongProtocolVersion

func TestWrongProtocolVersion(t *testing.T) {
	assert := assert.On(t)

	buffer := buf.New()
	buffer.AppendBytes(6, 1, 0)
	_, _, err := ReadAuthentication(buffer)
	assert.Error(err).Equals(proxy.ErrInvalidProtocolVersion)
}
開發者ID:v2ray,項目名稱:v2ray-core,代碼行數:8,代碼來源:socks_test.go

示例6: TestZeroAuthenticationMethod

func TestZeroAuthenticationMethod(t *testing.T) {
	assert := assert.On(t)

	buffer := buf.New()
	buffer.AppendBytes(5, 0)
	_, _, err := ReadAuthentication(buffer)
	assert.Error(err).Equals(crypto.ErrAuthenticationFailed)
}
開發者ID:v2ray,項目名稱:v2ray-core,代碼行數:8,代碼來源:socks_test.go

示例7: TestShadowsocksTCP

func TestShadowsocksTCP(t *testing.T) {
	assert := assert.On(t)

	tcpServer := &tcp.Server{
		Port: v2net.Port(50052),
		MsgProcessor: func(data []byte) []byte {
			buffer := make([]byte, 0, 2048)
			buffer = append(buffer, []byte("Processed: ")...)
			buffer = append(buffer, data...)
			return buffer
		},
	}
	_, err := tcpServer.Start()
	assert.Error(err).IsNil()
	defer tcpServer.Close()

	assert.Error(InitializeServerSetOnce("test_6")).IsNil()

	for i := 0; i < 1; i++ {
		conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
			IP:   []byte{127, 0, 0, 1},
			Port: 50050,
		})
		assert.Error(err).IsNil()

		payload := "dokodemo request."
		nBytes, err := conn.Write([]byte(payload))
		assert.Error(err).IsNil()
		assert.Int(nBytes).Equals(len(payload))

		//conn.CloseWrite()

		response := buf.New()
		finished := false
		expectedResponse := "Processed: " + payload
		for {
			err := response.AppendSupplier(buf.ReadFrom(conn))
			assert.Error(err).IsNil()
			if err != nil {
				break
			}
			if response.String() == expectedResponse {
				finished = true
				break
			}
			if response.Len() > len(expectedResponse) {
				fmt.Printf("Unexpected response: %v\n", response.Bytes())
				break
			}
		}
		assert.Bool(finished).IsTrue()

		conn.Close()
	}

	CloseAllServers()
}
開發者ID:v2ray,項目名稱:v2ray-core,代碼行數:57,代碼來源:shadowsocks_test.go

示例8: TestUDPReaderWriter

func TestUDPReaderWriter(t *testing.T) {
	assert := assert.On(t)

	user := &protocol.User{
		Account: serial.ToTypedMessage(&Account{
			Password:   "test-password",
			CipherType: CipherType_CHACHA20_IEFT,
		}),
	}
	cache := buf.New()
	writer := &UDPWriter{
		Writer: cache,
		Request: &protocol.RequestHeader{
			Version: Version,
			Address: v2net.DomainAddress("v2ray.com"),
			Port:    123,
			User:    user,
			Option:  RequestOptionOneTimeAuth,
		},
	}

	reader := &UDPReader{
		Reader: cache,
		User:   user,
	}

	b := buf.New()
	b.AppendSupplier(serial.WriteString("test payload"))
	err := writer.Write(b)
	assert.Error(err).IsNil()

	payload, err := reader.Read()
	assert.Error(err).IsNil()
	assert.String(payload.String()).Equals("test payload")

	b = buf.New()
	b.AppendSupplier(serial.WriteString("test payload 2"))
	err = writer.Write(b)
	assert.Error(err).IsNil()

	payload, err = reader.Read()
	assert.Error(err).IsNil()
	assert.String(payload.String()).Equals("test payload 2")
}
開發者ID:v2ray,項目名稱:v2ray-core,代碼行數:44,代碼來源:protocol_test.go

示例9: TestIPv6Request

func TestIPv6Request(t *testing.T) {
	assert := assert.On(t)

	b := buf.New()
	b.AppendBytes(5, 1, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 8)
	request, err := ReadRequest(b)
	assert.Error(err).IsNil()
	assert.Byte(request.Command).Equals(1)
	assert.Bytes(request.IPv6[:]).Equals([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6})
	assert.Port(request.Port).Equals(8)
}
開發者ID:v2ray,項目名稱:v2ray-core,代碼行數:11,代碼來源:socks_test.go

示例10: TestStreamIO

func TestStreamIO(t *testing.T) {
	assert := assert.On(t)

	stream := NewStream(context.Background())
	b1 := buf.New()
	b1.AppendBytes('a')
	assert.Error(stream.Write(b1)).IsNil()

	_, err := stream.Read()
	assert.Error(err).IsNil()

	stream.Close()
	_, err = stream.Read()
	assert.Error(err).Equals(io.EOF)

	b2 := buf.New()
	b2.AppendBytes('b')
	err = stream.Write(b2)
	assert.Error(err).Equals(io.ErrClosedPipe)
}
開發者ID:ylywyn,項目名稱:v2ray-core,代碼行數:20,代碼來源:direct_test.go

示例11: TestNormalChunkReading

func TestNormalChunkReading(t *testing.T) {
	assert := assert.On(t)

	buffer := buf.New()
	buffer.AppendBytes(
		0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18)
	reader := NewChunkReader(buffer, NewAuthenticator(ChunkKeyGenerator(
		[]byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36})))
	payload, err := reader.Read()
	assert.Error(err).IsNil()
	assert.Bytes(payload.Bytes()).Equals([]byte{11, 12, 13, 14, 15, 16, 17, 18})
}
開發者ID:v2ray,項目名稱:v2ray-core,代碼行數:12,代碼來源:ota_test.go

示例12: TestUDPEncoding

func TestUDPEncoding(t *testing.T) {
	assert := assert.On(t)

	b := buf.New()

	request := &protocol.RequestHeader{
		Address: net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}),
		Port:    1024,
	}
	writer := NewUDPWriter(request, b)

	content := []byte{'a'}
	payload := buf.New()
	payload.Append(content)
	assert.Error(writer.Write(payload)).IsNil()

	reader := NewUDPReader(b)

	decodedPayload, err := reader.Read()
	assert.Error(err).IsNil()
	assert.Bytes(decodedPayload.Bytes()).Equals(content)
}
開發者ID:ylywyn,項目名稱:v2ray-core,代碼行數:22,代碼來源:protocol_test.go

示例13: TestAuthenticationRequestRead

func TestAuthenticationRequestRead(t *testing.T) {
	assert := assert.On(t)

	buffer := buf.New()
	buffer.AppendBytes(
		0x05, // version
		0x01, // nMethods
		0x02, // methods
	)
	request, _, err := ReadAuthentication(buffer)
	assert.Error(err).IsNil()
	assert.Byte(request.version).Equals(0x05)
	assert.Byte(request.nMethods).Equals(0x01)
	assert.Byte(request.authMethods[0]).Equals(0x02)
}
開發者ID:v2ray,項目名稱:v2ray-core,代碼行數:15,代碼來源:socks_test.go

示例14: TestReaderWriter

func TestReaderWriter(t *testing.T) {
	assert := assert.On(t)

	cache := buf.New()
	b := buf.NewLocal(256)
	b.AppendSupplier(serial.WriteString("abcd" + ENDING))
	writer := NewHeaderWriter(b)
	err := writer.Write(cache)
	assert.Error(err).IsNil()
	assert.Int(cache.Len()).Equals(8)
	_, err = cache.Write([]byte{'e', 'f', 'g'})
	assert.Error(err).IsNil()

	reader := &HeaderReader{}
	buffer, err := reader.Read(cache)
	assert.Error(err).IsNil()
	assert.Bytes(buffer.Bytes()).Equals([]byte{'e', 'f', 'g'})
}
開發者ID:ylywyn,項目名稱:v2ray-core,代碼行數:18,代碼來源:http_test.go

示例15: TestBufferedWriter

func TestBufferedWriter(t *testing.T) {
	assert := assert.On(t)

	content := buf.New()

	writer := NewWriter(content)
	assert.Bool(writer.IsBuffered()).IsTrue()

	payload := make([]byte, 16)

	nBytes, err := writer.Write(payload)
	assert.Int(nBytes).Equals(16)
	assert.Error(err).IsNil()

	assert.Bool(content.IsEmpty()).IsTrue()

	writer.SetBuffered(false)
	assert.Int(content.Len()).Equals(16)
}
開發者ID:ylywyn,項目名稱:v2ray-core,代碼行數:19,代碼來源:writer_test.go


注:本文中的v2ray/com/core/common/buf.New函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。