[RICPLT-2048] X2 ENDC Setup request refactoring
[ric-plt/e2mgr.git] / E2Manager / e2pdus / endc_x2_setup_request_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         "fmt"
22         "strings"
23         "testing"
24 )
25
26 /*
27  * Create and pack an x2ap setup request.
28  * Verify the packed representation matches the want value.
29  */
30 func TestPackEndcX2apSetupRequest(t *testing.T) {
31         pLMNId := []byte{0xbb, 0xbc, 0xcc}
32         ricFlag := []byte{0xbb, 0xbc, 0xcc} /*pLMNId [3]bytes*/
33
34         var testCases = []struct {
35                 eNBId       []byte
36                 eNBIdBitqty uint
37                 packedPdu   string
38         }{
39                 {
40                         eNBId:       []byte{0xab, 0xcd, 0x2}, /*00000010 -> 10000000*/
41                         eNBIdBitqty: ShortMacro_eNB_ID,
42                         packedPdu:   "0024003200000100f4002b0000020015000900bbbccc8003abcd8000fa0017000001f700bbbcccabcd80000000bbbccc000000000001",
43                 },
44
45                 {
46                         eNBId:       []byte{0xab, 0xcd, 0xe},
47                         eNBIdBitqty: Macro_eNB_ID,
48                         packedPdu:   "0024003100000100f4002a0000020015000800bbbccc00abcde000fa0017000001f700bbbcccabcde0000000bbbccc000000000001",
49                 },
50                 {
51                         eNBId:       []byte{0xab, 0xcd, 0x7}, /*00000111 -> 00111000*/
52                         eNBIdBitqty: LongMacro_eNB_ID,
53                         //packedPdu: "0024003200000100f4002b0000020015000900bbbccc8103abcd3800fa0017000001f700bbbcccabcd38000000bbbccc000000000001",
54                         packedPdu: "0024003200000100f4002b0000020015000900bbbcccc003abcd3800fa0017000001f700bbbcccabcd38000000bbbccc000000000001",
55                 },
56                 {
57                         eNBId:       []byte{0xab, 0xcd, 0xef, 0x8},
58                         eNBIdBitqty: Home_eNB_ID,
59                         packedPdu:   "0024003200000100f4002b0000020015000900bbbccc40abcdef8000fa0017000001f700bbbcccabcdef800000bbbccc000000000001",
60                 },
61         }
62
63         for _, tc := range testCases {
64                 t.Run(tc.packedPdu, func(t *testing.T) {
65
66                         payload, _, err := preparePackedEndcX2SetupRequest(MaxAsn1PackedBufferSize /*max packed buffer*/, MaxAsn1CodecMessageBufferSize /*max message buffer*/, pLMNId, tc.eNBId, tc.eNBIdBitqty, ricFlag)
67                         if err != nil {
68                                 t.Errorf("want: success, got: pack failed. Error: %v\n", err)
69                         } else {
70                                 t.Logf("packed X2AP setup request(size=%d): %x\n", len(payload), payload)
71                                 tmp := fmt.Sprintf("%x", payload)
72                                 if len(tmp) != len(tc.packedPdu) {
73                                         t.Errorf("want packed len:%d, got: %d\n", len(tc.packedPdu)/2, len(payload)/2)
74                                 }
75
76                                 if strings.Compare(tmp, tc.packedPdu) != 0 {
77                                         t.Errorf("\nwant :\t[%s]\n got: \t\t[%s]\n", tc.packedPdu, tmp)
78                                 }
79                         }
80                 })
81         }
82 }
83
84 /*Packing error*/
85
86 func TestPackEndcX2apSetupRequestPackError(t *testing.T) {
87         pLMNId := []byte{0xbb, 0xbc, 0xcc}
88         ricFlag := []byte{0xbb, 0xbc, 0xcc} /*pLMNId [3]bytes*/
89         eNBId := []byte{0xab, 0xcd, 0x2}
90         eNBIdBitqty := uint(Macro_eNB_ID)
91         wantError := "packing error: #src/asn1codec_utils.c.pack_pdu_aux - Encoded output of E2AP-PDU, is too big:53"
92
93         _, _, err := preparePackedEndcX2SetupRequest(40 /*max packed buffer*/, MaxAsn1CodecMessageBufferSize /*max message buffer*/, pLMNId, eNBId, eNBIdBitqty, ricFlag)
94         if err != nil {
95                 if 0 != strings.Compare(fmt.Sprintf("%s", err), wantError) {
96                         t.Errorf("want failure: %s, got: %s", wantError, err)
97                 }
98         } else {
99                 t.Errorf("want failure: %s, got: success", wantError)
100
101         }
102 }