/ common / utils / utils_test.go
utils_test.go
 1  // Copyright (c) 2024-2026 Tencent Zhuque Lab. All rights reserved.
 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  // Requirement: Any integration or derivative work must explicitly attribute
16  // Tencent Zhuque Lab (https://github.com/Tencent/AI-Infra-Guard) in its
17  // documentation or user interface, as detailed in the NOTICE file.
18  
19  package utils
20  
21  import (
22  	"github.com/Tencent/AI-Infra-Guard/pkg/httpx"
23  	"github.com/hashicorp/go-version"
24  	"github.com/projectdiscovery/fastdialer/fastdialer"
25  	"github.com/stretchr/testify/assert"
26  	"testing"
27  	"time"
28  )
29  
30  func TestIsFileExists(t *testing.T) {
31  	assert.True(t, IsFileExists("/etc/passwd"))
32  	assert.True(t, IsFileExists("/etc/"))
33  	assert.False(t, IsDir("/etc/passwd"))
34  	assert.True(t, IsDir("/etc/"))
35  }
36  
37  func TestCompareVersions(t *testing.T) {
38  	version1 := "1.0.0"
39  	version2 := "1.2"
40  	v := CompareVersions(version1, version2)
41  	t.Log(v)
42  }
43  
44  func TestCompareVersions2(t *testing.T) {
45  	version1 := "2.13"
46  	version2 := "2.13.1"
47  	v1 := version.Must(version.NewVersion(version1))
48  	v2 := version.Must(version.NewVersion(version2))
49  	assert.True(t, v1.LessThan(v2))
50  }
51  
52  func TestFaviconHash(t *testing.T) {
53  	url := "http://127.0.0.1:8265/favicon.ico"
54  	dialer, err := fastdialer.NewDialer(fastdialer.DefaultOptions)
55  	assert.NoError(t, err)
56  	httpOptions := &httpx.HTTPOptions{
57  		Timeout:          time.Duration(30) * time.Second,
58  		RetryMax:         3,
59  		FollowRedirects:  false,
60  		Unsafe:           false,
61  		DefaultUserAgent: httpx.GetRandomUserAgent(),
62  		Dialer:           dialer,
63  	}
64  	hp, err := httpx.NewHttpx(httpOptions)
65  	assert.NoError(t, err)
66  	resp, err := hp.Get(url, nil)
67  	assert.NoError(t, err)
68  	hash := FaviconHash(resp.Data)
69  	t.Log(hash)
70  }
71  
72  func TestGetLocalOpenPorts(t *testing.T) {
73  	op, err := GetLocalOpenPorts()
74  	assert.NoError(t, err)
75  	for _, item := range op {
76  		t.Log(item.Address, item.Port)
77  	}
78  }