Merge pull request #567 from TomerFi/add-sip-fake-data

feature: added sip data generator.
This commit is contained in:
Ricky Yim
2020-08-19 18:57:33 +10:00
committed by GitHub
6 changed files with 464 additions and 1 deletions
+1
View File
@@ -123,6 +123,7 @@ Fakers
* Robin
* RockBand
* Shakespeare
* Sip
* SlackEmoji
* Space
* StarTrek
@@ -102,6 +102,7 @@ public class Faker {
private final Disease disease;
private final Basketball basketball;
private final Barcode barcode;
private final Sip sip;
public Faker() {
this(Locale.ENGLISH);
@@ -213,6 +214,7 @@ public class Faker {
this.disease = new Disease(this);
this.basketball = new Basketball(this);
this.barcode = new Barcode(this);
this.sip = new Sip(this);
}
/**
@@ -667,6 +669,8 @@ public class Faker {
public Barcode barcode() { return barcode; }
public Sip sip() { return sip; }
public String resolve(String key) {
return this.fakeValuesService.resolve(key, this, this);
}
+215
View File
@@ -0,0 +1,215 @@
package com.github.javafaker;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.util.ArrayList;
/**
* Faker class for generating Session Initiation Protocol (SIP) related data.
*
* @author TomerFi
*/
public final class Sip {
private final Faker faker;
private final ArrayList<Integer> portPool;
protected Sip(final Faker faker) {
this.faker = faker;
int port = 40000;
portPool = new ArrayList<Integer>();
while (port <= 50000) {
portPool.add(port);
port = port + 2;
}
}
/**
* The various SIP methods are listed in https://en.wikipedia.org/wiki/Session_Initiation_Protocol.
*
* @return a SIP method String, e.g. {@code INVITE}.
*/
public String method() {
return faker.resolve("sip.methods");
}
/**
* Content types are based on https://tools.ietf.org/html/rfc5621 and
* https://tools.ietf.org/html/rfc3261.
*
* @return a SIP content-type declaration String, e.g. {@code application/sdp}
*/
public String contentType() {
return faker.resolve("sip.content.types");
}
/**
* Get a 4 digit random port for SIP messaging.
*
* @return a SIP messaging port int, e.g. 5060.
*/
public int messagingPort() {
return faker.random().nextInt(1000, 9999);
}
/**
* Get a 5 digit positive even port for rtp udp communication.
*
* @return an RTP UDP 5 digit port int, e.g. 40002.
*/
public int rtpPort() {
return portPool.get(faker.random().nextInt(0, portPool.size()));
}
/**
* Proviosional code, the various response codes are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a 3 digit SIP provisioan response code between 100 and 199 int, e.g. {@code 180}.
*/
public int provisionalResponseCode() {
return Integer.parseInt(faker.resolve("sip.response.codes.provisional"));
}
/**
* Success code, the various response codes are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a 3 digit SIP success response code between 200 and 299 int, e.g. {@code 200}.
*/
public int successResponseCode() {
return Integer.parseInt(faker.resolve("sip.response.codes.success"));
}
/**
* Redirection code, the various response codes are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a 3 digit SIP redirection response code between 300 and 399 int, e.g. {@code 301}.
*/
public int redirectResponseCode() {
return Integer.parseInt(faker.resolve("sip.response.codes.redirection"));
}
/**
* Client error code, the various response codes are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a 3 digit SIP client error response code between 400 and 499 int, e.g. {@code 486}.
*/
public int clientErrorResponseCode() {
return Integer.parseInt(faker.resolve("sip.response.codes.clientError"));
}
/**
* Server error code, the various response codes are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a 3 digit SIP server error response code between 500 and 599 int, e.g. {@code 503}.
*/
public int serverErrorResponseCode() {
return Integer.parseInt(faker.resolve("sip.response.codes.serverError"));
}
/**
* Global error code, the various response codes are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a 3 digit SIP global error response code between 600 and 699 int, e.g. {@code 608}.
*/
public int globalErrorResponseCode() {
return Integer.parseInt(faker.resolve("sip.response.codes.globalError"));
}
/**
* Proviosional phrase, the various response phrases are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a SIP provisional response phrase String, e.g. {@code Ringing}.
*/
public String provisionalResponsePhrase() {
return faker.resolve("sip.response.phrases.provisional");
}
/**
* Success phrase, the various response phrases are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a SIP success response phrase String, e.g. {@code OK}.
*/
public String successResponsePhrase() {
return faker.resolve("sip.response.phrases.success");
}
/**
* Redirection phrase, the various response phrases are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a SIP redirection response phrase String, e.g. {@code Moved Permanently}.
*/
public String redirectResponsePhrase() {
return faker.resolve("sip.response.phrases.redirection");
}
/**
* Client error phrase, the various response phrases are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a SIP client error response phrase String, e.g. {@code Busy Here}.
*/
public String clientErrorResponsePhrase() {
return faker.resolve("sip.response.phrases.clientError");
}
/**
* Server error phrase, the various response phrases are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a SIP server erro response phrase String, e.g. {@code Service Unavailable}.
*/
public String serverErrorResponsePhrase() {
return faker.resolve("sip.response.phrases.serverError");
}
/**
* Server error phrase, the various response phrases are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a SIP global error response phrase String, e.g. {@code Rejected}.
*/
public String globalErrorResponsePhrase() {
return faker.resolve("sip.response.phrases.globalError");
}
/**
* Body example of SDP type can be found in https://tools.ietf.org/html/rfc5621.
*
* @return a fake SDP type SIP body String.
*/
public String bodyString() {
return "v=0\n" +
"o=" + faker.name().firstName() + " " + faker.internet().uuid() + " IN IP4 " + faker.internet().domainName() + "\n" +
"s=-\n" +
"c=IN IP4 " + faker.internet().ipV4Address() + "\n" +
"t=0 0\n" +
"m=audio " + rtpPort() + " RTP/AVP 0\n" +
"a=rtpmap:0 PCMU/8000";
}
/**
* Body example of SDP type can be found in https://tools.ietf.org/html/rfc5621.
*
* @return a fake SDP type SIP body byte array.
*/
public byte[] bodyBytes() {
return bodyString().getBytes(UTF_8);
}
/**
* Return a valid name address to use with {@code to/from} headers.
*
* @return a valid name address String, e.g. {@code <sip:fakeName@10.1.2.3:5060>}.
*/
public String nameAddress() {
return "<sip:" + faker.name().firstName() + "@" + faker.internet().ipV4Address() + ":" + messagingPort() + ">";
}
}
@@ -145,6 +145,7 @@ public class EnFile {
"silicon_valley.yml",
"simpsons.yml",
"singular_siegler.yml",
"sip.yml",
"slack_emoji.yml",
"sonic_the_hedgehog.yml",
"source.yml",
+101
View File
@@ -0,0 +1,101 @@
---
en:
faker:
sip:
methods: ["INVITE", "ACK", "BYE", "CANCEL", "REGISTER", "OPTIONS", "PRACK", "SUBSCRIBE", "NOTIFY", "PUBLISH", "INFO", "REFER", "MESSAGE", "UPDATE"]
content:
types: [
"application/sdp",
"application/resource-lists",
"application/pkcs7-mime",
"application/pkcs7-signature",
"application/x-private",
"application/xml",
"multipart/mixed",
"multipart/alternative",
"multipart/related",
"text/html"]
response:
codes:
provisional: ["100", "180", "181", "182", "183", "199"]
success: ["200", "202"]
redirection: ["300", "301", "302", "305", "380"]
clientError: [
"400",
"401",
"402",
"403",
"404",
"405",
"406",
"407",
"408",
"409",
"410",
"413",
"414",
"415",
"416",
"420",
"421",
"422",
"423",
"480",
"481",
"482",
"483",
"484",
"485",
"486",
"487",
"488",
"491",
"493"]
serverError: ["500", "501", "502", "503", "504", "505", "513", "555", "580"]
globalError: ["600", "603", "604", "606", "607"]
phrases:
provisional: ["Trying", "Ringing", "Call Is Being Forwarded", "Queued", "Session Progress", "Early Dialog Terminated"]
success: ["OK", "accepted"]
redirection: ["Multiple Choices", "Moved Permanently", "Moved Temporarily", "Use Proxy", "Alternative Service"]
clientError: [
"Bad Request",
"Unauthorized",
"Payment Required",
"Forbidden",
"Not Found",
"Method Not Allowed",
"Not Acceptable",
"Proxy Authentication Required",
"Request Timeout",
"Conflict",
"Gone",
"Request Entity Too Large",
"Request-URI Too Long",
"Unsupported Media Type",
"Unsupported URI Scheme",
"Bad Extension",
"Extension Required",
"Session Interval Too Small",
"Interval Too Brief",
"Temporarily Unavailable",
"Call/Transaction Does Not Exist",
"Loop Detected",
"Too Many Hops",
"Address Incomplete",
"Ambiguous",
"Busy Here",
"Request Terminated",
"Not Acceptable Here",
"Request Pending",
"Undecipherable"]
serverError: [
"Server Internal Error",
"Not Implemented",
"Bad Gateway",
"Service Unavailable",
"Server Time-out",
"Version Not Supported",
"Message Too Large",
"Push Notification Service Not Supported",
"Precondition Failure"]
globalError: ["Busy Everywhere", "Decline", "Does Not Exist Anywhere", "Not Acceptable", "Unwanted"]
@@ -0,0 +1,141 @@
package com.github.javafaker;
import org.junit.Test;
import static com.github.javafaker.matchers.MatchesRegularExpression.matchesRegularExpression;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
public class SipTest extends AbstractFakerTest {
@Test
public void method_returnUpperCaseWithMinimum3Chars() {
assertThat(faker.sip().method(), matchesRegularExpression("^[A-Z]{3,}$"));
}
@Test
public void contentType_returnLowerCaseTwoWordsSepereatedBySlashMinimum3And4Chars() {
assertThat(faker.sip().contentType(), matchesRegularExpression("^[a-z]{4,}[/]{1,}[a-z0-9-]{3,}$"));
}
@Test
public void messagingPort_return4DigitIntBetween1000And9999() {
assertThat(faker.sip().messagingPort(), both(greaterThanOrEqualTo(1000)).and(lessThan(10000)));
}
@Test
public void rtpPort_returnPositiveEvenInt() {
int sut = faker.sip().rtpPort();
assertThat(sut, greaterThanOrEqualTo(2));
assertThat(sut % 2, is(0));
}
@Test
public void provisionalResponseCode_return3DigitIntBetween100And199() {
assertThat(faker.sip().provisionalResponseCode(), both(greaterThanOrEqualTo(100)).and(lessThan(200)));
}
@Test
public void successResponse_Codereturn3DigitIntBetween200And299() {
assertThat(faker.sip().successResponseCode(), both(greaterThanOrEqualTo(200)).and(lessThan(300)));
}
@Test
public void redirectResponseCode_Codereturn3DigitIntBetween300And399() {
assertThat(faker.sip().redirectResponseCode(), both(greaterThanOrEqualTo(300)).and(lessThan(400)));
}
@Test
public void clientErrorResponseCode_Codereturn3DigitIntBetween400And499() {
assertThat(faker.sip().clientErrorResponseCode(), both(greaterThanOrEqualTo(400)).and(lessThan(500)));
}
@Test
public void serverErrorResponseCode_Codereturn3DigitIntBetween500And599() {
assertThat(faker.sip().serverErrorResponseCode(), both(greaterThanOrEqualTo(500)).and(lessThan(600)));
}
@Test
public void globalErrorResponseCode_Codereturn3DigitIntBetween600And699() {
assertThat(faker.sip().globalErrorResponseCode(), both(greaterThanOrEqualTo(600)).and(lessThan(700)));
}
@Test
public void provisionalResponsePhrase_returnAnyNonDigitString() {
assertThat(faker.sip().provisionalResponsePhrase(), matchesRegularExpression("\\D+"));
}
@Test
public void successResponsePhrase_returnAnyNonDigitString() {
assertThat(faker.sip().successResponsePhrase(), matchesRegularExpression("\\D+"));
}
@Test
public void redirectResponsePhrase_returnAnyNonDigitString() {
assertThat(faker.sip().redirectResponsePhrase(), matchesRegularExpression("\\D+"));
}
@Test
public void clientErrorResponsePhrase_returnAnyNonDigitString() {
assertThat(faker.sip().clientErrorResponsePhrase(), matchesRegularExpression("\\D+"));
}
@Test
public void serverErrorResponsePhrase_returnAnyNonDigitString() {
assertThat(faker.sip().serverErrorResponsePhrase(), matchesRegularExpression("\\D+"));
}
@Test
public void globalErrorResponsePhrase_returnAnyNonDigitString() {
assertThat(faker.sip().globalErrorResponsePhrase(), matchesRegularExpression("\\D+"));
}
@Test
public void bodyString_returnAValidSdpBodyString() {
String[] sut = faker.sip().bodyString().split("\n");
assertThat(sut.length, is(7));
assertThat(sut[0], is("v=0"));
String[] secondLine = sut[1].split(" ");
assertThat(secondLine[0], startsWith("o="));
assertThat(secondLine[1], matchesRegularExpression("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"));
assertThat(secondLine[secondLine.length - 1], matchesRegularExpression("[a-z]+\\.\\w{2,4}"));
assertThat(sut[2], is("s=-"));
String[] fourthLine = sut[3].split(" ");
assertThat(fourthLine[0], is("c=IN"));
assertThat(fourthLine[fourthLine.length - 1], matchesRegularExpression("^\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}$"));
assertThat(sut[4], is("t=0 0"));
String[] sixthLine = sut[5].split(" ");
assertThat(sixthLine[0], is("m=audio"));
assertThat(Integer.parseInt(sixthLine[1]), greaterThanOrEqualTo(2));
assertThat(Integer.parseInt(sixthLine[1]) % 2, is(0));
assertThat(sut[6], is("a=rtpmap:0 PCMU/8000"));
}
@Test
public void bodyBytes_isNotNull() {
byte[] sut = faker.sip().bodyBytes();
assertThat(sut.length, notNullValue());
}
@Test
public void nameAddress_returnValidNameAddressString() {
String[] sut = faker.sip().nameAddress().split("@");
assertThat(sut[0].split(":")[1], matchesRegularExpression("\\w+"));
assertThat(sut[1].split(":")[0], matchesRegularExpression("^\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}$"));
}
}