[RIC-213] Add globalRicId to configuration and remove env var RIC_ID
[ric-plt/e2mgr.git] / E2Manager / e2pdus / x2_setup_request_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         "fmt"
26         "strings"
27         "testing"
28 )
29
30 /*
31  * Create and pack an x2ap setup request.
32  * Verify the packed representation matches the want value.
33  */
34 func TestPackX2apSetupRequest(t *testing.T) {
35         pLMNId := []byte{0xbb, 0xbc, 0xcc}
36         ricFlag := []byte{0xbb, 0xbc, 0xcc} /*pLMNId [3]bytes*/
37
38         var testCases = []struct {
39                 eNBId       []byte
40                 eNBIdBitqty uint
41                 packedPdu   string
42         }{
43                 {
44                         eNBId:       []byte{0xab, 0xcd, 0x2}, /*00000010 -> 10000000*/
45                         eNBIdBitqty: ShortMacro_eNB_ID,
46                         packedPdu:   "0006002b0000020015000900bbbccc8003abcd8000140017000001f700bbbcccabcd80000000bbbccc000000000001",
47                 },
48
49                 {
50                         eNBId:       []byte{0xab, 0xcd, 0xe},
51                         eNBIdBitqty: Macro_eNB_ID,
52                         packedPdu:   "0006002a0000020015000800bbbccc00abcde000140017000001f700bbbcccabcde0000000bbbccc000000000001",
53                 },
54                 {
55                         eNBId:       []byte{0xab, 0xcd, 0x7}, /*00000111 -> 00111000*/
56                         eNBIdBitqty: LongMacro_eNB_ID,
57                         //packedPdu:   "0006002b0000020015000900bbbccc8103abcd3800140017000001f700bbbcccabcd38000000bbbccc000000000001",
58                         packedPdu:   "0006002b0000020015000900bbbcccc003abcd3800140017000001f700bbbcccabcd38000000bbbccc000000000001",
59                 },
60                 {
61                         eNBId:       []byte{0xab, 0xcd, 0xef, 0x8},
62                         eNBIdBitqty: Home_eNB_ID,
63                         packedPdu:   "0006002b0000020015000900bbbccc40abcdef8000140017000001f700bbbcccabcdef800000bbbccc000000000001",
64                 },
65         }
66
67         // TODO: Consider using testify's assert/require
68         // testing/quick to input random value
69         for _, tc := range testCases {
70                 t.Run(tc.packedPdu, func(t *testing.T) {
71
72                         payload, _, err := preparePackedX2SetupRequest(MaxAsn1PackedBufferSize /*max packed buffer*/, MaxAsn1CodecMessageBufferSize /*max message buffer*/, pLMNId, tc.eNBId, tc.eNBIdBitqty,ricFlag)
73                         if err != nil {
74                                 t.Errorf("want: success, got: pack failed. Error: %v\n", err)
75                         } else {
76                                 t.Logf("packed X2AP setup request(size=%d): %x\n", len(payload), payload)
77                                 tmp := fmt.Sprintf("%x", payload)
78                                 if len(tmp) != len(tc.packedPdu) {
79                                         t.Errorf("want packed len:%d, got: %d\n", len(tc.packedPdu)/2, len(payload)/2)
80                                 }
81
82                                 if strings.Compare(tmp, tc.packedPdu) != 0 {
83                                         t.Errorf("\nwant :\t[%s]\n got: \t\t[%s]\n", tc.packedPdu, tmp)
84                                 }
85                         }
86                 })
87         }
88 }
89
90 /*Packing error*/
91
92 func TestPackX2apSetupRequestPackError(t *testing.T) {
93
94         wantError := "packing error: #src/asn1codec_utils.c.pack_pdu_aux - Encoded output of E2AP-PDU, is too big:46"
95         pLMNId := []byte{0xbb, 0xbc, 0xcc}
96         ricFlag := []byte{0xbb, 0xbc, 0xcc} /*pLMNId [3]bytes*/
97         eNBId := []byte{0xab, 0xcd, 0xe}
98         eNBIdBitqty := uint(Macro_eNB_ID)
99         _, _, err := preparePackedX2SetupRequest(40 /*max packed buffer*/, MaxAsn1CodecMessageBufferSize /*max message buffer*/, pLMNId, eNBId, eNBIdBitqty, ricFlag)
100         if err != nil {
101                 if 0 != strings.Compare(fmt.Sprintf("%s", err), wantError) {
102                         t.Errorf("want failure: %s, got: %s", wantError, err)
103                 }
104         } else {
105                 t.Errorf("want failure: %s, got: success", wantError)
106
107         }
108 }