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