114afd4a493d355d13d49d0bae91594885dc5368
[ric-plt/resource-status-manager.git] / RSM / converters / resource_status_response_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         "fmt"
22         "rsm/e2pdus"
23         "rsm/logger"
24         "strings"
25         "testing"
26 )
27
28 /*
29  * Unpack a response returned from RAN.
30  * Verify it matches the want pdu.
31  */
32
33 func TestResourceStatusResponseConverter(t *testing.T) {
34         logger, _ := logger.InitLogger(logger.DebugLevel)
35         unpacker := NewX2apPduUnpacker(logger, e2pdus.MaxAsn1CodecMessageBufferSize)
36         rsConverters := NewResourceStatusResponseConverter(unpacker)
37
38         var testCases = []struct {
39                 response  string
40                 packedPdu string
41         }{
42                 {
43                         response: "ENB1_Measurement_ID: 15, ENB2_Measurement_ID: 13, MeasurementInitiationResults:[ CellId: 02f829:0007ab50, MeasurementFailureCauses: [  ]  ]",
44                         /*
45                                 SuccessfulOutcome ::= {
46                                         procedureCode: 9
47                                         criticality: 0 (reject)
48                                         value: ResourceStatusResponse ::= {
49                                                 protocolIEs: ProtocolIE-Container ::= {
50                                                         ResourceStatusResponse-IEs ::= {
51                                                                 id: 39
52                                                                 criticality: 0 (reject)
53                                                                 value: 15
54                                                         }
55                                                         ResourceStatusResponse-IEs ::= {
56                                                                 id: 40
57                                                                 criticality: 0 (reject)
58                                                                 value: 13
59                                                         }
60                                                         ResourceStatusResponse-IEs ::= {
61                                                                 id: 65
62                                                                 criticality: 1 (ignore)
63                                                                 value: MeasurementInitiationResult-List ::= {
64                                                                         ProtocolIE-Single-Container ::= {
65                                                                                 id: 66
66                                                                                 criticality: 1 (ignore)
67                                                                                 value: MeasurementInitiationResult-Item ::= {
68                                                                                         cell-ID: ECGI ::= {
69                                                                                                 pLMN-Identity: 02 F8 29
70                                                                                                 eUTRANcellIdentifier: 00 07 AB 50 (4 bits unused)
71                                                                                         }
72                                                                                 }
73                                                                         }
74                                                                 }
75                                                         }
76                                                 }
77                                         }
78                                 }
79                         */
80
81                         packedPdu: "200900220000030027000300000e0028000300000c0041400d00004240080002f8290007ab50",
82                 },
83                 {response: "ENB1_Measurement_ID: 1, ENB2_Measurement_ID: 75, MeasurementInitiationResults:[ CellId: 133023:02b030a0, MeasurementFailureCauses: [ MeasurementFailedReportCharacteristics: 02000000 MeasurementFailedReportCharacteristics: 04000000 MeasurementFailedReportCharacteristics: 08000000 MeasurementFailedReportCharacteristics: 20000000 MeasurementFailedReportCharacteristics: 40000000 MeasurementFailedReportCharacteristics: 80000000  ]  ]",
84                         /*
85                                  SuccessfulOutcome ::= {
86                                     procedureCode: 9
87                                     criticality: 0 (reject)
88                                     value: ResourceStatusResponse ::= {
89                                         protocolIEs: ProtocolIE-Container ::= {
90                                             ResourceStatusResponse-IEs ::= {
91                                                 id: 39
92                                                 criticality: 0 (reject)
93                                                 value: 1
94                                             }
95                                             ResourceStatusResponse-IEs ::= {
96                                                 id: 40
97                                                 criticality: 0 (reject)
98                                                 value: 75
99                                             }
100                                             ResourceStatusResponse-IEs ::= {
101                                                 id: 65
102                                                 criticality: 1 (ignore)
103                                                 value: MeasurementInitiationResult-List ::= {
104                                                     ProtocolIE-Single-Container ::= {
105                                                         id: 66
106                                                         criticality: 1 (ignore)
107                                                         value: MeasurementInitiationResult-Item ::= {
108                                                             cell-ID: ECGI ::= {
109                                                                 pLMN-Identity: 13 30 23
110                                                                 eUTRANcellIdentifier: 02 B0 30 A0 (4 bits unused)
111                                                             }
112                                                             measurementFailureCause-List: MeasurementFailureCause-List ::= {
113                                                                 ProtocolIE-Single-Container ::= {
114                                                                     id: 67
115                                                                     criticality: 1 (ignore)
116                                                                     value: MeasurementFailureCause-Item ::= {
117                                                                         measurementFailedReportCharacteristics: 02 00 00 00
118                                                                         cause: 20 (measurement-temporarily-not-available)
119                                                                     }
120                                                                 }
121                                                                 ProtocolIE-Single-Container ::= {
122                                                                     id: 67
123                                                                     criticality: 1 (ignore)
124                                                                     value: MeasurementFailureCause-Item ::= {
125                                                                         measurementFailedReportCharacteristics: 04 00 00 00
126                                                                         cause: 20 (measurement-temporarily-not-available)
127                                                                     }
128                                                                 }
129                                                                 ProtocolIE-Single-Container ::= {
130                                                                     id: 67
131                                                                     criticality: 1 (ignore)
132                                                                     value: MeasurementFailureCause-Item ::= {
133                                                                         measurementFailedReportCharacteristics: 08 00 00 00
134                                                                         cause: 20 (measurement-temporarily-not-available)
135                                                                     }
136                                                                 }
137                                                                 ProtocolIE-Single-Container ::= {
138                                                                     id: 67
139                                                                     criticality: 1 (ignore)
140                                                                     value: MeasurementFailureCause-Item ::= {
141                                                                         measurementFailedReportCharacteristics: 20 00 00 00
142                                                                         cause: 20 (measurement-temporarily-not-available)
143                                                                     }
144                                                                 }
145                                                                 ProtocolIE-Single-Container ::= {
146                                                                     id: 67
147                                                                     criticality: 1 (ignore)
148                                                                     value: MeasurementFailureCause-Item ::= {
149                                                                         measurementFailedReportCharacteristics: 40 00 00 00
150                                                                         cause: 20 (measurement-temporarily-not-available)
151                                                                     }
152                                                                 }
153                                                                 ProtocolIE-Single-Container ::= {
154                                                                     id: 67
155                                                                     criticality: 1 (ignore)
156                                                                     value: MeasurementFailureCause-Item ::= {
157                                                                         measurementFailedReportCharacteristics: 80 00 00 00
158                                                                         cause: 20 (measurement-temporarily-not-available)
159                                                                     }
160                                                                 }
161                                                             }
162                                                         }
163                                                     }
164                                                 }
165                                             }
166                                         }
167                                     }
168                                 }
169                         */
170                         packedPdu: "20090065000003002700030000000028000300004a00414050000042404b4013302302b030a2800043400700020000000a000043400700040000000a000043400700080000000a000043400700200000000a000043400700400000000a000043400700800000000a00"},
171         }
172
173         for _, tc := range testCases {
174                 t.Run(tc.packedPdu, func(t *testing.T) {
175                         var payload []byte
176
177                         _, err := fmt.Sscanf(tc.packedPdu, "%x", &payload)
178                         if err != nil {
179                                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
180                         }
181
182                         response, err := rsConverters.Convert(payload)
183                         if err != nil {
184                                 t.Errorf("want: success, got: unpack failed. Error: %v\n", err)
185                         }
186
187                         got := response.String()
188                         if len(tc.response) != len(got) {
189                                 t.Errorf("\nwant :\t[%s]\n got: \t\t[%s]\n", tc.response, response)
190                         }
191                         if strings.Compare(tc.response, got) != 0 {
192                                 t.Errorf("\nwant :\t[%s]\n got: \t\t[%s]\n", tc.response, got)
193                         }
194                 })
195         }
196 }
197
198 /*unpacking error*/
199
200 func TestResourceStatusResponseConverterError(t *testing.T) {
201         logger, _ := logger.InitLogger(logger.InfoLevel)
202         unpacker := NewX2apPduUnpacker(logger, e2pdus.MaxAsn1CodecMessageBufferSize)
203         rsConverters := NewResourceStatusResponseConverter(unpacker)
204
205         wantError := "unpacking error: #src/asn1codec_utils.c.unpack_pdu_aux - Failed to decode E2AP-PDU (consumed 0), error = 0 Success"
206         //--------------------2006002a
207         inputPayloadAsStr := "2006002b000002001500080002f82900007a8000140017000000630002f8290007ab50102002f829000001000133"
208         var payload []byte
209         _, err := fmt.Sscanf(inputPayloadAsStr, "%x", &payload)
210         if err != nil {
211                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
212         }
213
214         _, err = rsConverters.Convert(payload)
215         if err != nil {
216                 if 0 != strings.Compare(fmt.Sprintf("%s", err), wantError) {
217                         t.Errorf("want failure: %s, got: %s", wantError, err)
218                 }
219         } else {
220                 t.Errorf("want failure: %s, got: success", wantError)
221
222         }
223 }
224
225 func TestResourceStatusResponseConverterPduOfFailure(t *testing.T) {
226         logger, _ := logger.InitLogger(logger.InfoLevel)
227         unpacker := NewX2apPduUnpacker(logger, e2pdus.MaxAsn1CodecMessageBufferSize)
228         rsConverters := NewResourceStatusResponseConverter(unpacker)
229
230         wantError := "unexpected PDU, 3"
231         inputPayloadAsStr := "400900170000030027000300000000280003000049000540020a80"
232         var payload []byte
233         _, err := fmt.Sscanf(inputPayloadAsStr, "%x", &payload)
234         if err != nil {
235                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
236         }
237
238         _, err = rsConverters.Convert(payload)
239         if err != nil {
240                 if 0 != strings.Compare(fmt.Sprintf("%s", err), wantError) {
241                         t.Errorf("want failure: %s, got: %s", wantError, err)
242                 }
243         } else {
244                 t.Errorf("want failure: %s, got: success", wantError)
245
246         }
247 }
248
249 func TestResourceStatusResponseConverterWrongPdu(t *testing.T) {
250         logger, _ := logger.InitLogger(logger.InfoLevel)
251         unpacker := NewX2apPduUnpacker(logger, e2pdus.MaxAsn1CodecMessageBufferSize)
252         rsConverters := NewResourceStatusResponseConverter(unpacker)
253
254         wantError := "unexpected PDU - not a resource status response"
255         inputPayloadAsStr := "2006002a000002001500080002f82900007a8000140017000000630002f8290007ab50102002f829000001000133"
256         var payload []byte
257         _, err := fmt.Sscanf(inputPayloadAsStr, "%x", &payload)
258         if err != nil {
259                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
260         }
261
262         _, err = rsConverters.Convert(payload)
263         if err != nil {
264                 if 0 != strings.Compare(fmt.Sprintf("%s", err), wantError) {
265                         t.Errorf("want failure: %s, got: %s", wantError, err)
266                 }
267         } else {
268                 t.Errorf("want failure: %s, got: success", wantError)
269
270         }
271 }