use of com.amazonaws.services.ec2.model.Ipv6Range in project photon-model by vmware.
the class TestAWSSecurityGroupService method testSecurityGroupRulesCalculation.
/*
* Test calculating Rule range and Cidr based on the AWS IpPermission configuration
*/
@Test
public void testSecurityGroupRulesCalculation() throws Throwable {
IpRange ipv4Range = new IpRange();
ipv4Range.setCidrIp("0.0.0.0/0");
Ipv6Range ipv6Range = new Ipv6Range();
ipv6Range.setCidrIpv6("::/0");
// {IpProtocol: -1,UserIdGroupPairs: [],Ipv6Ranges: [],PrefixListIds: [], Ipv4Ranges: [{CidrIp: 0.0.0.0/0}]}
// expected output: cidr = "0.0.0.0/0" range = "1-65535"
IpPermission mockPermission1 = new IpPermission();
mockPermission1.setFromPort(-1);
mockPermission1.setToPort(-1);
mockPermission1.setIpProtocol("-1");
mockPermission1.setIpv4Ranges(Collections.singleton(ipv4Range));
Rule outputRule1 = new Rule();
outputRule1.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule1, mockPermission1.getIpProtocol());
outputRule1.ports = AWSSecurityGroupUtils.calculatePorts(outputRule1, mockPermission1);
outputRule1.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule1, mockPermission1);
assertEquals("wrong ports range:", SecurityGroupService.ALL_PORTS, outputRule1.ports);
assertEquals("wrong cidr:", "0.0.0.0/0", outputRule1.ipRangeCidr);
// {IpProtocol: icmpv6,FromPort: -1,ToPort: -1,UserIdGroupPairs: [], Ipv6Ranges: [{CidrIpv6: ::/0}],
// PrefixListIds: [],Ipv4Ranges: []}
// expected output: cidr = "::/0" range = "1-65535"
IpPermission mockPermission2 = new IpPermission();
mockPermission2.setFromPort(-1);
mockPermission2.setToPort(-1);
mockPermission2.setIpProtocol("icmpv6");
mockPermission2.setIpv6Ranges(Collections.singleton(ipv6Range));
Rule outputRule2 = new Rule();
outputRule2.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule2, mockPermission2.getIpProtocol());
outputRule2.ports = AWSSecurityGroupUtils.calculatePorts(outputRule2, mockPermission2);
outputRule2.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule2, mockPermission2);
assertEquals("wrong ports range:", SecurityGroupService.ALL_PORTS, outputRule2.ports);
assertEquals("wrong cidr:", "::/0", outputRule2.ipRangeCidr);
// {IpProtocol: icmp,FromPort: -1,ToPort: -1,UserIdGroupPairs: [], Ipv6Ranges: [{CidrIpv6: ::/0}],
// PrefixListIds: [],Ipv4Ranges: []}
// expected output: cidr = "::/0" range = "1-65535"
IpPermission mockPermission3 = new IpPermission();
mockPermission3.setFromPort(-1);
mockPermission3.setToPort(-1);
mockPermission3.setIpProtocol("icmp");
mockPermission3.setIpv6Ranges(Collections.singleton(ipv6Range));
Rule outputRule3 = new Rule();
outputRule3.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule3, mockPermission3.getIpProtocol());
outputRule3.ports = AWSSecurityGroupUtils.calculatePorts(outputRule3, mockPermission3);
outputRule3.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule3, mockPermission3);
assertEquals("wrong ports range:", SecurityGroupService.ALL_PORTS, outputRule3.ports);
assertEquals("wrong cidr:", "::/0", outputRule3.ipRangeCidr);
// {IpProtocol: icmp,FromPort: 0,ToPort: -1,UserIdGroupPairs: [], Ipv6Ranges: [],
// PrefixListIds: [],Ipv4Ranges: [{CidrIp: 0.0.0.0/0}]}
// expected output: cidr = "0.0.0.0/0" range = "0"
IpPermission mockPermission4 = new IpPermission();
mockPermission4.setFromPort(0);
mockPermission4.setToPort(-1);
mockPermission4.setIpProtocol("icmp");
mockPermission4.setIpv4Ranges(Collections.singleton(ipv4Range));
Rule outputRule4 = new Rule();
outputRule4.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule4, mockPermission4.getIpProtocol());
outputRule4.ports = AWSSecurityGroupUtils.calculatePorts(outputRule4, mockPermission4);
outputRule4.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule4, mockPermission4);
assertEquals("wrong ports range:", "0", outputRule4.ports);
assertEquals("wrong cidr:", "0.0.0.0/0", outputRule4.ipRangeCidr);
// {IpProtocol: icmpv6,FromPort: -1,ToPort: -1,UserIdGroupPairs: [], Ipv6Ranges: [],PrefixListIds: [],
// Ipv4Ranges: [{CidrIp: 0.0.0.0/0}]}
// expected output: cidr = "*" range = "1-65535"
IpPermission mockPermission5 = new IpPermission();
mockPermission5.setFromPort(-1);
mockPermission5.setToPort(-1);
mockPermission5.setIpProtocol("icmpv6");
mockPermission5.setIpv4Ranges(Collections.singleton(ipv4Range));
Rule outputRule5 = new Rule();
outputRule5.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule5, mockPermission5.getIpProtocol());
outputRule5.ports = AWSSecurityGroupUtils.calculatePorts(outputRule5, mockPermission5);
outputRule5.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule5, mockPermission5);
assertEquals("wrong ports range:", SecurityGroupService.ALL_PORTS, outputRule5.ports);
assertEquals("wrong cidr:", SecurityGroupService.ANY, outputRule5.ipRangeCidr);
// {IpProtocol: icmp,FromPort: 3,ToPort: 0,UserIdGroupPairs: [], Ipv6Ranges: [],PrefixListIds: [],
// Ipv4Ranges: [{CidrIp: 0.0.0.0/0}]}
// expected output: protocol = "ICMPv4" cidr = "0.0.0.0/0" range = "3-0"
IpPermission mockPermission6 = new IpPermission();
mockPermission6.setFromPort(3);
mockPermission6.setToPort(0);
mockPermission6.setIpProtocol("icmp");
mockPermission6.setIpv4Ranges(Collections.singleton(ipv4Range));
Rule outputRule6 = new Rule();
outputRule6.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule6, mockPermission6.getIpProtocol());
outputRule6.ports = AWSSecurityGroupUtils.calculatePorts(outputRule6, mockPermission6);
outputRule6.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule6, mockPermission6);
assertEquals("wrong ports range:", "3-0", outputRule6.ports);
assertEquals("wrong cidr:", "0.0.0.0/0", outputRule6.ipRangeCidr);
// {IpProtocol: icmp,FromPort: 8,ToPort: -1,UserIdGroupPairs: [], Ipv6Ranges: [],PrefixListIds: [],
// Ipv4Ranges: [{CidrIp: 0.0.0.0/0}]}
// expected output: cidr = "0.0.0.0/0" range = "8"
IpPermission mockPermission7 = new IpPermission();
mockPermission7.setFromPort(8);
mockPermission7.setToPort(-1);
mockPermission7.setIpProtocol("icmp");
mockPermission7.setIpv4Ranges(Collections.singleton(ipv4Range));
Rule outputRule7 = new Rule();
outputRule7.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule7, mockPermission7.getIpProtocol());
outputRule7.ports = AWSSecurityGroupUtils.calculatePorts(outputRule7, mockPermission7);
outputRule7.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule7, mockPermission7);
assertEquals("wrong ports range:", "8", outputRule7.ports);
assertEquals("wrong cidr:", "0.0.0.0/0", outputRule7.ipRangeCidr);
}
Aggregations