mazepa~ linux userland keylogger |
| git clone https://github.com/djnnvx/mazepa.git |
| log | files | refs | archive | README |
pkg_test.go (5727B)
1 package icmp 2 3 import ( 4 "net" 5 "testing" 6 7 "golang.org/x/net/icmp" 8 "golang.org/x/net/ipv4" 9 "golang.org/x/net/ipv6" 10 ) 11 12 func TestDetectProtocol(t *testing.T) { 13 tests := []struct { 14 name string 15 addr string 16 expected Protocol 17 }{ 18 {"IPv4 localhost", "127.0.0.1", ProtocolIPv4}, 19 {"IPv4 any", "0.0.0.0", ProtocolIPv4}, 20 {"IPv4 public", "8.8.8.8", ProtocolIPv4}, 21 {"IPv6 localhost", "::1", ProtocolIPv6}, 22 {"IPv6 any", "::", ProtocolIPv6}, 23 {"IPv6 full", "2001:4860:4860::8888", ProtocolIPv6}, 24 {"IPv6 compressed", "fe80::1", ProtocolIPv6}, 25 {"invalid address", "invalid", ProtocolIPv4}, 26 {"empty string", "", ProtocolIPv4}, 27 } 28 29 for _, tt := range tests { 30 t.Run(tt.name, func(t *testing.T) { 31 result := DetectProtocol(tt.addr) 32 if result != tt.expected { 33 t.Errorf("DetectProtocol(%q) = %v, want %v", tt.addr, result, tt.expected) 34 } 35 }) 36 } 37 } 38 39 func TestGetNetworkConfig(t *testing.T) { 40 tests := []struct { 41 name string 42 proto Protocol 43 wantNetwork string 44 wantListenAddr string 45 wantICMPProto int 46 }{ 47 { 48 "IPv4", 49 ProtocolIPv4, 50 NetworkIPv4, 51 ListenAddrV4, 52 ipv4.ICMPTypeEcho.Protocol(), 53 }, 54 { 55 "IPv6", 56 ProtocolIPv6, 57 NetworkIPv6, 58 ListenAddrV6, 59 ipv6.ICMPTypeEchoRequest.Protocol(), 60 }, 61 } 62 63 for _, tt := range tests { 64 t.Run(tt.name, func(t *testing.T) { 65 network, listenAddr, icmpProto := getNetworkConfig(tt.proto) 66 if network != tt.wantNetwork { 67 t.Errorf("network = %q, want %q", network, tt.wantNetwork) 68 } 69 if listenAddr != tt.wantListenAddr { 70 t.Errorf("listenAddr = %q, want %q", listenAddr, tt.wantListenAddr) 71 } 72 if icmpProto != tt.wantICMPProto { 73 t.Errorf("icmpProto = %d, want %d", icmpProto, tt.wantICMPProto) 74 } 75 }) 76 } 77 } 78 79 func TestGetEchoTypes(t *testing.T) { 80 tests := []struct { 81 name string 82 proto Protocol 83 wantEchoRequest icmp.Type 84 wantEchoReply icmp.Type 85 }{ 86 { 87 "IPv4", 88 ProtocolIPv4, 89 ipv4.ICMPTypeEcho, 90 ipv4.ICMPTypeEchoReply, 91 }, 92 { 93 "IPv6", 94 ProtocolIPv6, 95 ipv6.ICMPTypeEchoRequest, 96 ipv6.ICMPTypeEchoReply, 97 }, 98 } 99 100 for _, tt := range tests { 101 t.Run(tt.name, func(t *testing.T) { 102 echoRequest, echoReply := getEchoTypes(tt.proto) 103 if echoRequest != tt.wantEchoRequest { 104 t.Errorf("echoRequest = %v, want %v", echoRequest, tt.wantEchoRequest) 105 } 106 if echoReply != tt.wantEchoReply { 107 t.Errorf("echoReply = %v, want %v", echoReply, tt.wantEchoReply) 108 } 109 }) 110 } 111 } 112 113 func TestIsTimeout(t *testing.T) { 114 tests := []struct { 115 name string 116 err error 117 expected bool 118 }{ 119 { 120 "timeout error", 121 &net.OpError{Op: "read", Err: &timeoutError{}}, 122 true, 123 }, 124 { 125 "non-timeout OpError", 126 &net.OpError{Op: "read", Err: &nonTimeoutError{}}, 127 false, 128 }, 129 { 130 "regular error", 131 net.ErrClosed, 132 false, 133 }, 134 { 135 "nil error", 136 nil, 137 false, 138 }, 139 } 140 141 for _, tt := range tests { 142 t.Run(tt.name, func(t *testing.T) { 143 result := isTimeout(tt.err) 144 if result != tt.expected { 145 t.Errorf("isTimeout() = %v, want %v", result, tt.expected) 146 } 147 }) 148 } 149 } 150 151 type timeoutError struct{} 152 153 func (e *timeoutError) Error() string { return "timeout" } 154 func (e *timeoutError) Timeout() bool { return true } 155 func (e *timeoutError) Temporary() bool { return true } 156 157 type nonTimeoutError struct{} 158 159 func (e *nonTimeoutError) Error() string { return "not timeout" } 160 func (e *nonTimeoutError) Timeout() bool { return false } 161 func (e *nonTimeoutError) Temporary() bool { return false } 162 163 func TestCreateEchoReply(t *testing.T) { 164 echoBody := &icmp.Echo{ 165 ID: 1234, 166 Seq: 5678, 167 Data: []byte("test data"), 168 } 169 170 tests := []struct { 171 name string 172 replyType icmp.Type 173 }{ 174 {"IPv4 reply", ipv4.ICMPTypeEchoReply}, 175 {"IPv6 reply", ipv6.ICMPTypeEchoReply}, 176 } 177 178 for _, tt := range tests { 179 t.Run(tt.name, func(t *testing.T) { 180 reply := createEchoReply(echoBody, tt.replyType) 181 182 if reply.Type != tt.replyType { 183 t.Errorf("Type = %v, want %v", reply.Type, tt.replyType) 184 } 185 if reply.Code != 0 { 186 t.Errorf("Code = %d, want 0", reply.Code) 187 } 188 189 replyBody := reply.Body.(*icmp.Echo) 190 if replyBody.ID != echoBody.ID { 191 t.Errorf("ID = %d, want %d", replyBody.ID, echoBody.ID) 192 } 193 if replyBody.Seq != echoBody.Seq { 194 t.Errorf("Seq = %d, want %d", replyBody.Seq, echoBody.Seq) 195 } 196 if string(replyBody.Data) != string(echoBody.Data) { 197 t.Errorf("Data = %q, want %q", replyBody.Data, echoBody.Data) 198 } 199 }) 200 } 201 } 202 203 func TestParseICMPMessage(t *testing.T) { 204 echoMsg := icmp.Message{ 205 Type: ipv4.ICMPTypeEcho, 206 Code: 0, 207 Body: &icmp.Echo{ 208 ID: 100, 209 Seq: 1, 210 Data: []byte("hello"), 211 }, 212 } 213 214 data, err := echoMsg.Marshal(nil) 215 if err != nil { 216 t.Fatalf("Failed to marshal test message: %v", err) 217 } 218 219 parsed, err := parseICMPMessage(data, ipv4.ICMPTypeEcho.Protocol()) 220 if err != nil { 221 t.Fatalf("parseICMPMessage failed: %v", err) 222 } 223 224 if parsed.Type != ipv4.ICMPTypeEcho { 225 t.Errorf("Type = %v, want %v", parsed.Type, ipv4.ICMPTypeEcho) 226 } 227 228 parsedBody := parsed.Body.(*icmp.Echo) 229 if parsedBody.ID != 100 { 230 t.Errorf("ID = %d, want 100", parsedBody.ID) 231 } 232 if parsedBody.Seq != 1 { 233 t.Errorf("Seq = %d, want 1", parsedBody.Seq) 234 } 235 } 236 237 func TestConstants(t *testing.T) { 238 if NetworkIPv4 != "ip4:icmp" { 239 t.Errorf("NetworkIPv4 = %q, want %q", NetworkIPv4, "ip4:icmp") 240 } 241 if NetworkIPv6 != "ip6:ipv6-icmp" { 242 t.Errorf("NetworkIPv6 = %q, want %q", NetworkIPv6, "ip6:ipv6-icmp") 243 } 244 if ListenAddrV4 != "0.0.0.0" { 245 t.Errorf("ListenAddrV4 = %q, want %q", ListenAddrV4, "0.0.0.0") 246 } 247 if ListenAddrV6 != "::" { 248 t.Errorf("ListenAddrV6 = %q, want %q", ListenAddrV6, "::") 249 } 250 if MaxPacketSize != 1500 { 251 t.Errorf("MaxPacketSize = %d, want 1500", MaxPacketSize) 252 } 253 }