[RICPLT-2048] X2 ENDC Setup request refactoring - unit test added
[ric-plt/e2mgr.git] / E2Manager / e2pdus / x2_setup_requests_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 e2pdus
19
20 import (
21         "bytes"
22         "e2mgr/logger"
23         "fmt"
24         "strings"
25         "testing"
26 )
27
28 func TestPreparePackedEndcX2SetupRequest(t *testing.T) {
29         _,err := logger.InitLogger(logger.InfoLevel)
30         if err!=nil{
31                 t.Errorf("failed to initialize logger, error: %s", err)
32         }
33         packedPdu := "0024003100000100f4002a0000020015000800bbbccc00abcde000fa0017000001f700bbbcccabcde0000000bbbccc000000000001"
34         packedEndcX2setupRequest := PackedEndcX2setupRequest
35
36         tmp := fmt.Sprintf("%x", packedEndcX2setupRequest)
37         if len(tmp) != len(packedPdu) {
38                 t.Errorf("want packed len:%d, got: %d\n", len(packedPdu)/2, len(packedEndcX2setupRequest)/2)
39         }
40
41         if strings.Compare(tmp, packedPdu) != 0 {
42                 t.Errorf("\nwant :\t[%s]\n got: \t\t[%s]\n", packedPdu, tmp)
43         }
44 }
45
46 func TestPreparePackedX2SetupRequest(t *testing.T) {
47         _,err := logger.InitLogger(logger.InfoLevel)
48         if err!=nil{
49                 t.Errorf("failed to initialize logger, error: %s", err)
50         }
51         packedPdu := "0006002a0000020015000800bbbccc00abcde000140017000001f700bbbcccabcde0000000bbbccc000000000001"
52         packedX2setupRequest := PackedX2setupRequest
53
54         tmp := fmt.Sprintf("%x", packedX2setupRequest)
55         if len(tmp) != len(packedPdu) {
56                 t.Errorf("want packed len:%d, got: %d\n", len(packedPdu)/2, len(packedX2setupRequest)/2)
57         }
58
59         if strings.Compare(tmp, packedPdu) != 0 {
60                 t.Errorf("\nwant :\t[%s]\n got: \t\t[%s]\n", packedPdu, tmp)
61         }
62 }
63
64 func TestPreparePackedX2SetupRequestFailure(t *testing.T) {
65         _, err := logger.InitLogger(logger.InfoLevel)
66         if err != nil {
67                 t.Errorf("failed to initialize logger, error: %s", err)
68         }
69
70         _, _, err  = preparePackedX2SetupRequest(1, 4096, pLMNId, eNBId, eNBIdBitqty, ricFlag)
71         if err == nil {
72                 t.Errorf("want: error, got: success.\n")
73         }
74
75         expected:= "packing error: #src/asn1codec_utils.c.pack_pdu_aux - Encoded output of E2AP-PDU, is too big"
76         if !strings.Contains(err.Error(), expected) {
77                 t.Errorf("want :[%s], got: [%s]\n", expected, err)
78         }
79 }
80
81 func TestPreparePackedEndcSetupRequestFailure(t *testing.T) {
82         _, err := logger.InitLogger(logger.InfoLevel)
83         if err != nil {
84                 t.Errorf("failed to initialize logger, error: %s", err)
85         }
86
87         _, _, err  = preparePackedEndcX2SetupRequest(1, 4096, pLMNId, eNBId, eNBIdBitqty, ricFlag)
88         if err == nil {
89                 t.Errorf("want: error, got: success.\n")
90         }
91
92         expected:= "packing error: #src/asn1codec_utils.c.pack_pdu_aux - Encoded output of E2AP-PDU, is too big"
93         if !strings.Contains(err.Error(), expected) {
94                 t.Errorf("want :[%s], got: [%s]\n", expected, err)
95         }
96 }
97
98 func TestParseRicId(t *testing.T) {
99         var testCases = []struct {
100                 ricId       string
101                 pLMNId      []byte
102                 eNBId       []byte
103                 eNBIdBitqty uint
104                 failure     error
105         }{
106                 {
107                         ricId:       "bbbccc-abcd02/18",
108                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
109                         eNBId:       []byte{0xab, 0xcd, 0x2}, /*00000010 -> 10000000*/
110                         eNBIdBitqty: ShortMacro_eNB_ID,
111                 },
112                 {
113                         ricId:       "bbbccc-abcd0e/20",
114                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
115                         eNBId:       []byte{0xab, 0xcd, 0xe},
116                         eNBIdBitqty: Macro_eNB_ID,
117                 },
118                 {
119                         ricId:       "bbbccc-abcd07/21",
120                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
121                         eNBId:       []byte{0xab, 0xcd, 0x7}, /*00000111 -> 00111000*/
122                         eNBIdBitqty: LongMacro_eNB_ID,
123                 },
124                 {
125                         ricId:       "bbbccc-abcdef08/28",
126                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
127                         eNBId:       []byte{0xab, 0xcd, 0xef, 0x8},
128                         eNBIdBitqty: Home_eNB_ID,
129                 },
130                 {
131                         ricId:   "",
132                         failure: fmt.Errorf("unable to extract the value of RIC_ID: EOF"),
133                 },
134
135                 {
136                         ricId:   "bbbccc",
137                         failure: fmt.Errorf("unable to extract the value of RIC_ID: unexpected EOF"),
138                 },
139                 {
140                         ricId:   "bbbccc-",
141                         failure: fmt.Errorf("unable to extract the value of RIC_ID: EOF"),
142                 },
143                 {
144                         ricId:   "-bbbccc",
145                         failure: fmt.Errorf("%s", "unable to extract the value of RIC_ID: no hex data for %x string"),
146                 },
147                 {
148                         ricId:   "/20",
149                         failure: fmt.Errorf("%s", "unable to extract the value of RIC_ID: no hex data for %x string"),
150                 },
151                 {
152                         ricId:   "bbbcccdd-abcdef08/28", // pLMNId too long
153                         failure: fmt.Errorf("unable to extract the value of RIC_ID: input does not match format"),
154                 },
155                 {
156                         ricId:   "bbbccc-abcdef0809/28", // eNBId too long
157                         failure: fmt.Errorf("unable to extract the value of RIC_ID: input does not match format"),
158                 },
159
160                 {
161                         ricId:   "bbbc-abcdef08/28", // pLMNId too short
162                         failure: fmt.Errorf("invalid value for RIC_ID, len(pLMNId:[187 188]) != 3"),
163                 },
164                 {
165                         ricId:   "bbbccc-abcd/28", // eNBId too short
166                         failure: fmt.Errorf("invalid value for RIC_ID, len(eNBId:[171 205]) != 3 or 4"),
167                 },
168                 {
169                         ricId:   "bbbccc-abcdef08/239", // bit quantity too long - no error, will return 23 (which is invalid)
170                         failure: fmt.Errorf("invalid value for RIC_ID, eNBIdBitqty: 23"),
171                 },
172         }
173
174         for _, tc := range testCases {
175                 t.Run(tc.ricId, func(t *testing.T) {
176
177                         err := parseRicID(tc.ricId)
178                         if err != nil {
179                                 if tc.failure == nil {
180                                         t.Errorf("want: success, got: parse failed. Error: %v\n", err)
181                                 } else {
182                                         if strings.Compare(err.Error(), tc.failure.Error()) != 0 {
183                                                 t.Errorf("want: %s, got: %s\n", err, tc.failure)
184                                         }
185                                 }
186                         } else {
187                                 if bytes.Compare(tc.pLMNId, pLMNId) != 0 {
188                                         t.Errorf("want: pLMNId = %v, got: pLMNId = %v", tc.pLMNId, pLMNId)
189                                 }
190
191                                 if bytes.Compare(tc.eNBId, eNBId) != 0 {
192                                         t.Errorf("want: eNBId = %v, got: eNBId = %v", tc.eNBId, eNBId)
193                                 }
194
195                                 if tc.eNBIdBitqty != eNBIdBitqty {
196                                         t.Errorf("want: eNBIdBitqty = %d, got: eNBIdBitqty = %d", tc.eNBIdBitqty, eNBIdBitqty)
197                                 }
198                         }
199                 })
200         }
201 }