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