Al-HUWAITI Shell
Al-huwaiti


Server : LiteSpeed
System : Linux in-mum-web1112.main-hosting.eu 4.18.0-553.34.1.lve.el8.x86_64 #1 SMP Thu Jan 9 16:30:32 UTC 2025 x86_64
User : u451330669 ( 451330669)
PHP Version : 8.2.27
Disable Function : NONE
Directory :  /opt/go/pkg/mod/github.com/prometheus/alertmanager@v0.26.0/cluster/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //opt/go/pkg/mod/github.com/prometheus/alertmanager@v0.26.0/cluster/tls_connection_test.go
// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cluster

import (
	"errors"
	"net"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestWriteStream(t *testing.T) {
	w, r := net.Pipe()
	conn := &tlsConn{
		connection: w,
	}
	defer r.Close()
	go func() {
		conn.writeStream()
		w.Close()
	}()
	packet, err := rcvTLSConn(r).read()
	require.Nil(t, err)
	require.Nil(t, packet)
}

func TestWritePacket(t *testing.T) {
	testCases := []struct {
		fromAddr string
		msg      string
	}{
		{fromAddr: "127.0.0.1:8001", msg: ""},
		{fromAddr: "10.0.0.4:9094", msg: "hello"},
		{fromAddr: "127.0.0.1:8001", msg: "0"},
	}
	for _, tc := range testCases {
		w, r := net.Pipe()
		defer r.Close()
		go func() {
			conn := &tlsConn{connection: w}
			conn.writePacket(tc.fromAddr, []byte(tc.msg))
			w.Close()
		}()
		packet, err := rcvTLSConn(r).read()
		require.Nil(t, err)
		require.Equal(t, tc.msg, string(packet.Buf))
		require.Equal(t, tc.fromAddr, packet.From.String())

	}
}

func TestRead_Nil(t *testing.T) {
	packet, err := (&tlsConn{}).read()
	require.Nil(t, packet)
	require.NotNil(t, err)
}

func TestTLSConn_Close(t *testing.T) {
	testCases := []string{
		"foo",
		"bar",
	}
	for _, tc := range testCases {
		c := &tlsConn{
			connection: &mockConn{
				errMsg: tc,
			},
			live: true,
		}
		err := c.Close()
		require.Equal(t, errors.New(tc), err, tc)
		require.False(t, c.alive())
		require.True(t, c.connection.(*mockConn).closed)
	}
}

type mockConn struct {
	closed bool
	errMsg string
}

func (m *mockConn) Read(b []byte) (n int, err error) {
	panic("implement me")
}

func (m *mockConn) Write(b []byte) (n int, err error) {
	panic("implement me")
}

func (m *mockConn) Close() error {
	m.closed = true
	return errors.New(m.errMsg)
}

func (m *mockConn) LocalAddr() net.Addr {
	panic("implement me")
}

func (m *mockConn) RemoteAddr() net.Addr {
	panic("implement me")
}

func (m *mockConn) SetDeadline(t time.Time) error {
	panic("implement me")
}

func (m *mockConn) SetReadDeadline(t time.Time) error {
	panic("implement me")
}

func (m *mockConn) SetWriteDeadline(t time.Time) error {
	panic("implement me")
}

Al-HUWAITI Shell