89527f52331c2879e0825fcc0708ee00684ce2bd
[ric-plt/e2mgr.git] / E2Manager / handlers / x2apPdu_asn1_unpacker_test.go
1 /*******************************************************************************
2  *
3  *   Copyright (c) 2019 AT&T Intellectual Property.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  *
17  *******************************************************************************/
18 package handlers
19
20 import (
21         "e2mgr/logger"
22         "fmt"
23         "strings"
24         "testing"
25 )
26
27 /*
28  * Unpack an x2setup response returned from RAN.
29  * Verify it matches the want pdu.
30  */
31
32 func TestUnpackX2apSetupResponse(t *testing.T) {
33         logger, _ := logger.InitLogger(logger.DebugLevel)
34
35         wantPduAsStr := `SuccessfulOutcome ::= {
36             procedureCode: 6
37             criticality: 0 (reject)
38             value: X2SetupResponse ::= {
39                 protocolIEs: ProtocolIE-Container ::= {
40                     X2SetupResponse-IEs ::= {
41                         id: 21
42                         criticality: 0 (reject)
43                         value: GlobalENB-ID ::= {
44                             pLMN-Identity: 02 F8 29
45                             eNB-ID: 00 7A 80 (4 bits unused)
46                         }
47                     }
48                     X2SetupResponse-IEs ::= {
49                         id: 20
50                         criticality: 0 (reject)
51                         value: ServedCells ::= {
52                             SEQUENCE ::= {
53                                 servedCellInfo: ServedCell-Information ::= {
54                                     pCI: 99
55                                     cellId: ECGI ::= {
56                                         pLMN-Identity: 02 F8 29
57                                         eUTRANcellIdentifier: 00 07 AB 50 (4 bits unused)
58                                     }
59                                     tAC: 01 02
60                                     broadcastPLMNs: BroadcastPLMNs-Item ::= {
61                                         02 F8 29
62                                     }
63                                     eUTRA-Mode-Info: FDD-Info ::= {
64                                         uL-EARFCN: 1
65                                         dL-EARFCN: 1
66                                         uL-Transmission-Bandwidth: 3 (bw50)
67                                         dL-Transmission-Bandwidth: 3 (bw50)
68                                     }
69                                 }
70                             }
71                         }
72                     }
73                 }
74             }
75         }`
76
77         inputPayloadAsStr := "2006002a000002001500080002f82900007a8000140017000000630002f8290007ab50102002f829000001000133"
78         var payload []byte
79
80         _, err := fmt.Sscanf(inputPayloadAsStr, "%x", &payload)
81         if err != nil {
82                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
83         }
84
85         response, err := unpackX2apPduAndRefine(logger, MaxAsn1CodecAllocationBufferSize /*allocation buffer*/, len(payload), payload, MaxAsn1CodecMessageBufferSize /*message buffer*/)
86         if err != nil {
87                 t.Errorf("want: success, got: unpack failed. Error: %v\n", err)
88         }
89
90         want:= strings.Fields(wantPduAsStr)
91         got:= strings.Fields(response.pduPrint)
92         if len(want) != len(got) {
93                 t.Errorf("\nwant :\t[%s]\n got: \t\t[%s]\n", wantPduAsStr, response.pduPrint)
94         }
95         for i:=0; i < len(want); i++ {
96                 if strings.Compare(want[i], got[i] ) != 0 {
97                         t.Errorf("\nwant :\t[%s]\n got: \t\t[%s]\n", wantPduAsStr, strings.TrimSpace(response.pduPrint))
98                 }
99
100         }
101 }
102
103 /*unpacking error*/
104
105 func TestUnpackX2apSetupResponseUnpackError(t *testing.T) {
106         logger, _ := logger.InitLogger(logger.InfoLevel)
107
108         wantError := "unpacking error: #src/asn1codec_utils.c.unpack_pdu_aux - Failed to decode E2AP-PDU (consumed 0), error = 0 Success"
109         //--------------------2006002a
110         inputPayloadAsStr := "2006002b000002001500080002f82900007a8000140017000000630002f8290007ab50102002f829000001000133"
111         var payload []byte
112         _, err := fmt.Sscanf(inputPayloadAsStr, "%x", &payload)
113         if err != nil {
114                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
115         }
116
117         _, err = unpackX2apPduAndRefine(logger, MaxAsn1CodecAllocationBufferSize /*allocation buffer*/, len(payload), payload, MaxAsn1CodecMessageBufferSize /*message buffer*/)
118         if err != nil {
119                 if 0 != strings.Compare(fmt.Sprintf("%s", err), wantError) {
120                         t.Errorf("want failure: %s, got: %s", wantError, err)
121                 }
122         } else {
123                 t.Errorf("want failure: %s, got: success", wantError)
124
125         }
126 }
127
128