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