11import pytest
2- from .imds import is_imds_ip_address
3-
4-
5- # Assuming the is_imds_ip_address function is defined in the same file or imported from another module
6- def is_imds_ip_address (ip : str ) -> bool :
7- # This is a placeholder for the actual implementation
8- # You should replace this with the actual function from your code
9- return ip in ["169.254.169.254" , "fd00:ec2::254" ]
2+ from .imds import is_imds_ip_address , resolves_to_imds_ip
103
114
125def test_returns_true_for_imds_ip_addresses ():
@@ -17,3 +10,53 @@ def test_returns_true_for_imds_ip_addresses():
1710def test_returns_false_for_non_imds_ip_addresses ():
1811 assert is_imds_ip_address ("1.2.3.4" ) is False
1912 assert is_imds_ip_address ("example.com" ) is False
13+
14+
15+ # --- Tests ---
16+ def test_trusted_hostname_returns_false ():
17+ """Test that trusted hostnames always return False."""
18+ assert resolves_to_imds_ip (["1.1.1.1" ], "metadata.google.internal" ) is False
19+
20+
21+ def test_aws_imds_ipv4_present_returns_ip ():
22+ """Test that an AWS IMDS IPv4 address is returned if present."""
23+ assert (
24+ resolves_to_imds_ip (["169.254.169.254" , "8.8.8.8" ], "example.com" )
25+ == "169.254.169.254"
26+ )
27+
28+
29+ def test_aws_imds_ipv6_present_returns_ip ():
30+ """Test that an AWS IMDS IPv6 address is returned if present."""
31+ assert (
32+ resolves_to_imds_ip (["fd00:ec2::254" , "2001:db8::1" ], "example.com" )
33+ == "fd00:ec2::254"
34+ )
35+
36+
37+ def test_alibaba_imds_ip_present_returns_ip ():
38+ """Test that an Alibaba IMDS IP address is returned if present."""
39+ assert (
40+ resolves_to_imds_ip (["100.100.100.200" , "8.8.8.8" ], "example.com" )
41+ == "100.100.100.200"
42+ )
43+
44+
45+ def test_no_imds_ip_present_returns_false ():
46+ """Test that False is returned if no IMDS IP is present."""
47+ assert resolves_to_imds_ip (["8.8.8.8" , "1.1.1.1" ], "example.com" ) is False
48+
49+
50+ def test_empty_ip_list_returns_false ():
51+ """Test that False is returned if the IP list is empty."""
52+ assert resolves_to_imds_ip ([], "example.com" ) is False
53+
54+
55+ def test_mixed_imds_and_normal_ips ():
56+ """Test that the first IMDS IP in the list is returned."""
57+ assert (
58+ resolves_to_imds_ip (
59+ ["8.8.8.8" , "169.254.169.254" , "100.100.100.200" ], "example.com"
60+ )
61+ == "169.254.169.254"
62+ )
0 commit comments