 .../Ec2/SecurityGroupIpPermissionsTest.php         | 190 ++++++++++++++++++++-
 .../tests/src/Traits/AwsCloudTestFormDataTrait.php |  42 +++++
 2 files changed, 231 insertions(+), 1 deletion(-)

diff --git a/modules/cloud_service_providers/aws_cloud/tests/src/Functional/Ec2/SecurityGroupIpPermissionsTest.php b/modules/cloud_service_providers/aws_cloud/tests/src/Functional/Ec2/SecurityGroupIpPermissionsTest.php
index cb0b970..289418b 100644
--- a/modules/cloud_service_providers/aws_cloud/tests/src/Functional/Ec2/SecurityGroupIpPermissionsTest.php
+++ b/modules/cloud_service_providers/aws_cloud/tests/src/Functional/Ec2/SecurityGroupIpPermissionsTest.php
@@ -192,7 +192,7 @@ class SecurityGroupIpPermissionsTest extends AwsCloudTestCase {
       );
 
       $edit_url = "/clouds/aws_cloud/$cloud_context/security_group/$num/edit";
-      $view_url = "/clouds/aws_cloud/$cloud_context/security_group";
+      $view_url = "/clouds/aws_cloud/$cloud_context/security_group/$num";
 
       // Test case 1. (Inbound rule add (only) / delete).
       $rules = $this->createRulesTestFormData(self::$awsCloudSecurityGroupRulesInbound, $edit_url, 1, self::$awsCloudSecurityGroupRulesRepeatCount);
@@ -204,7 +204,28 @@ class SecurityGroupIpPermissionsTest extends AwsCloudTestCase {
 
       // Test case 3. (Combination of mixing above Test case 1. and 2.).
       $rules = $this->createRulesTestFormData(self::$awsCloudSecurityGroupRulesMix, $edit_url, 1, self::$awsCloudSecurityGroupRulesRepeatCount);
+
+      // Test case3.2 edit rules.
+      $params = $this->editRuleParams($rules);
+      $params['name'] = $add[$i]['group_name'];
+      $this->drupalPostForm($edit_url,
+          $params,
+          t('Save'));
+
+      $this->assertText(
+        t('The AWS Cloud Security Group "@name" has been saved.', [
+          '@name' => $params['name'],
+        ]),
+        t('Confirm Message: Edit | The AWS Cloud Security Group "@name" has been saved.', [
+          '@name' => $params['name'],
+        ])
+      );
+
+      // Confirm the values of edit form.
+      $this->confirmRulesFormData($rules, $edit_url);
+
       $this->revokeRulesTestFormData($rules, $view_url, self::$awsCloudSecurityGroupRulesRepeatCount);
+
     }
   }
 
@@ -336,4 +357,171 @@ class SecurityGroupIpPermissionsTest extends AwsCloudTestCase {
 
   }
 
+  /**
+   * Test for update IP permissions.
+   */
+  public function testUpdateIpPermissions() {
+    $this->repeatTestUpdateIpPermissions(self::$awsCloudSecurityGroupRepeatCount);
+  }
+
+
+  /**
+   * Test for updating IP permissions.
+   *
+   * @param int $max_test_repeat_count
+   *   Max test repeating count.
+   */
+  private function repeatTestUpdateIpPermissions($max_test_repeat_count = 1) {
+    $cloud_context = $this->cloudContext;
+
+    $add = $this->createSecurityGroupTestFormData(self::$awsCloudSecurityGroupRepeatCount);
+
+    for ($i = 0; $i < $max_test_repeat_count; $i++) {
+      $num = $i + 1;
+
+      $this->reloadMockData();
+
+      $defaults = $this->latestTemplateVars;
+      $defaults['group_name'] = $add[$i]['group_name'];
+      $add[$i]['vpc_id'] = $defaults['vpc_id'];
+
+      $this->drupalGet("/clouds/aws_cloud/$cloud_context/security_group/add");
+      $this->drupalPostForm("/clouds/aws_cloud/$cloud_context/security_group/add",
+          $add[$i],
+          t('Save'));
+
+      // After save, assert the save is successful.
+      $this->assertResponse(200, t('Add | HTTP 200: The new AWS Cloud Security Group', ['@num' => $num]));
+      $this->assertNoText(t('Notice'), t('Add | Make sure w/o Notice'));
+      $this->assertNoText(t('warning'), t('Add | Make sure w/o Warnings'));
+      $this->assertText($add[$i]['group_name'], t('Add | Key Pair: @group_name', ['@group_name' => $add[$i]['group_name']]));
+      $this->assertText(
+        t('The AWS Cloud Security Group "@group_name" has been created.', [
+          '@group_name' => $add[$i]['group_name'],
+        ]),
+        t('Confirm Message: Add | The AWS Cloud Security Group "@group_name" has been created.', [
+          '@group_name' => $add[$i]['group_name'],
+        ])
+      );
+
+      $edit_url = "/clouds/aws_cloud/$cloud_context/security_group/$num/edit";
+
+      // Add rules.
+      $add_rules = $this->createRulesTestFormData(self::$awsCloudSecurityGroupRulesMix, $edit_url, 1, self::$awsCloudSecurityGroupRulesRepeatCount);
+
+      // Create rules for mock data.
+      $count = rand(1, count($add_rules) - 1);
+      $types = [self::$awsCloudSecurityGroupRulesInbound, self::$awsCloudSecurityGroupRulesOutbound];
+      $rules = [];
+      $idx = 0;
+      while ($idx < $count) {
+        $type = $types[array_rand($types)];
+        $rule = ['type' => $type];
+        $this->getRandomRule($rule);
+        $rules[] = $rule;
+        $idx++;
+      }
+      // Update rules in mock data.
+      $this->updateRulesMockData($rules, self::$awsCloudSecurityGroupRulesOutbound);
+
+      // Update.
+      $this->drupalGet("/clouds/aws_cloud/$cloud_context/security_group/update");
+
+      // Confirm the values of edit form.
+      $this->confirmRulesFormData($rules, $edit_url);
+    }
+
+  }
+
+
+  /**
+   * Add, edit and delete rules and making parameter.
+   *
+   * @param array $rules
+   *   The array of rules.
+   *
+   * @return array
+   *   The edited params.
+   */
+  private function editRuleParams(array &$rules) {
+    $params = [];
+    $inbound_index = 0;
+    $outbound_index = 0;
+    $del_idxs = array_rand($rules, rand(1, count($rules)));
+    if (!is_array($del_idxs)) {
+      $del_idxs = [$del_idxs];
+    }
+
+    foreach ($rules as $idx=>&$rule) {
+      if ($rule['type'] == self::$awsCloudSecurityGroupRulesInbound) {
+        if ($inbound_index === 0 && rand(0 , 1) === 1) {
+          $rules[] = ['type' => self::$awsCloudSecurityGroupRulesInbound];
+        }
+        $index = $inbound_index++;
+        $prefix = 'ip_permission';
+      }
+      else {
+        if ($outbound_index === 0 && rand(0 , 1) === 1) {
+          $rules[] = ['type' => self::$awsCloudSecurityGroupRulesOutbound];
+        }
+        $index = $outbound_index++;
+        $prefix = 'outbound_permission';
+      }
+      if (in_array($idx, $del_idxs)) {
+        foreach ($rule as $key => $value) {
+          if ($key == 'type' || $key == 'source') {
+            continue;
+          }
+          $rule[$key] = '';
+        }
+      }
+      else {
+        $this->getRandomRule($rule);
+      }
+      foreach ($rule as $key => $value) {
+        if ($key == 'type') {
+          continue;
+        }
+        $params["${prefix}[${index}][${key}]"] = $value;
+      }
+    }
+
+    $del_idxs = array_flip($del_idxs);
+    $rules = array_diff_key($rules, $del_idxs);
+
+    $this->updateRulesMockData($rules, self::$awsCloudSecurityGroupRulesOutbound);
+
+    return $params;
+
+  }
+
+  /**
+   * Get random rule.
+   *
+   * @param array $rule
+   *   The array of rule.
+   */
+  private function getRandomRule(&$rule) {
+    $sources = ['ip4', 'ip6', 'group'];
+    $rule = ['type' => $rule['type']];
+    $source = $sources[array_rand($sources)];
+    $rule['source'] = $source;
+    $rule['from_port'] = Utils::getRandomFromPort();
+    $rule['to_port'] = Utils::getRandomToPort();
+    if ($source == 'ip4') {
+      $rule['cidr_ip'] = Utils::getRandomCidr();
+    }
+    elseif ($source == 'ip6') {
+      $rule['cidr_ip_v6'] = Utils::getRandomCidrV6();
+    }
+    else {
+      $rule['user_id'] = $this->random->name(8, TRUE);
+      $rule['group_id'] = "sg-{$this->getRandomAwsId()}";
+      $rule['vpc_id'] = "vpc-{$this->getRandomAwsId()}";
+      $rule['peering_connection_id'] = "pcx-{$this->getRandomAwsId()}";
+      $rule['peering_status'] = 'active';
+    }
+
+  }
+
 }
diff --git a/modules/cloud_service_providers/aws_cloud/tests/src/Traits/AwsCloudTestFormDataTrait.php b/modules/cloud_service_providers/aws_cloud/tests/src/Traits/AwsCloudTestFormDataTrait.php
index 2de9b8a..0df5a97 100644
--- a/modules/cloud_service_providers/aws_cloud/tests/src/Traits/AwsCloudTestFormDataTrait.php
+++ b/modules/cloud_service_providers/aws_cloud/tests/src/Traits/AwsCloudTestFormDataTrait.php
@@ -740,6 +740,9 @@ trait AwsCloudTestFormDataTrait {
       $this->assertText($rule['from_port'], 'Create Rule');
     }
 
+    // Confirm the values of edit form.
+    $this->confirmRulesFormData($rules, $edit_url);
+
     return $rules;
   }
 
@@ -754,6 +757,7 @@ trait AwsCloudTestFormDataTrait {
    *   The Security Group rules repeat count.
    */
   protected function revokeRulesTestFormData(array $rules, $view_url, $security_group_rules_repeat_count = 1) {
+    $this->drupalGet($view_url);
     $count = count($rules);
     $inbound_rules = array_filter($rules, function ($a) {
       return $a['type'] == self::$awsCloudSecurityGroupRulesInbound;
@@ -779,4 +783,42 @@ trait AwsCloudTestFormDataTrait {
     }
   }
 
+  /**
+   * Confirm rule values of security group edit form.
+   *
+   * @param array $rules
+   *   The array of rules.
+   * @param string $edit_url
+   *   The url of edit form.
+   */
+  protected function confirmRulesFormData(array $rules, $edit_url) {
+    $this->drupalGet($edit_url);
+
+    $inbound_index = 0;
+    $outbound_index = 0;
+    foreach ($rules as $rule) {
+      if ($rule['type'] == self::$awsCloudSecurityGroupRulesInbound) {
+        $index = $inbound_index++;
+        $prefix = 'ip_permission';
+      }
+      else {
+        $index = $outbound_index++;
+        $prefix = 'outbound_permission';
+      }
+      foreach ($rule as $key => $value) {
+        if ($key == 'type') {
+          continue;
+        }
+        $name = "${prefix}[${index}][${key}]";
+        $this->assertFieldByName($name, $value, 'Edit Rule');
+      }
+    }
+
+    // Confirm the last raw is empty.
+    $name = "ip_permission[${inbound_index}][from_port]";
+    $this->assertFieldByName($name, '', 'Edit Rule');
+    $name = "outbound_permission[${outbound_index}][from_port]";
+    $this->assertFieldByName($name, '', 'Edit Rule');
+  }
+
 }
