Skip to content

Commit

Permalink
Add IPv6 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 2146ba2 commit 9f9987c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
* https://github.com/serpro69/kotlin-faker/pull/214[#214] [core] Update dictionary files, including:
** Data and functions in existing data providers
** `Faker().internet.domain()` now takes optional argument and produces a "safe-domain" (ending with `.example` or `.test`)
** New functions:
*** `Faker().internet.iPv4Address()`
*** `Faker().internet.publicIPv4Address()`
*** `Faker().internet.privateIPv4Address()`
** New `Internet` functions:
*** `iPv4Address()`
*** `publicIPv4Address()`
*** `privateIPv4Address()`
*** `iPv6Address()`
*** `macAddress()`
** Updates to existing localized dictionaries + one new locale
* https://github.com/serpro69/kotlin-faker/issues/208[#208] [core] Allow `StringProvider#regexify` to take Regex as input
* https://github.com/serpro69/kotlin-faker/pull/202[#202] [core] Allow `randomClassInstance` to directly use predefined generators
Expand Down
30 changes: 20 additions & 10 deletions core/src/main/kotlin/io/github/serpro69/kfaker/provider/Internet.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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
Expand Down Expand Up @@ -42,19 +41,37 @@ class Internet internal constructor(fakerService: FakerService) : YamlFakeDataPr
}

/**
* Returns an IPv4 address
* Returns a random IPv4 address
*/
fun iPv4Address() = List(4) { fakerService.randomService.nextInt(0, 255) }
.joinToString(".")

/**
* Returns a private IPv4 address
* Returns a random private IPv4 address
*/
fun privateIPv4Address(): String {
val ranges = fakerService.randomService.randomValue(privateIpv4Ranges)
return ranges.map { fakerService.randomService.nextInt(it) }.joinToString(".")
}

/**
* Returns a random public IPv4 address
*/
fun publicIPv4Address(): String = with(iPv4Address()) {
if (isReservedNet(this)) publicIPv4Address() else this
}

/**
* Returns a random IPv6 address
*
* Example:
* ```
* Faker().internet.iPv6Address() // => 176f:cfec:c73b:e0cb:534d:4b3e:db4e:3b53
* ```
*/
fun iPv6Address(): String = List(8) { fakerService.randomService.nextInt(65_536).toString(16) }
.joinToString(":")

/**
* Returns a random mac-address with an optional [prefix]
*
Expand All @@ -74,13 +91,6 @@ class Internet internal constructor(fakerService: FakerService) : YamlFakeDataPr
return listOf(pref, addr).flatten().joinToString(":") { format("%02x", it) }
}

/**
* Returns a public IPv4 address
*/
fun publicIPv4Address(): String = with(iPv4Address()) {
if (isReservedNet(this)) publicIPv4Address() else this
}

@JvmOverloads
fun email(name: String = ""): String {
val localName = if (name.trim() == "") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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
import sun.net.util.IPAddressUtil

class InternetTest: DescribeSpec({
describe("Internet provider") {
Expand All @@ -17,28 +18,41 @@ class InternetTest: DescribeSpec({
it("should generate private IPv4 address run#$it") {
isPrivateNet(internet.privateIPv4Address()) shouldBe true
isReservedNet(internet.privateIPv4Address()) shouldBe true
IPAddressUtil.isIPv4LiteralAddress(internet.privateIPv4Address()) shouldBe true
}
it("should generate a public IPv4 address run#$it") {
isPrivateNet(internet.publicIPv4Address()) shouldBe false
isReservedNet(internet.publicIPv4Address()) shouldBe false
IPAddressUtil.isIPv4LiteralAddress(internet.publicIPv4Address()) shouldBe true
}
it("should generate a random IPv4 address run#$it") {
IPAddressUtil.isIPv4LiteralAddress(internet.iPv4Address()) shouldBe true
internet.iPv4Address().split(".").all { s -> s.toInt() in 0..255 } shouldBe true
}
}
}

context("IPv6 address generation") {
repeat(100) {
it("should generate a valid IPv6 address run#$it") {
IPAddressUtil.isIPv6LiteralAddress(internet.iPv6Address()) shouldBe true
}
}
}

@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 }
repeat(100) {
it("should generate a valid mac-address run#$it") {
internet.macAddress() shouldMatch Regex("^([0-9a-f]{2}:){5}[0-9a-f]{2}$")
internet.macAddress().split(":").all { s -> s.hexToInt() in 0..255 }
}
it("should generate a valid mac-address with prefix run#$it") {
internet.macAddress("a").split(":").all { s -> s.hexToInt() in 0..255 }
internet.macAddress("aa").split(":").all { s -> s.hexToInt() in 0..255 }
internet.macAddress("aa:ce").split(":").all { s -> s.hexToInt() in 0..255 }
internet.macAddress("aa:ce:").split(":").all { s -> s.hexToInt() in 0..255 }
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion docs/src/orchid/resources/pages/data-provider/internet.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ Faker().internet.privateIPv4Address() // => 192.168.82.243
Faker().internet.publicIPv4Address() // => 25.162.239.133
Faker().internet.iPv4Address() // => 230.168.182.77
// Mac Addresses
// IPv6 addresses
Faker().internet.iPv6Address() // => 176f:cfec:c73b:e0cb:534d:4b3e:db4e:3b53
// 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
Expand Down

0 comments on commit 9f9987c

Please sign in to comment.