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