From 0b647fdb8bc8ddac751c78ca06daed13b799e7b1 Mon Sep 17 00:00:00 2001 From: Daniel Fangl Date: Tue, 19 Oct 2021 15:04:30 +0200 Subject: [PATCH] Fix random_ipv6_cidr network generation, with test (#4438) --- moto/ec2/utils.py | 2 +- tests/test_ec2/test_utils.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index 32689dc05..c80cf3328 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -252,7 +252,7 @@ def randor_ipv4_cidr(): def random_ipv6_cidr(): - return "2400:6500:{}:{}::/56".format(random_resource_id(4), random_resource_id(4)) + return "2400:6500:{}:{}00::/56".format(random_resource_id(4), random_resource_id(2)) def generate_route_id( diff --git a/tests/test_ec2/test_utils.py b/tests/test_ec2/test_utils.py index 75e3953bf..0a32e8666 100644 --- a/tests/test_ec2/test_utils.py +++ b/tests/test_ec2/test_utils.py @@ -1,3 +1,6 @@ +import ipaddress +from unittest.mock import patch + from moto.ec2 import utils from .helpers import rsa_check_private_key @@ -10,3 +13,13 @@ def test_random_key_pair(): # AWS uses MD5 fingerprints, which are 47 characters long, *not* SHA1 # fingerprints with 59 characters. assert len(key_pair["fingerprint"]) == 47 + + +def test_random_ipv6_cidr(): + def mocked_random_resource_id(chars: int): + return "a" * chars + + with patch("moto.ec2.utils.random_resource_id", mocked_random_resource_id): + cidr_address = utils.random_ipv6_cidr() + # this will throw value error if host bits are set + ipaddress.ip_network(cidr_address)