/ common / runner / ipnet_test.go
ipnet_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 runner
 20  
 21  import (
 22  	"testing"
 23  
 24  	"github.com/stretchr/testify/assert"
 25  	"github.com/stretchr/testify/require"
 26  )
 27  
 28  // collectTargets 将 chan string 收集为 []string,便于断言
 29  func collectTargets(ch chan string) []string {
 30  	var result []string
 31  	for s := range ch {
 32  		result = append(result, s)
 33  	}
 34  	return result
 35  }
 36  
 37  // TestTargets_WithSpace 测试包含空格的目标返回空 channel
 38  func TestTargets_WithSpace(t *testing.T) {
 39  	ch := Targets("192.168.1.1 192.168.1.2")
 40  	result := collectTargets(ch)
 41  	// 包含空格时应返回空
 42  	assert.Empty(t, result, "包含空格的目标应返回空结果")
 43  }
 44  
 45  // TestTargets_WithStar 测试包含星号的目标返回空 channel
 46  func TestTargets_WithStar(t *testing.T) {
 47  	ch := Targets("192.168.1.*")
 48  	result := collectTargets(ch)
 49  	// 包含 * 时应返回空
 50  	assert.Empty(t, result, "包含 * 的目标应返回空结果")
 51  }
 52  
 53  // TestTargets_SingleIP 测试单个 IP 直接返回该 IP
 54  func TestTargets_SingleIP(t *testing.T) {
 55  	ch := Targets("192.168.1.1")
 56  	result := collectTargets(ch)
 57  	// 单个 IP 应直接返回
 58  	require.Len(t, result, 1, "单个 IP 应返回一个结果")
 59  	assert.Equal(t, "192.168.1.1", result[0], "返回的 IP 应与输入一致")
 60  }
 61  
 62  // TestTargets_Hostname 测试主机名直接返回
 63  func TestTargets_Hostname(t *testing.T) {
 64  	ch := Targets("example.com")
 65  	result := collectTargets(ch)
 66  	// 非 CIDR、无特殊字符时应直接返回
 67  	require.Len(t, result, 1, "主机名应返回一个结果")
 68  	assert.Equal(t, "example.com", result[0], "返回的主机名应与输入一致")
 69  }
 70  
 71  // TestTargets_CIDR_Small 测试 /30 CIDR 展开 4 个 IP
 72  func TestTargets_CIDR_Small(t *testing.T) {
 73  	ch := Targets("192.168.1.0/30")
 74  	result := collectTargets(ch)
 75  	// /30 包含 4 个 IP(192.168.1.0 - 192.168.1.3)
 76  	assert.Len(t, result, 4, "/30 CIDR 应展开为 4 个 IP")
 77  	assert.Contains(t, result, "192.168.1.0")
 78  	assert.Contains(t, result, "192.168.1.3")
 79  }
 80  
 81  // TestTargets_CIDR_32 测试 /32 CIDR 仅展开单个 IP
 82  func TestTargets_CIDR_32(t *testing.T) {
 83  	ch := Targets("10.0.0.1/32")
 84  	result := collectTargets(ch)
 85  	// /32 应只有 1 个 IP
 86  	require.Len(t, result, 1, "/32 CIDR 应展开为 1 个 IP")
 87  	assert.Equal(t, "10.0.0.1", result[0])
 88  }
 89  
 90  // TestIPAddresses_Valid 测试合法 CIDR 返回正确的 IP 列表
 91  func TestIPAddresses_Valid(t *testing.T) {
 92  	ips, err := IPAddresses("192.168.0.0/30")
 93  	require.NoError(t, err, "合法 CIDR 不应报错")
 94  	// /30 应返回 4 个 IP
 95  	assert.Len(t, ips, 4, "/30 应返回 4 个 IP")
 96  	assert.Equal(t, "192.168.0.0", ips[0], "第一个 IP 应为网络地址")
 97  	assert.Equal(t, "192.168.0.3", ips[3], "最后一个 IP 应为广播地址")
 98  }
 99  
100  // TestIPAddresses_Invalid 测试非法 CIDR 返回错误
101  func TestIPAddresses_Invalid(t *testing.T) {
102  	_, err := IPAddresses("not-a-cidr")
103  	// 非法 CIDR 应返回错误
104  	assert.Error(t, err, "非法 CIDR 应返回错误")
105  }
106  
107  // TestIPAddresses_SingleHost 测试 /32 CIDR 返回单个 IP
108  func TestIPAddresses_SingleHost(t *testing.T) {
109  	ips, err := IPAddresses("172.16.0.5/32")
110  	require.NoError(t, err)
111  	require.Len(t, ips, 1, "/32 应只返回 1 个 IP")
112  	assert.Equal(t, "172.16.0.5", ips[0])
113  }
114  
115  // TestIPAddresses_Slash24 测试 /24 CIDR 返回 256 个 IP
116  func TestIPAddresses_Slash24(t *testing.T) {
117  	ips, err := IPAddresses("10.0.0.0/24")
118  	require.NoError(t, err)
119  	// /24 包含 256 个 IP(.0 - .255)
120  	assert.Len(t, ips, 256, "/24 应返回 256 个 IP")
121  	assert.Equal(t, "10.0.0.0", ips[0])
122  	assert.Equal(t, "10.0.0.255", ips[255])
123  }