Skip to content

Commit

Permalink
Add mac-addr generation to Internet
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed Feb 12, 2024
1 parent 59e9ca1 commit 2205b7f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.github.serpro69.kfaker.provider

import io.github.serpro69.kfaker.Faker
import io.github.serpro69.kfaker.FakerService
import io.github.serpro69.kfaker.dictionary.YamlCategory
import io.github.serpro69.kfaker.helper.isReservedNet
import io.github.serpro69.kfaker.helper.prepare
import io.github.serpro69.kfaker.provider.unique.LocalUniqueDataProvider
import io.github.serpro69.kfaker.provider.unique.UniqueProviderDelegate
import java.lang.String.format

/**
* [FakeDataProvider] implementation for [YamlCategory.INTERNET] category.
Expand Down Expand Up @@ -53,6 +55,25 @@ class Internet internal constructor(fakerService: FakerService) : YamlFakeDataPr
return ranges.map { fakerService.randomService.nextInt(it) }.joinToString(".")
}

/**
* Returns a random mac-address with an optional [prefix]
*
* Examples:
* ```
* Faker().internet.macAddress() // => 17:12:d9:fc:fe:f6
* Faker().internet.macAddress("a") // => 0a:11:ed:7c:b5:af
* Faker().internet.macAddress("aa") // => aa:ec:eb:54:b9:f5
* Faker().internet.macAddress("aa:ce") // => aa:ce:e3:e1:83:c4
*
* ```
*/
@OptIn(ExperimentalStdlibApi::class)
fun macAddress(prefix: String = ""): String {
val pref = prefix.split(":").mapNotNull { if (it.isNotBlank()) it.hexToInt() else null }
val addr = List(6 - pref.size) { fakerService.randomService.nextInt(256) }
return listOf(pref, addr).flatten().joinToString(":") { format("%02x", it) }
}

/**
* Returns a public IPv4 address
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.github.serpro69.kfaker.helper.isPrivateNet
import io.github.serpro69.kfaker.helper.isReservedNet
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldMatch

class InternetTest: DescribeSpec({
describe("Internet provider") {
Expand All @@ -26,5 +27,19 @@ class InternetTest: DescribeSpec({
}
}
}

@OptIn(ExperimentalStdlibApi::class)
context("MacAddress generation") {
it("should generate a valid mac-address") {
internet.macAddress() shouldMatch Regex("^([0-9a-f]{2}:){5}[0-9a-f]{2}$")
internet.macAddress().split(":").all { it.hexToInt() in 0..255 }
}
it("should generate a valid mac-address with prefix") {
internet.macAddress("a").split(":").all { it.hexToInt() in 0..255 }
internet.macAddress("aa").split(":").all { it.hexToInt() in 0..255 }
internet.macAddress("aa:ce").split(":").all { it.hexToInt() in 0..255 }
internet.macAddress("aa:ce:").split(":").all { it.hexToInt() in 0..255 }
}
}
}
})
6 changes: 6 additions & 0 deletions docs/src/orchid/resources/pages/data-provider/internet.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Faker().internet.privateIPv4Address() // => 192.168.82.243
Faker().internet.publicIPv4Address() // => 25.162.239.133
Faker().internet.iPv4Address() // => 230.168.182.77
// Mac Addresses
Faker().internet.macAddress() // => 17:12:d9:fc:fe:f6
Faker().internet.macAddress("a") // => 0a:11:ed:7c:b5:af
Faker().internet.macAddress("aa") // => aa:ec:eb:54:b9:f5
Faker().internet.macAddress("aa:ce") // => aa:ce:e3:e1:83:c4
Faker().internet.email() // les.weissnat@gmail.com
Faker().internet.email("john.doe") // => john.doe@gmail.com
Expand Down

0 comments on commit 2205b7f

Please sign in to comment.