[RICPLT-2157] Restructure handlers and converters.......
[ric-plt/e2mgr.git] / E2Manager / converters / 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 converters
19
20 import (
21         "e2mgr/e2pdus"
22         "e2mgr/logger"
23         "fmt"
24         "strings"
25         "testing"
26 )
27
28 /*
29  * Unpack an x2setup response returned from RAN.
30  * Verify it matches the want pdu.
31  */
32
33 func TestUnpackX2apSetupResponse(t *testing.T) {
34         logger, _ := logger.InitLogger(logger.DebugLevel)
35
36         wantPduAsStr := `SuccessfulOutcome ::= {
37             procedureCode: 6
38             criticality: 0 (reject)
39             value: X2SetupResponse ::= {
40                 protocolIEs: ProtocolIE-Container ::= {
41                     X2SetupResponse-IEs ::= {
42                         id: 21
43                         criticality: 0 (reject)
44                         value: GlobalENB-ID ::= {
45                             pLMN-Identity: 02 F8 29
46                             eNB-ID: 00 7A 80 (4 bits unused)
47                         }
48                     }
49                     X2SetupResponse-IEs ::= {
50                         id: 20
51                         criticality: 0 (reject)
52                         value: ServedCells ::= {
53                             SEQUENCE ::= {
54                                 servedCellInfo: ServedCell-Information ::= {
55                                     pCI: 99
56                                     cellId: ECGI ::= {
57                                         pLMN-Identity: 02 F8 29
58                                         eUTRANcellIdentifier: 00 07 AB 50 (4 bits unused)
59                                     }
60                                     tAC: 01 02
61                                     broadcastPLMNs: BroadcastPLMNs-Item ::= {
62                                         02 F8 29
63                                     }
64                                     eUTRA-Mode-Info: FDD-Info ::= {
65                                         uL-EARFCN: 1
66                                         dL-EARFCN: 1
67                                         uL-Transmission-Bandwidth: 3 (bw50)
68                                         dL-Transmission-Bandwidth: 3 (bw50)
69                                     }
70                                 }
71                             }
72                         }
73                     }
74                 }
75             }
76         }`
77
78         inputPayloadAsStr := "2006002a000002001500080002f82900007a8000140017000000630002f8290007ab50102002f829000001000133"
79         var payload []byte
80
81         _, err := fmt.Sscanf(inputPayloadAsStr, "%x", &payload)
82         if err != nil {
83                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
84         }
85
86         response, err := UnpackX2apPduAndRefine(logger, e2pdus.MaxAsn1CodecAllocationBufferSize , len(payload), payload, e2pdus.MaxAsn1CodecMessageBufferSize /*message buffer*/)
87         if err != nil {
88                 t.Errorf("want: success, got: unpack failed. Error: %v\n", err)
89         }
90
91         want := strings.Fields(wantPduAsStr)
92         got := strings.Fields(response.PduPrint)
93         if len(want) != len(got) {
94                 t.Errorf("\nwant :\t[%s]\n got: \t\t[%s]\n", wantPduAsStr, response.PduPrint)
95         }
96         for i := 0; i < len(want); i++ {
97                 if strings.Compare(want[i], got[i]) != 0 {
98                         t.Errorf("\nwant :\t[%s]\n got: \t\t[%s]\n", wantPduAsStr, strings.TrimSpace(response.PduPrint))
99                 }
100
101         }
102 }
103
104 /*unpacking error*/
105
106 func TestUnpackX2apSetupResponseUnpackError(t *testing.T) {
107         logger, _ := logger.InitLogger(logger.InfoLevel)
108
109         wantError := "unpacking error: #src/asn1codec_utils.c.unpack_pdu_aux - Failed to decode E2AP-PDU (consumed 0), error = 0 Success"
110         //--------------------2006002a
111         inputPayloadAsStr := "2006002b000002001500080002f82900007a8000140017000000630002f8290007ab50102002f829000001000133"
112         var payload []byte
113         _, err := fmt.Sscanf(inputPayloadAsStr, "%x", &payload)
114         if err != nil {
115                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
116         }
117
118         _, err = UnpackX2apPduAndRefine(logger, e2pdus.MaxAsn1CodecAllocationBufferSize , len(payload), payload, e2pdus.MaxAsn1CodecMessageBufferSize /*message buffer*/)
119         if err != nil {
120                 if 0 != strings.Compare(fmt.Sprintf("%s", err), wantError) {
121                         t.Errorf("want failure: %s, got: %s", wantError, err)
122                 }
123         } else {
124                 t.Errorf("want failure: %s, got: success", wantError)
125
126         }
127 }