gerardo.laurenzi | f14a956 | 2019-10-24 07:08:34 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package common |
| 17 | |
| 18 | import ( |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 19 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
gerardo.laurenzi | f14a956 | 2019-10-24 07:08:34 +0000 | [diff] [blame] | 20 | "github.com/stretchr/testify/assert" |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 21 | "google.golang.org/grpc/codes" |
gerardo.laurenzi | f14a956 | 2019-10-24 07:08:34 +0000 | [diff] [blame] | 22 | "strconv" |
| 23 | sp "strings" |
| 24 | "testing" |
| 25 | ) |
| 26 | |
| 27 | const sim = "0123456789abcdefABCDEF" |
| 28 | const rstr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 29 | |
| 30 | func TestGetSerialNumber(t *testing.T) { |
| 31 | |
| 32 | serial := GetRandomSerialNumber() |
| 33 | assert.NotNil(t, serial) |
| 34 | |
| 35 | sparsed := sp.Split(serial, ".") |
| 36 | sparsed2 := sparsed[3] |
| 37 | |
| 38 | for i := 0; i <= 2; i++ { |
| 39 | ioct, _ := strconv.ParseInt(sparsed[i], 10, 0) |
| 40 | |
| 41 | assert.True(t, ioct <= 255 && ioct >= 0, "Octect %d IP octect wrong!", i) |
| 42 | } |
| 43 | |
| 44 | sp3 := sp.Split(sparsed2, ":") |
| 45 | oct4, _ := strconv.ParseInt(sp3[0], 10, 0) |
| 46 | |
| 47 | assert.True(t, oct4 <= 255 && oct4 >= 0, "Fourth IP octect wrong!") |
| 48 | |
| 49 | port, _ := strconv.ParseInt(sp3[1], 10, 0) |
| 50 | assert.True(t, port <= 65535 && port >= 0) |
| 51 | } |
| 52 | func TestGetString(t *testing.T) { |
| 53 | str := GetRandomString(20) |
| 54 | strslide := sp.Split(str, "") |
| 55 | for i := 0; i < len(strslide); i++ { |
| 56 | assert.True(t, sp.Contains(rstr, strslide[i]), "Error! The string doesn't appears correct --> %s Expected in --> %s", str, rstr) |
| 57 | } |
| 58 | assert.NotNil(t, str) |
| 59 | } |
| 60 | func TestGetMacAddress(t *testing.T) { |
| 61 | mac := GetRandomMacAddress() |
| 62 | assert.NotNil(t, mac, "Mac address null") |
| 63 | smac := sp.Split(mac, ":") |
| 64 | assert.True(t, len(smac) == 6, "mac address not correctly formatted") |
| 65 | |
| 66 | for i := 0; i < len(smac); i++ { |
| 67 | oct := sp.Split(smac[i], "") |
| 68 | assert.True(t, sp.Contains(sim, oct[0])) |
| 69 | assert.True(t, sp.Contains(sim, oct[1])) |
| 70 | } |
| 71 | |
| 72 | } |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 73 | |
| 74 | func TestICProxyErrorCodeToGrpcErrorCode(t *testing.T) { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 75 | unsupported := ICProxyErrorCodeToGrpcErrorCode(ic.ErrorCode_UNSUPPORTED_REQUEST) |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 76 | assert.Equal(t, unsupported, codes.Unavailable) |
| 77 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 78 | invalid := ICProxyErrorCodeToGrpcErrorCode(ic.ErrorCode_INVALID_PARAMETERS) |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 79 | assert.Equal(t, invalid, codes.InvalidArgument) |
| 80 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 81 | timeout := ICProxyErrorCodeToGrpcErrorCode(ic.ErrorCode_DEADLINE_EXCEEDED) |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 82 | assert.Equal(t, timeout, codes.DeadlineExceeded) |
| 83 | } |