Skip to content

Commit

Permalink
Add ipv4 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 3ecaffb commit 4345c3d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
* 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()`
** 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.serpro69.kfaker.provider

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
Expand Down Expand Up @@ -38,6 +39,27 @@ class Internet internal constructor(fakerService: FakerService) : YamlFakeDataPr
}
}

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

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

/**
* 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 All @@ -60,3 +82,23 @@ class Internet internal constructor(fakerService: FakerService) : YamlFakeDataPr
fun userAgent(browserType: String): String = resolve("user_agent", browserType.lowercase())
fun botUserAgent(type: String): String = resolve("bot_user_agent", type.lowercase())
}

// Private, Host, and Link-Local network address blocks as defined in https://en.wikipedia.org/wiki/IPv4#Special-use_addresses
private val privateIpv4Ranges = listOf(
// 10.0.0.0/8 - Used for local communications within a private network
listOf(10..10, 0..255, 0..255, 1..255),
// 100.64.0.0/10 - Shared address space for communications between an ISP and its subscribers
listOf(100..100, 64..127, 0..255, 1..255),
// 127.0.0.0/8 - Used for loopback addresses to the local host
listOf(127..127, 0..255, 0..255, 1..255),
// 169.254.0.0/16 - Used for link-local addresses between two hosts on a single link when
listOf(169..169, 254..254, 0..255, 1..255),
// 172.16.0.0/12 - Used for local communications within a private network
listOf(172..172, 16..31, 0..255, 1..255),
// 192.0.0.0/24 - IETF Protocol Assignments
listOf(192..192, 0..0, 0..0, 1..255),
// 192.168.0.0/16 - Used for local communications within a private network
listOf(192..192, 168..168, 0..255, 1..255),
// 198.18.0.0/15 - Used for benchmark testing of inter-network communications between subnets
listOf(198..198, 18..19, 0..255, 1..255)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.serpro69.kfaker.provider

import io.github.serpro69.kfaker.Faker
import io.github.serpro69.kfaker.FakerService
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

class InternetTest: DescribeSpec({
describe("Internet provider") {
val internet = Internet(FakerService(faker = Faker()))

context("IPv4 address generation") {
repeat(100) {
it("should generate private IPv4 address run#$it") {
isPrivateNet(internet.privateIPv4Address()) shouldBe true
isReservedNet(internet.privateIPv4Address()) shouldBe true
}
it("should generate a public IPv4 address run#$it") {
isPrivateNet(internet.publicIPv4Address()) shouldBe false
isReservedNet(internet.publicIPv4Address()) shouldBe false
}
it("should generate a random IPv4 address run#$it") {
internet.iPv4Address().split(".").all { s -> s.toInt() in 0..255 } shouldBe true
}
}
}
}
})
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 @@ -17,12 +17,18 @@
====
[source,kotlin]
----
// "Safe" internet domain
Faker().internet.domain() // => cartwright-lesch.test
Faker().internet.domain(subdomain = true) // => mraz.mills.test
Faker().internet.domain(domain = "foo") // => foo.test
Faker().internet.domain(subdomain = true, domain = "bar") // => doyle.bar.test
Faker().internet.domain(domain = "kotlin-faker.test") // => kotlin-faker.test
// IPv4 addresses, public and private
Faker().internet.privateIPv4Address() // => 192.168.82.243
Faker().internet.publicIPv4Address() // => 25.162.239.133
Faker().internet.iPv4Address() // => 230.168.182.77
Faker().internet.email() // les.weissnat@gmail.com
Faker().internet.email("john.doe") // => john.doe@gmail.com
Expand Down

0 comments on commit 4345c3d

Please sign in to comment.