# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 26.1.2. [TC-SC-5.2] Receiving a group message - TH to DUT

PICS:
    - MCORE.ROLE.COMMISSIONEE

config:
    nodeId: 0x12344321
    cluster: "Groups"
    endpoint: 0

    # Users should set endpoint value on the command line to PIXIT.G.ENDPOINT
    Groups.Endpoint: 1

tests:
    - label: "Step 0: Wait for the commissioned device to be retrieved"
      cluster: "DelayCommands"
      command: "WaitForCommissionee"
      arguments:
          values:
              - name: "nodeId"
                value: nodeId

    - label: "Read the commissioner node ID"
      cluster: "CommissionerCommands"
      command: "GetCommissionerNodeId"
      response:
          values:
              - name: "nodeId"
                saveAs: commissionerNodeId

    - label:
          "Step 1: TH writes the ACL attribute in the Access Control cluster to
          add Manage privileges for group 0x0103 and maintain the current
          administrative privileges for the TH. The following access control
          list shall be used: List item 1 (TH admin): 1)Privilege: Administer
          (5) 2)AuthMode: CASE (2) 3)Subjects: TH node id ([N1]) 4)Targets: all
          (null) List item 2 (group operate access): 1)Privilege: Manage (4)
          2)AuthMode: Group (3) 3)Subjects: group 0x0103 ([0x0103]) 4)Targets:
          all (null)"
      cluster: "Access Control"
      command: "writeAttribute"
      attribute: "ACL"
      arguments:
          value:
              [
                  {
                      FabricIndex: 1,
                      Privilege: 5,
                      AuthMode: 2,
                      Subjects: [commissionerNodeId],
                      Targets: null,
                  },
                  {
                      FabricIndex: 1,
                      Privilege: 4,
                      AuthMode: 3,
                      Subjects: [0x0103],
                      Targets: null,
                  },
              ]

    - label:
          "Step 2: TH sends KeySetWrite command in the GroupKeyManagement
          cluster to DUT using a key that is pre-installed on the TH.
          GroupKeySet fields are as follows: 1)GroupKeySetID: 0x01a3
          2)GroupKeySecurityPolicy: TrustFirst (0) 3)EpochKey0:
          d0d1d2d3d4d5d6d7d8d9dadbdcdddedf 4)EpochStartTime0:1 5)EpochKey1:
          d1d1d2d3d4d5d6d7d8d9dadbdcdddedf 6)EpochStartTime1:
          18446744073709551613 7)EpochKey2: d2d1d2d3d4d5d6d7d8d9dadbdcdddedf
          8)EpochStartTime2: 18446744073709551614"
      PICS: GRPKEY.S.C00.Rsp
      cluster: "Group Key Management"
      command: "KeySetWrite"
      arguments:
          values:
              - name: GroupKeySet
                value:
                    {
                        GroupKeySetID: 0x01a3,
                        GroupKeySecurityPolicy: 0,
                        EpochKey0: "hex:d0d1d2d3d4d5d6d7d8d9dadbdcdddedf",
                        EpochStartTime0: 1,
                        EpochKey1: "hex:d1d1d2d3d4d5d6d7d8d9dadbdcdddedf",
                        EpochStartTime1: 18446744073709551613,
                        EpochKey2: "hex:d2d1d2d3d4d5d6d7d8d9dadbdcdddedf",
                        EpochStartTime2: 18446744073709551614,
                    }

    - label:
          "Step 3: TH binds GroupId 0x0103 and 0x0101 with GroupKeySetID 0x01a3
          in the GroupKeyMap attribute list on GroupKeyManagement cluster by
          writing the GroupKeyMap attribute with 2 entries as follows: List item
          1: 1)GroupId: 0x0103 2)GroupKeySetId: 0x01a3 List item 2: 1)GroupId:
          0x0101 2)GroupKeySetId: 0x01a3"
      PICS: GRPKEY.S.A0000
      cluster: "Group Key Management"
      command: "writeAttribute"
      attribute: "GroupKeyMap"
      arguments:
          value:
              [
                  { FabricIndex: 1, GroupId: 0x0103, GroupKeySetID: 0x01a3 },
                  { FabricIndex: 1, GroupId: 0x0101, GroupKeySetID: 0x01a3 },
              ]

    - label:
          "Step 4: TH cleans up the groups by sending the RemoveAllGroups
          command to the DUT on PIXIT.G.ENDPOINT"
      PICS: G.S.C04.Rsp
      endpoint: Groups.Endpoint
      command: "RemoveAllGroups"

    - label:
          "Step 5: TH sends AddGroup Command to DUT on PIXIT.G.ENDPOINT with the
          the following settings 1)GroupID: 0x0103 2)GroupName: 'Test Group
          0103'"
      PICS: G.S.C00.Rsp
      endpoint: Groups.Endpoint
      command: "AddGroup"
      arguments:
          values:
              - name: GroupID
                value: 0x0103
              - name: GroupName
                value: "Test Group 0103"
      response:
          values:
              - name: Status
                value: 0
              - name: GroupID
                value: 0x0103

    - label:
          "Step 6: TH sends a AddGroup Command to the Groups cluster with the
          GroupID field set to 0x0101 and the GroupName set to an 'Test Group
          0101'. The command is sent as a group command using GroupID 0x0103"
      PICS: G.S.C00.Rsp
      groupId: 0x0103
      command: "AddGroup"
      arguments:
          values:
              - name: GroupID
                value: 0x0101
              - name: GroupName
                value: "Test Group 0101"

    # Give the group AddGroup time to actually happen; unicast delivery can outrace
    # multicast if the unicast packet is sent immediately after the multicast one.
    - label: "Wait for AddGroup"
      cluster: "DelayCommands"
      command: "WaitForMs"
      arguments:
          values:
              - name: "ms"
                value: 1000

    - label: "Verify there is no response from DUT"
      cluster: "LogCommands"
      command: "UserPrompt"
      PICS: "!PICS_SDK_CI_ONLY"
      arguments:
          values:
              - name: "message"
                value: "Verify that there is no response from DUT"
              - name: "expectedValue"
                value: "y"

    - label:
          "Step 7: TH sends a ViewGroup Command to the Groups cluster on
          Endpoint PIXIT.G.ENDPOINT over CASE with the GroupID set to 0x0101 to
          confirm that the AddGroup command from step 6 was successful"
      PICS: G.S.F00 && G.S.C01.Rsp
      endpoint: Groups.Endpoint
      command: "ViewGroup"
      arguments:
          values:
              - name: GroupID
                value: 0x0101
      response:
          values:
              - name: Status
                value: 0
              - name: GroupID
                value: 0x0101
              - name: GroupName
                value: "Test Group 0101"

    - label:
          "Step 8: TH sends a ViewGroup Command to the Groups cluster on
          Endpoint PIXIT.G.ENDPOINT over CASE with the GroupID set to 0x0101 to
          confirm that the AddGroup command from step 6 was successful "
      PICS: " !G.S.F00 && G.S.C01.Rsp "
      endpoint: Groups.Endpoint
      command: "ViewGroup"
      arguments:
          values:
              - name: GroupID
                value: 0x0101
      response:
          values:
              - name: Status
                value: 0
              - name: GroupID
                value: 0x0101
              - name: GroupName
                value: ""

    - label:
          "Step 9: TH removes the Group key set that was added by sending a
          KeySetRemove command to the GroupKeyManagement cluster with the
          GroupKeySetID field set to 0x01a3"
      PICS: GRPKEY.S.C03.Rsp
      cluster: "Group Key Management"
      command: "KeySetRemove"
      arguments:
          values:
              - name: GroupKeySetID
                value: 0x01a3

    - label:
          "Step 10: TH cleans up the groups by sending the RemoveAllGroups
          command to the DUT on PIXIT.G.ENDPOINT"
      PICS: G.S.C04.Rsp
      endpoint: Groups.Endpoint
      command: "RemoveAllGroups"

    - label:
          "Step 11: TH writes the ACL attribute in the Access Control cluster to
          remove Manage privileges for group 0x0103 and maintain the current
          administrative privileges for the TH. The following access control
          list shall be used: List item 1 (TH admin): 1)Privilege: Administer
          (5) 2)AuthMode: CASE (2) 3)Subjects: TH node id ([N1]) 4)Targets: all
          (null)"
      cluster: "Access Control"
      command: "writeAttribute"
      attribute: "ACL"
      arguments:
          value:
              [
                  {
                      FabricIndex: 1,
                      Privilege: 5,
                      AuthMode: 2,
                      Subjects: [commissionerNodeId],
                      Targets: null,
                  },
              ]
