github.com/pawelgaczynski/gain@v0.4.0-alpha.0.20230821120126-41f1e60a18da/util_test.go (about)

     1  // Copyright (c) 2023 Paweł Gaczyński
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gain_test
    16  
    17  import (
    18  	"net"
    19  	"os"
    20  	"reflect"
    21  	"strconv"
    22  	"strings"
    23  	"sync/atomic"
    24  	"syscall"
    25  
    26  	"github.com/pawelgaczynski/gain"
    27  	"github.com/rs/zerolog"
    28  )
    29  
    30  var testLoggerLevel = os.Getenv("TEST_LOGGER_LEVEL")
    31  
    32  func getTestLoggerLevel() zerolog.Level {
    33  	if testLoggerLevel == "" {
    34  		return zerolog.FatalLevel
    35  	}
    36  
    37  	loggerLevel, err := zerolog.ParseLevel(testLoggerLevel)
    38  	if err != nil {
    39  		return zerolog.FatalLevel
    40  	}
    41  
    42  	return loggerLevel
    43  }
    44  
    45  func int8ToStr(arr []int8) string {
    46  	buffer := make([]byte, 0, len(arr))
    47  
    48  	for _, v := range arr {
    49  		if v == 0x00 {
    50  			break
    51  		}
    52  
    53  		buffer = append(buffer, byte(v))
    54  	}
    55  
    56  	return string(buffer)
    57  }
    58  
    59  func checkKernelCompatibility(expectedKernelVersion, expectedMajorVersion int) bool {
    60  	var uname syscall.Utsname
    61  
    62  	err := syscall.Uname(&uname)
    63  	if err != nil {
    64  		return false
    65  	}
    66  
    67  	kernelString := int8ToStr(uname.Release[:])
    68  
    69  	kernelParts := strings.Split(kernelString, ".")
    70  	if len(kernelParts) < 2 {
    71  		return false
    72  	}
    73  
    74  	kernelVersion, err := strconv.Atoi(kernelParts[0])
    75  	if err != nil {
    76  		return false
    77  	}
    78  
    79  	majorVersion, err := strconv.Atoi(kernelParts[1])
    80  	if err != nil {
    81  		return false
    82  	}
    83  
    84  	if expectedKernelVersion < kernelVersion {
    85  		return true
    86  	}
    87  
    88  	if expectedMajorVersion < majorVersion {
    89  		return true
    90  	}
    91  
    92  	return false
    93  }
    94  
    95  var port int32 = 9000
    96  
    97  func getTestPort() int {
    98  	return int(atomic.AddInt32(&port, 1))
    99  }
   100  
   101  type onStartCallback func(server gain.Server, network string)
   102  
   103  type onAcceptCallback func(c gain.Conn, network string)
   104  
   105  type onReadCallback func(c gain.Conn, n int, network string)
   106  
   107  type onWriteCallback func(c gain.Conn, n int, network string)
   108  
   109  type onCloseCallback func(c gain.Conn, err error, network string)
   110  
   111  func getFdFromConn(c net.Conn) int {
   112  	v := reflect.Indirect(reflect.ValueOf(c))
   113  	conn := v.FieldByName("conn")
   114  	netFD := reflect.Indirect(conn.FieldByName("fd"))
   115  	pfd := netFD.FieldByName("pfd")
   116  	fd := int(pfd.FieldByName("Sysfd").Int())
   117  
   118  	return fd
   119  }