<commit_before>package aws

import (
	"reflect"
	"testing"

	"github.com/awslabs/aws-sdk-go/service/ec2"
	"github.com/hashicorp/aws-sdk-go/aws"
	"github.com/hashicorp/aws-sdk-go/gen/elb"
	"github.com/hashicorp/aws-sdk-go/gen/rds"
	"github.com/hashicorp/terraform/flatmap"
	"github.com/hashicorp/terraform/helper/hashcode"
	"github.com/hashicorp/terraform/helper/schema"
)

// Returns test configuration
func testConfSDK() map[string]string {
	return map[string]string{
		"listener.#":                   "1",
		"listener.0.lb_port":           "80",
		"listener.0.lb_protocol":       "http",
		"listener.0.instance_port":     "8000",
		"listener.0.instance_protocol": "http",
		"availability_zones.#":         "2",
		"availability_zones.0":         "us-east-1a",
		"availability_zones.1":         "us-east-1b",
		"ingress.#":                    "1",
		"ingress.0.protocol":           "icmp",
		"ingress.0.from_port":          "1",
		"ingress.0.to_port":            "-1",
		"ingress.0.cidr_blocks.#":      "1",
		"ingress.0.cidr_blocks.0":      "0.0.0.0/0",
		"ingress.0.security_groups.#":  "2",
		"ingress.0.security_groups.0":  "sg-11111",
		"ingress.0.security_groups.1":  "foo/sg-22222",
	}
}

func TestExpandIPPermsSDK(t *testing.T) {
	hash := func(v interface{}) int {
		return hashcode.String(v.(string))
	}

	expanded := []interface{}{
		map[string]interface{}{
			"protocol":    "icmp",
			"from_port":   1,
			"to_port":     -1,
			"cidr_blocks": []interface{}{"0.0.0.0/0"},
			"security_groups": schema.NewSet(hash, []interface{}{
				"sg-11111",
				"foo/sg-22222",
			}),
		},
		map[string]interface{}{
			"protocol":  "icmp",
			"from_port": 1,
			"to_port":   -1,
			"self":      true,
		},
	}
	group := ec2.SecurityGroup{
		GroupID: aws.String("foo"),
		VPCID:   aws.String("bar"),
	}
	perms := expandIPPermsSDK(group, expanded)

	expected := []ec2.IPPermission{
		ec2.IPPermission{
			IPProtocol: aws.String("icmp"),
			FromPort:   aws.Long(1),
			ToPort:     aws.Long(-1),
			IPRanges:   []*ec2.IPRange{&ec2.IPRange{CIDRIP: aws.String("0.0.0.0/0")}},
			UserIDGroupPairs: []*ec2.UserIDGroupPair{
				&ec2.UserIDGroupPair{
					UserID:  aws.String("foo"),
					GroupID: aws.String("sg-22222"),
				},
				&ec2.UserIDGroupPair{
					GroupID: aws.String("sg-22222"),
				},
			},
		},
		ec2.IPPermission{
			IPProtocol: aws.String("icmp"),
			FromPort:   aws.Long(1),
			ToPort:     aws.Long(-1),
			UserIDGroupPairs: []*ec2.UserIDGroupPair{
				&ec2.UserIDGroupPair{
					UserID: aws.String("foo"),
				},
			},
		},
	}

	exp := expected[0]
	perm := perms[0]

	if *exp.FromPort != *perm.FromPort {
		t.Fatalf(
			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
			*perm.FromPort,
			*exp.FromPort)
	}

	if *exp.IPRanges[0].CIDRIP != *perm.IPRanges[0].CIDRIP {
		t.Fatalf(
			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
			*perm.IPRanges[0].CIDRIP,
			*exp.IPRanges[0].CIDRIP)
	}

	if *exp.UserIDGroupPairs[0].UserID != *perm.UserIDGroupPairs[0].UserID {
		t.Fatalf(
			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
			*perm.UserIDGroupPairs[0].UserID,
			*exp.UserIDGroupPairs[0].UserID)
	}

}

func TestExpandIPPerms_nonVPCSDK(t *testing.T) {
	hash := func(v interface{}) int {
		return hashcode.String(v.(string))
	}

	expanded := []interface{}{
		map[string]interface{}{
			"protocol":    "icmp",
			"from_port":   1,
			"to_port":     -1,
			"cidr_blocks": []interface{}{"0.0.0.0/0"},
			"security_groups": schema.NewSet(hash, []interface{}{
				"sg-11111",
				"foo/sg-22222",
			}),
		},
		map[string]interface{}{
			"protocol":  "icmp",
			"from_port": 1,
			"to_port":   -1,
			"self":      true,
		},
	}
	group := ec2.SecurityGroup{
		GroupName: aws.String("foo"),
	}
	perms := expandIPPermsSDK(group, expanded)

	expected := []ec2.IPPermission{<commit_msg>fix pointer references<commit_after>package aws

import (
	"reflect"
	"testing"

	"github.com/awslabs/aws-sdk-go/service/ec2"
	"github.com/hashicorp/aws-sdk-go/aws"
	"github.com/hashicorp/aws-sdk-go/gen/elb"
	"github.com/hashicorp/aws-sdk-go/gen/rds"
	"github.com/hashicorp/terraform/flatmap"
	"github.com/hashicorp/terraform/helper/hashcode"
	"github.com/hashicorp/terraform/helper/schema"
)

// Returns test configuration
func testConfSDK() map[string]string {
	return map[string]string{
		"listener.#":                   "1",
		"listener.0.lb_port":           "80",
		"listener.0.lb_protocol":       "http",
		"listener.0.instance_port":     "8000",
		"listener.0.instance_protocol": "http",
		"availability_zones.#":         "2",
		"availability_zones.0":         "us-east-1a",
		"availability_zones.1":         "us-east-1b",
		"ingress.#":                    "1",
		"ingress.0.protocol":           "icmp",
		"ingress.0.from_port":          "1",
		"ingress.0.to_port":            "-1",
		"ingress.0.cidr_blocks.#":      "1",
		"ingress.0.cidr_blocks.0":      "0.0.0.0/0",
		"ingress.0.security_groups.#":  "2",
		"ingress.0.security_groups.0":  "sg-11111",
		"ingress.0.security_groups.1":  "foo/sg-22222",
	}
}

func TestExpandIPPermsSDK(t *testing.T) {
	hash := func(v interface{}) int {
		return hashcode.String(v.(string))
	}

	expanded := []interface{}{
		map[string]interface{}{
			"protocol":    "icmp",
			"from_port":   1,
			"to_port":     -1,
			"cidr_blocks": []interface{}{"0.0.0.0/0"},
			"security_groups": schema.NewSet(hash, []interface{}{
				"sg-11111",
				"foo/sg-22222",
			}),
		},
		map[string]interface{}{
			"protocol":  "icmp",
			"from_port": 1,
			"to_port":   -1,
			"self":      true,
		},
	}
	group := &ec2.SecurityGroup{
		GroupID: aws.String("foo"),
		VPCID:   aws.String("bar"),
	}
	perms := expandIPPermsSDK(group, expanded)

	expected := []ec2.IPPermission{
		ec2.IPPermission{
			IPProtocol: aws.String("icmp"),
			FromPort:   aws.Long(1),
			ToPort:     aws.Long(-1),
			IPRanges:   []*ec2.IPRange{&ec2.IPRange{CIDRIP: aws.String("0.0.0.0/0")}},
			UserIDGroupPairs: []*ec2.UserIDGroupPair{
				&ec2.UserIDGroupPair{
					UserID:  aws.String("foo"),
					GroupID: aws.String("sg-22222"),
				},
				&ec2.UserIDGroupPair{
					GroupID: aws.String("sg-22222"),
				},
			},
		},
		ec2.IPPermission{
			IPProtocol: aws.String("icmp"),
			FromPort:   aws.Long(1),
			ToPort:     aws.Long(-1),
			UserIDGroupPairs: []*ec2.UserIDGroupPair{
				&ec2.UserIDGroupPair{
					UserID: aws.String("foo"),
				},
			},
		},
	}

	exp := expected[0]
	perm := perms[0]

	if *exp.FromPort != *perm.FromPort {
		t.Fatalf(
			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
			*perm.FromPort,
			*exp.FromPort)
	}

	if *exp.IPRanges[0].CIDRIP != *perm.IPRanges[0].CIDRIP {
		t.Fatalf(
			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
			*perm.IPRanges[0].CIDRIP,
			*exp.IPRanges[0].CIDRIP)
	}

	if *exp.UserIDGroupPairs[0].UserID != *perm.UserIDGroupPairs[0].UserID {
		t.Fatalf(
			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
			*perm.UserIDGroupPairs[0].UserID,
			*exp.UserIDGroupPairs[0].UserID)
	}

}

func TestExpandIPPerms_nonVPCSDK(t *testing.T) {
	hash := func(v interface{}) int {
		return hashcode.String(v.(string))
	}

	expanded := []interface{}{
		map[string]interface{}{
			"protocol":    "icmp",
			"from_port":   1,
			"to_port":     -1,
			"cidr_blocks": []interface{}{"0.0.0.0/0"},
			"security_groups": schema.NewSet(hash, []interface{}{
				"sg-11111",
				"foo/sg-22222",
			}),
		},
		map[string]interface{}{
			"protocol":  "icmp",
			"from_port": 1,
			"to_port":   -1,
			"self":      true,
		},
	}
	group := &ec2.SecurityGroup{
		GroupName: aws.String("foo"),
	}
	perms := expandIPPermsSDK(group, expanded)

	expected := []ec2.IPPermission{
{'fraction_non_alphanumeric': 0.20433683650392445, 'fraction_numerical': 0.03405613941732074, 'mean_word_length': 3.0659816116819902, 'pattern_counts': {'":': 70, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '122300', 'n_tokens_mistral': 3565, 'n_tokens_neox': 3178, 'n_words': 399}