blob: 59a8df2a7e3456e66c4754a2555c22eddee3057f [file] [log] [blame]
gerardo.laurenzif14a9562019-10-24 07:08:34 +00001/*
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05002* Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
gerardo.laurenzif14a9562019-10-24 07:08:34 +00003
Joey Armstrong7f8436c2023-07-09 20:23:27 -04004* 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
gerardo.laurenzif14a9562019-10-24 07:08:34 +00007
Joey Armstrong7f8436c2023-07-09 20:23:27 -04008* http://www.apache.org/licenses/LICENSE-2.0
gerardo.laurenzif14a9562019-10-24 07:08:34 +00009
Joey Armstrong7f8436c2023-07-09 20:23:27 -040010* 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.
gerardo.laurenzif14a9562019-10-24 07:08:34 +000015 */
16package common
17
18import (
19 "github.com/stretchr/testify/assert"
20 "strconv"
21 sp "strings"
22 "testing"
23)
24
25const sim = "0123456789abcdefABCDEF"
26const rstr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
27
28func TestGetSerialNumber(t *testing.T) {
29
30 serial := GetRandomSerialNumber()
31 assert.NotNil(t, serial)
32
33 sparsed := sp.Split(serial, ".")
34 sparsed2 := sparsed[3]
35
36 for i := 0; i <= 2; i++ {
37 ioct, _ := strconv.ParseInt(sparsed[i], 10, 0)
38
39 assert.True(t, ioct <= 255 && ioct >= 0, "Octect %d IP octect wrong!", i)
40 }
41
42 sp3 := sp.Split(sparsed2, ":")
43 oct4, _ := strconv.ParseInt(sp3[0], 10, 0)
44
45 assert.True(t, oct4 <= 255 && oct4 >= 0, "Fourth IP octect wrong!")
46
47 port, _ := strconv.ParseInt(sp3[1], 10, 0)
48 assert.True(t, port <= 65535 && port >= 0)
49}
50func TestGetString(t *testing.T) {
51 str := GetRandomString(20)
52 strslide := sp.Split(str, "")
53 for i := 0; i < len(strslide); i++ {
54 assert.True(t, sp.Contains(rstr, strslide[i]), "Error! The string doesn't appears correct --> %s Expected in --> %s", str, rstr)
55 }
56 assert.NotNil(t, str)
57}
58func TestGetMacAddress(t *testing.T) {
59 mac := GetRandomMacAddress()
60 assert.NotNil(t, mac, "Mac address null")
61 smac := sp.Split(mac, ":")
62 assert.True(t, len(smac) == 6, "mac address not correctly formatted")
63
64 for i := 0; i < len(smac); i++ {
65 oct := sp.Split(smac[i], "")
66 assert.True(t, sp.Contains(sim, oct[0]))
67 assert.True(t, sp.Contains(sim, oct[1]))
68 }
69
70}