230dd6c5101827be102ac6ea62dff3eb0c5c1d58
[ric-plt/resource-status-manager.git] / RSM / 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         "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 TestUnpackX2apPduResponse(t *testing.T) {
34         logger, _ := logger.InitLogger(logger.DebugLevel)
35         unpacker := NewX2apPduUnpacker(logger)
36
37         wantPduAsStr := `UnsuccessfulOutcome ::= {
38     procedureCode: 9
39     criticality: 0 (reject)
40     value: ResourceStatusFailure ::= {
41         protocolIEs: ProtocolIE-Container ::= {
42             ResourceStatusFailure-IEs ::= {
43                 id: 39
44                 criticality: 0 (reject)
45                 value: 15
46             }
47             ResourceStatusFailure-IEs ::= {
48                 id: 40
49                 criticality: 0 (reject)
50                 value: 13
51             }
52             ResourceStatusFailure-IEs ::= {
53                 id: 5
54                 criticality: 1 (ignore)
55                 value: 1 (hardware-failure)
56             }
57             ResourceStatusFailure-IEs ::= {
58                 id: 68
59                 criticality: 1 (ignore)
60                 value: CompleteFailureCauseInformation-List ::= {
61                     ProtocolIE-Single-Container ::= {
62                         id: 69
63                         criticality: 1 (ignore)
64                         value: CompleteFailureCauseInformation-Item ::= {
65                             cell-ID: ECGI ::= {
66                                 pLMN-Identity: 02 F8 29
67                                 eUTRANcellIdentifier: 00 07 AB 50 (4 bits unused)
68                             }
69                             measurementFailureCause-List: MeasurementFailureCause-List ::= {
70                                 ProtocolIE-Single-Container ::= {
71                                     id: 67
72                                     criticality: 1 (ignore)
73                                     value: MeasurementFailureCause-Item ::= {
74                                         measurementFailedReportCharacteristics: 00 00 00 07
75                                         cause: 0 (transfer-syntax-error)
76                                     }
77                                 }
78                             }
79                         }
80                     }
81                 }
82             }
83         }
84     }
85 }`
86
87         inputPayloadAsStr := "400900320000040027000300000e0028000300000c00054001620044401800004540130002f8290007ab500000434006000000000740"
88         var payload []byte
89
90         _, err := fmt.Sscanf(inputPayloadAsStr, "%x", &payload)
91         if err != nil {
92                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
93         }
94
95         response, err := unpacker.UnpackX2apPduAsString(len(payload), payload, e2pdus.MaxAsn1CodecMessageBufferSize /*message buffer*/)
96         if err != nil {
97                 t.Errorf("want: success, got: unpack failed. Error: %v\n", err)
98         }
99
100         want := strings.Fields(wantPduAsStr)
101         got := strings.Fields(response)
102         if len(want) != len(got) {
103                 t.Errorf("\nwant :\t[%s]\n got: \t\t[%s]\n", wantPduAsStr, response)
104         }
105         for i := 0; i < len(want); i++ {
106                 if strings.Compare(want[i], got[i]) != 0 {
107                         t.Errorf("\nwant :\t[%s]\n got: \t\t[%s]\n", wantPduAsStr, strings.TrimSpace(response))
108                 }
109
110         }
111 }
112
113 /*unpacking error*/
114
115 func TestUnpackX2apPduError(t *testing.T) {
116         logger, _ := logger.InitLogger(logger.InfoLevel)
117         unpacker := NewX2apPduUnpacker(logger)
118
119         wantError := "unpacking error: #src/asn1codec_utils.c.unpack_pdu_aux - Failed to decode E2AP-PDU (consumed 0), error = 0 Success"
120         //--------------------2006002a
121         inputPayloadAsStr := "2006002b000002001500080002f82900007a8000140017000000630002f8290007ab50102002f829000001000133"
122         var payload []byte
123         _, err := fmt.Sscanf(inputPayloadAsStr, "%x", &payload)
124         if err != nil {
125                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
126         }
127
128         _, err = unpacker.UnpackX2apPduAsString(len(payload), payload, e2pdus.MaxAsn1CodecMessageBufferSize /*message buffer*/)
129         if err != nil {
130                 if 0 != strings.Compare(fmt.Sprintf("%s", err), wantError) {
131                         t.Errorf("want failure: %s, got: %s", wantError, err)
132                 }
133         } else {
134                 t.Errorf("want failure: %s, got: success", wantError)
135
136         }
137 }