blob: d05162e78fdda0d95d1334a50440a77c1565254f [file] [log] [blame]
gerardo.laurenzif14a9562019-10-24 07:08:34 +00001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package common
17
18import (
Neha Sharma94f16a92020-06-26 04:17:55 +000019 "context"
Girish Gowdra89c985b2020-10-14 15:02:09 -070020 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
gerardo.laurenzif14a9562019-10-24 07:08:34 +000021 "github.com/stretchr/testify/assert"
Matteo Scandolo1530d412020-01-30 14:57:46 -080022 "google.golang.org/grpc/codes"
gerardo.laurenzif14a9562019-10-24 07:08:34 +000023 "strconv"
24 sp "strings"
25 "testing"
26)
27
28const sim = "0123456789abcdefABCDEF"
29const rstr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
30
31func TestGetSerialNumber(t *testing.T) {
32
33 serial := GetRandomSerialNumber()
34 assert.NotNil(t, serial)
35
36 sparsed := sp.Split(serial, ".")
37 sparsed2 := sparsed[3]
38
39 for i := 0; i <= 2; i++ {
40 ioct, _ := strconv.ParseInt(sparsed[i], 10, 0)
41
42 assert.True(t, ioct <= 255 && ioct >= 0, "Octect %d IP octect wrong!", i)
43 }
44
45 sp3 := sp.Split(sparsed2, ":")
46 oct4, _ := strconv.ParseInt(sp3[0], 10, 0)
47
48 assert.True(t, oct4 <= 255 && oct4 >= 0, "Fourth IP octect wrong!")
49
50 port, _ := strconv.ParseInt(sp3[1], 10, 0)
51 assert.True(t, port <= 65535 && port >= 0)
52}
53func TestGetString(t *testing.T) {
54 str := GetRandomString(20)
55 strslide := sp.Split(str, "")
56 for i := 0; i < len(strslide); i++ {
57 assert.True(t, sp.Contains(rstr, strslide[i]), "Error! The string doesn't appears correct --> %s Expected in --> %s", str, rstr)
58 }
59 assert.NotNil(t, str)
60}
61func TestGetMacAddress(t *testing.T) {
62 mac := GetRandomMacAddress()
63 assert.NotNil(t, mac, "Mac address null")
64 smac := sp.Split(mac, ":")
65 assert.True(t, len(smac) == 6, "mac address not correctly formatted")
66
67 for i := 0; i < len(smac); i++ {
68 oct := sp.Split(smac[i], "")
69 assert.True(t, sp.Contains(sim, oct[0]))
70 assert.True(t, sp.Contains(sim, oct[1]))
71 }
72
73}
Matteo Scandolo1530d412020-01-30 14:57:46 -080074
75func TestICProxyErrorCodeToGrpcErrorCode(t *testing.T) {
Neha Sharma94f16a92020-06-26 04:17:55 +000076 unsupported := ICProxyErrorCodeToGrpcErrorCode(context.Background(), ic.ErrorCode_UNSUPPORTED_REQUEST)
Matteo Scandolo1530d412020-01-30 14:57:46 -080077 assert.Equal(t, unsupported, codes.Unavailable)
78
Neha Sharma94f16a92020-06-26 04:17:55 +000079 invalid := ICProxyErrorCodeToGrpcErrorCode(context.Background(), ic.ErrorCode_INVALID_PARAMETERS)
Matteo Scandolo1530d412020-01-30 14:57:46 -080080 assert.Equal(t, invalid, codes.InvalidArgument)
81
Neha Sharma94f16a92020-06-26 04:17:55 +000082 timeout := ICProxyErrorCodeToGrpcErrorCode(context.Background(), ic.ErrorCode_DEADLINE_EXCEEDED)
Matteo Scandolo1530d412020-01-30 14:57:46 -080083 assert.Equal(t, timeout, codes.DeadlineExceeded)
84}