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