fd6648008b9dc8fda1131308769f95d5992354ea
[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         "fmt"
23         "strings"
24         "testing"
25 )
26
27 func TestParseRicId(t *testing.T) {
28         var testCases = []struct {
29                 ricId       string
30                 pLMNId      []byte
31                 eNBId       []byte
32                 eNBIdBitqty uint
33                 failure     error
34         }{
35                 {
36                         ricId:       "bbbccc-abcd02/18",
37                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
38                         eNBId:       []byte{0xab, 0xcd, 0x2}, /*00000010 -> 10000000*/
39                         eNBIdBitqty: ShortMacro_eNB_ID,
40                 },
41                 {
42                         ricId:       "bbbccc-abcd0e/20",
43                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
44                         eNBId:       []byte{0xab, 0xcd, 0xe},
45                         eNBIdBitqty: Macro_eNB_ID,
46                 },
47                 {
48                         ricId:       "bbbccc-abcd07/21",
49                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
50                         eNBId:       []byte{0xab, 0xcd, 0x7}, /*00000111 -> 00111000*/
51                         eNBIdBitqty: LongMacro_eNB_ID,
52                 },
53                 {
54                         ricId:       "bbbccc-abcdef08/28",
55                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
56                         eNBId:       []byte{0xab, 0xcd, 0xef, 0x8},
57                         eNBIdBitqty: Home_eNB_ID,
58                 },
59                 {
60                         ricId:   "",
61                         failure: fmt.Errorf("unable to extract the value of RIC_ID: EOF"),
62                 },
63
64                 {
65                         ricId:   "bbbccc",
66                         failure: fmt.Errorf("unable to extract the value of RIC_ID: unexpected EOF"),
67                 },
68                 {
69                         ricId:   "bbbccc-",
70                         failure: fmt.Errorf("unable to extract the value of RIC_ID: EOF"),
71                 },
72                 {
73                         ricId:   "-bbbccc",
74                         failure: fmt.Errorf("%s", "unable to extract the value of RIC_ID: no hex data for %x string"),
75                 },
76                 {
77                         ricId:   "/20",
78                         failure: fmt.Errorf("%s", "unable to extract the value of RIC_ID: no hex data for %x string"),
79                 },
80                 {
81                         ricId:   "bbbcccdd-abcdef08/28", // pLMNId too long
82                         failure: fmt.Errorf("unable to extract the value of RIC_ID: input does not match format"),
83                 },
84                 {
85                         ricId:   "bbbccc-abcdef0809/28", // eNBId too long
86                         failure: fmt.Errorf("unable to extract the value of RIC_ID: input does not match format"),
87                 },
88
89                 {
90                         ricId:   "bbbc-abcdef08/28", // pLMNId too short
91                         failure: fmt.Errorf("invalid value for RIC_ID, len(pLMNId:[187 188]) != 3"),
92                 },
93                 {
94                         ricId:   "bbbccc-abcd/28", // eNBId too short
95                         failure: fmt.Errorf("invalid value for RIC_ID, len(eNBId:[171 205]) != 3 or 4"),
96                 },
97                 {
98                         ricId:   "bbbccc-abcdef08/239", // bit quantity too long - no error, will return 23 (which is invalid)
99                         failure: fmt.Errorf("invalid value for RIC_ID, eNBIdBitqty: 23"),
100                 },
101         }
102
103         for _, tc := range testCases {
104                 t.Run(tc.ricId, func(t *testing.T) {
105
106                         err := parseRicID(tc.ricId)
107                         if err != nil {
108                                 if tc.failure == nil {
109                                         t.Errorf("want: success, got: parse failed. Error: %v\n", err)
110                                 } else {
111                                         if strings.Compare(err.Error(), tc.failure.Error()) != 0 {
112                                                 t.Errorf("want: %s, got: %s\n", err, tc.failure)
113                                         }
114                                 }
115                         } else {
116                                 if bytes.Compare(tc.pLMNId, pLMNId) != 0 {
117                                         t.Errorf("want: pLMNId = %v, got: pLMNId = %v", tc.pLMNId, pLMNId)
118                                 }
119
120                                 if bytes.Compare(tc.eNBId, eNBId) != 0 {
121                                         t.Errorf("want: eNBId = %v, got: eNBId = %v", tc.eNBId, eNBId)
122                                 }
123
124                                 if tc.eNBIdBitqty != eNBIdBitqty {
125                                         t.Errorf("want: eNBIdBitqty = %d, got: eNBIdBitqty = %d", tc.eNBIdBitqty, eNBIdBitqty)
126                                 }
127                         }
128                 })
129         }
130 }