Skip to content

Commit

Permalink
#15 SafeUser to validate names
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Sep 25, 2016
1 parent a476d2d commit bb67c13
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 18 deletions.
17 changes: 0 additions & 17 deletions src/main/java/io/jare/dynamo/DyUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand All @@ -47,13 +46,6 @@
*/
public final class DyUser implements User {

/**
* Pattern to match.
*/
private static final Pattern PTN = Pattern.compile(
"[a-zA-Z0-9\\.\\-]+"
);

/**
* The region to work with.
*/
Expand Down Expand Up @@ -93,15 +85,6 @@ public Iterable<Domain> mine() {

@Override
public void add(final String name) throws IOException {
if (!DyUser.PTN.matcher(name).matches()) {
throw new IllegalArgumentException(
String.format(
// @checkstyle LineLength (1 line)
"invalid domain name \"%s\", must contain letters, dots and dashes",
name
)
);
}
synchronized (this.region) {
final Iterator<Domain> before = new DyBase(this.region)
.domain(name);
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/io/jare/smarts/SafeUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 jare.io
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions: the above copyright notice and this
* permission notice shall be included in all copies or substantial
* portions of the Software. The software is provided "as is", without
* warranty of any kind, express or implied, including but not limited to
* the warranties of merchantability, fitness for a particular purpose
* and non-infringement. In no event shall the authors or copyright
* holders be liable for any claim, damages or other liability, whether
* in an action of contract, tort or otherwise, arising from, out of or
* in connection with the software or the use or other dealings in the
* software.
*/
package io.jare.smarts;

import io.jare.model.Domain;
import io.jare.model.User;
import java.io.IOException;
import java.util.regex.Pattern;

/**
* Safe user.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 1.0
*/
public final class SafeUser implements User {

/**
* Pattern to match.
*/
private static final Pattern PTN = Pattern.compile(
"(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\\.)+[a-zA-Z]{2,63}$)"
);

/**
* Original one.
*/
private final transient User origin;

/**
* Ctor.
* @param user Original user
*/
public SafeUser(final User user) {
this.origin = user;
}

@Override
public Iterable<Domain> mine() {
return this.origin.mine();
}

@Override
public void add(final String name) throws IOException {
if (!SafeUser.PTN.matcher(name).matches()) {
throw new SafeUser.InvalidNameException(name);
}
this.origin.add(name);
}

/**
* When name is not valid.
*/
public static final class InvalidNameException extends IOException {
/**
* Serialization marker.
*/
private static final long serialVersionUID = -869776873934626730L;
/**
* Ctor.
* @param name Domain name
*/
public InvalidNameException(final String name) {
super(
String.format(
"domain name \"%s\" is not valid",
name
)
);
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/io/jare/smarts/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 jare.io
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions: the above copyright notice and this
* permission notice shall be included in all copies or substantial
* portions of the Software. The software is provided "as is", without
* warranty of any kind, express or implied, including but not limited to
* the warranties of merchantability, fitness for a particular purpose
* and non-infringement. In no event shall the authors or copyright
* holders be liable for any claim, damages or other liability, whether
* in an action of contract, tort or otherwise, arising from, out of or
* in connection with the software or the use or other dealings in the
* software.
*/

/**
* Smarts.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 1.0
*/
package io.jare.smarts;
3 changes: 2 additions & 1 deletion src/main/java/io/jare/tk/TkAdd.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package io.jare.tk;

import io.jare.model.Base;
import io.jare.smarts.SafeUser;
import java.io.IOException;
import org.takes.Request;
import org.takes.Response;
Expand Down Expand Up @@ -57,7 +58,7 @@ final class TkAdd implements Take {
public Response act(final Request req) throws IOException {
final String name = new RqForm.Base(req).param("name")
.iterator().next().trim();
this.base.user(new RqUser(req).name()).add(name);
new SafeUser(this.base.user(new RqUser(req).name())).add(name);
return new RsForward(
new RsFlash(
String.format(
Expand Down
87 changes: 87 additions & 0 deletions src/test/java/io/jare/smarts/SafeUserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 jare.io
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions: the above copyright notice and this
* permission notice shall be included in all copies or substantial
* portions of the Software. The software is provided "as is", without
* warranty of any kind, express or implied, including but not limited to
* the warranties of merchantability, fitness for a particular purpose
* and non-infringement. In no event shall the authors or copyright
* holders be liable for any claim, damages or other liability, whether
* in an action of contract, tort or otherwise, arising from, out of or
* in connection with the software or the use or other dealings in the
* software.
*/
package io.jare.smarts;

import io.jare.fake.FkUser;
import io.jare.model.User;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;

/**
* Test case for {@link User}.
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.5
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class SafeUserTest {

/**
* User.Safe can accept normal domain names.
* @throws Exception If some problem inside
*/
@Test
public void acceptsValidDomains() throws Exception {
final User user = new SafeUser(new FkUser());
final String[] domains = {
"google.com",
"www.google.com",
"www-1.google.com",
"google.ua",
"www-8-9.google.ua",
};
for (final String domain : domains) {
user.add(domain);
}
}

/**
* User.Safe can reject invalid domain names.
* @throws Exception If some problem inside
*/
@Test
public void rejectsInvalidDomains() throws Exception {
final User user = new SafeUser(new FkUser());
final String[] domains = {
"google-com",
"google",
"www-1 .google.com",
"google.УА",
"www-8=9.google.ua",
};
for (final String domain : domains) {
try {
user.add(domain);
Assert.fail(String.format("exception expected: %s", domain));
} catch (final SafeUser.InvalidNameException ex) {
MatcherAssert.assertThat(
ex.getLocalizedMessage(),
Matchers.containsString(domain)
);
}
}
}

}
31 changes: 31 additions & 0 deletions src/test/java/io/jare/smarts/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 jare.io
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions: the above copyright notice and this
* permission notice shall be included in all copies or substantial
* portions of the Software. The software is provided "as is", without
* warranty of any kind, express or implied, including but not limited to
* the warranties of merchantability, fitness for a particular purpose
* and non-infringement. In no event shall the authors or copyright
* holders be liable for any claim, damages or other liability, whether
* in an action of contract, tort or otherwise, arising from, out of or
* in connection with the software or the use or other dealings in the
* software.
*/

/**
* Smarts, tests.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 1.0
*/
package io.jare.smarts;

0 comments on commit bb67c13

Please sign in to comment.