diff --git a/core/src/main/kotlin/io/github/serpro69/kfaker/provider/Internet.kt b/core/src/main/kotlin/io/github/serpro69/kfaker/provider/Internet.kt index 9d80fc1e1..38df3108d 100644 --- a/core/src/main/kotlin/io/github/serpro69/kfaker/provider/Internet.kt +++ b/core/src/main/kotlin/io/github/serpro69/kfaker/provider/Internet.kt @@ -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. @@ -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 */ diff --git a/core/src/test/kotlin/io/github/serpro69/kfaker/provider/InternetTest.kt b/core/src/test/kotlin/io/github/serpro69/kfaker/provider/InternetTest.kt index 2e5236b42..95dd629c9 100644 --- a/core/src/test/kotlin/io/github/serpro69/kfaker/provider/InternetTest.kt +++ b/core/src/test/kotlin/io/github/serpro69/kfaker/provider/InternetTest.kt @@ -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") { @@ -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 } + } + } } }) diff --git a/docs/src/orchid/resources/pages/data-provider/internet.adoc b/docs/src/orchid/resources/pages/data-provider/internet.adoc index 7fc12e986..8fcaee9d4 100644 --- a/docs/src/orchid/resources/pages/data-provider/internet.adoc +++ b/docs/src/orchid/resources/pages/data-provider/internet.adoc @@ -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