Skip to content

Commit cd8fffc

Browse files
RandMAC() added. Generates a random mac address. (#479)
* Added a LastSeen value to session and added automatic updating of it. Also added an Active param to the Session struct and changed the logic to RemoveSession(s) functions to set this value to false instead of deleting it * added RandMac * changed case * added docs
1 parent 6b3dcde commit cd8fffc

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

random/random.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ func RandLettersRange(rangeMin int, rangeMax int) string {
8787
return RandLetters(RandIntRange(rangeMin, rangeMax-1))
8888
}
8989

90+
// RandMAC generates a random MAC address and returns it
91+
func RandMAC() string {
92+
parts := []string{}
93+
for range 6 {
94+
parts = append(parts, RandHex(2))
95+
}
96+
return strings.Join(parts, ":")
97+
}
98+
9099
func RandHex(n int) string {
91100
runeSlice := make([]rune, n)
92101
for i := range runeSlice {

random/random_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ func Test_RandDomain(t *testing.T) {
155155
}
156156
}
157157

158+
func Test_RandMAC(t *testing.T) {
159+
for range 100 {
160+
r := RandMAC()
161+
if len(r) != 17 {
162+
t.Error("Mac address generated with an unexpected length: " + r)
163+
}
164+
if !strings.Contains(r, ":") {
165+
t.Error("Mac address generated without expected characters: " + r)
166+
}
167+
}
168+
}
169+
158170
func Test_RandEmail(t *testing.T) {
159171
for range 100 {
160172
r := RandEmail()

0 commit comments

Comments
 (0)