-
Notifications
You must be signed in to change notification settings - Fork 477
#684: add tag checking for invalid characters #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
1d4016b
#684: add tag checking for invalid characters
BrentonPoke ef277d0
#684: forgot that posix-styled stuff doesn't work here
BrentonPoke f5aac7a
cleaning out throwing of exception.
BrentonPoke 226eece
attempting to fix checkstyle errors
BrentonPoke 04e1a21
attempting to fix checkstyle errors pt. 2
BrentonPoke 507fbb2
Adding unit tests for coverage
BrentonPoke f259449
removing unused exception
BrentonPoke 4b9157d
revised per feedback on pullrequst
BrentonPoke a684bd9
missing a carriage return test
BrentonPoke 6d475c5
testBufferLimitGreaterThanActions is still failing for unkown reasons
BrentonPoke 3622a59
reverting prinout
BrentonPoke 37309e1
checkstyle doesn't like my comment
BrentonPoke fced434
let's remove that, too
BrentonPoke 89c3ece
cleaning up an unused exception class i made
BrentonPoke b38fd60
attempting better code coverage
BrentonPoke 0725aef
attempting to raise code coverage.
BrentonPoke 735ffb4
refactor
BrentonPoke 78725e3
fixing checkstyle
BrentonPoke 0e8c7aa
adding another case
BrentonPoke 7b6c2cf
missing a carriage return test
BrentonPoke f34b73e
Merge branch 'master' of github.com:BrentonPoke/influxdb-java
BrentonPoke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.influxdb.dto.utils; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Utility class intended for internal use. | ||
* Checks the format of tag names and values to see if they conform to a regular expression. | ||
* | ||
* @author Brenton Poke | ||
* | ||
*/ | ||
public final class CheckTags { | ||
static final String NAMEREGEX = "[^\r\n]+"; | ||
static final String VALUEREGEX = "[\\x00-\\x7F]+"; | ||
static final Pattern NAMEPATTERN = Pattern.compile(NAMEREGEX); | ||
static final Pattern VALUEPATTERN = Pattern.compile(VALUEREGEX, Pattern.MULTILINE); | ||
|
||
private CheckTags() { } | ||
/** | ||
* Check a single tag's name according to the corresponding regular expression. | ||
* | ||
* @param name | ||
* the tag name | ||
* @return Boolean indicating that the tag name is legal | ||
* | ||
*/ | ||
public static Boolean isTagNameLegal(final String name) { | ||
final Matcher matcher = NAMEPATTERN.matcher(name); | ||
return matcher.matches(); | ||
} | ||
/** | ||
* Check a single tag's name according to the corresponding regular expression. | ||
* | ||
* @param value | ||
* the tag value | ||
* @return Boolean indicating that the tag value is legal | ||
* | ||
*/ | ||
public static Boolean isTagValueLegal(final String value) { | ||
final Matcher matcher = VALUEPATTERN.matcher(value); | ||
return matcher.matches(); | ||
} | ||
public static Boolean isLegalFullCheck(final String name, final String value) { | ||
return !name.isEmpty() | ||
&& !value.isEmpty() | ||
&& CheckTags.isTagNameLegal(name) | ||
&& CheckTags.isTagValueLegal(value); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package org.influxdb.dto; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.TimeUnit; | ||
import org.assertj.core.api.Assertions; | ||
import org.influxdb.dto.Point.Builder; | ||
import org.influxdb.dto.utils.CheckTags; | ||
import org.junit.Assert; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
public class CheckTagsTest { | ||
@Test | ||
public void TagNameNewLineTest() { | ||
final String tagname = "ma\ndrid"; | ||
final String tagname1 = "madrid\n"; | ||
final String tagname2 = "\nmadrid"; | ||
|
||
Point point = Point.measurement("test").time(1, TimeUnit.NANOSECONDS).tag(tagname,"city").addField("a", 1.0).build(); | ||
Assert.assertFalse(point.getTags().containsKey("madrid")); | ||
Assert.assertFalse(point.getTags().containsKey("mad\nrid")); | ||
Assert.assertFalse(CheckTags.isTagNameLegal(tagname)); | ||
Assert.assertFalse(CheckTags.isTagNameLegal(tagname1)); | ||
Assert.assertFalse(CheckTags.isTagNameLegal(tagname2)); | ||
} | ||
@Test | ||
public void TagNameCarriageReturnTest() { | ||
final String tagname = "ma\rdrid"; | ||
final String tagname1 = "madrid\r"; | ||
final String tagname2 = "\rmadrid"; | ||
Point point = Point.measurement("test").time(1, TimeUnit.NANOSECONDS).tag(tagname,"city").addField("a", 1.0).build(); | ||
Assert.assertFalse(point.getTags().containsKey("madrid")); | ||
Assert.assertFalse(point.getTags().containsKey("mad\rrid")); | ||
Assert.assertFalse(CheckTags.isTagNameLegal(tagname)); | ||
Assert.assertFalse(CheckTags.isTagNameLegal(tagname1)); | ||
Assert.assertFalse(CheckTags.isTagNameLegal(tagname2)); | ||
} | ||
@Test | ||
public void TagNameSymbolTest() { | ||
final String tagname = "$cost"; | ||
final String tagname1 = "!cost"; | ||
Point point = Point.measurement("test").time(1, TimeUnit.NANOSECONDS).tag(tagname,"$15").addField("a", 1.0).build(); | ||
Assert.assertFalse(point.getTags().containsKey("cost")); | ||
Assert.assertTrue(point.getTags().containsKey("$cost")); | ||
Assert.assertTrue(CheckTags.isTagNameLegal(tagname)); | ||
Assert.assertTrue(CheckTags.isTagNameLegal(tagname1)); | ||
final HashMap<String, String> map = new HashMap<>(); | ||
map.put("$cost","$15"); | ||
map.put("$mortgage","$34,000"); | ||
map.put("%interest","65%"); | ||
map.put("@email","[email protected]"); | ||
Point.Builder point1 = Point.measurement("test").time(1, TimeUnit.NANOSECONDS).tag(map).addField("a", 1.0); | ||
Assertions.assertThat(point1.build().getTags().values().equals(map.values())); | ||
} | ||
@Test | ||
public void BatchPointsTagTest() { | ||
final HashMap<String, String> map = new HashMap<>(); | ||
map.put("$cost","$15"); | ||
map.put("$mortgage","$34,000"); | ||
map.put("%interest","65%"); | ||
map.put("@email","[email protected]"); | ||
Point point = Point.measurement("test").time(1, TimeUnit.NANOSECONDS).addField("a", 1.0).build(); | ||
BatchPoints.Builder points = BatchPoints.builder() | ||
.tag("$cost","$15").tag("$mortgage","$34,000") | ||
.tag("%interest","65%").tag("@email","[email protected]").point(point); | ||
Assertions.assertThat(points.build().getPoints().get(0).getTags().equals(map.values())); | ||
map.put("#phone","1-555-0101"); | ||
Assertions.assertThat(!points.build().getPoints().get(0).getTags().equals(map.values())); | ||
} | ||
@Test | ||
public void LegalFullCheckTagTest() { | ||
Assert.assertTrue(CheckTags.isLegalFullCheck("test","value")); | ||
Assert.assertFalse(CheckTags.isLegalFullCheck("","")); | ||
Assert.assertFalse(CheckTags.isLegalFullCheck("test","")); | ||
Assert.assertFalse(CheckTags.isLegalFullCheck("","value")); | ||
Assert.assertFalse(CheckTags.isLegalFullCheck("ma\ndrid", "city")); | ||
Assert.assertFalse(CheckTags.isLegalFullCheck("city","ąćę")); | ||
Assert.assertFalse(CheckTags.isLegalFullCheck("","ąćę")); | ||
} | ||
@Test | ||
public void TagNameHyphenTest() { | ||
final String tagname = "-cost"; | ||
final String tagname1 = "bushel-cost"; | ||
Point point = Point.measurement("test").time(1, TimeUnit.NANOSECONDS).tag(tagname,"$15").addField("a", 1.0).build(); | ||
Assert.assertTrue(point.getTags().containsKey("-cost")); | ||
Assert.assertFalse(point.getTags().containsKey("cost")); | ||
Assert.assertTrue(CheckTags.isTagNameLegal(tagname)); | ||
Assert.assertTrue(CheckTags.isTagNameLegal(tagname1)); | ||
} | ||
@Test | ||
public void TagNameUnderscoreTest() { | ||
final String tagname = "_cost_"; | ||
final String tagname1 = "bushel_cost"; | ||
Point point = Point.measurement("test").time(1, TimeUnit.NANOSECONDS).tag(tagname,"$15").addField("a", 1.0).build(); | ||
Assert.assertTrue(point.getTags().containsKey("_cost_")); | ||
Assert.assertTrue(CheckTags.isTagNameLegal(tagname)); | ||
Assert.assertTrue(CheckTags.isTagNameLegal(tagname1)); | ||
} | ||
@Test | ||
public void TagValueASCIITest() { | ||
final String tagvalue = "$15"; | ||
final String tagvalue1 = "$34,000"; | ||
final String tagvalue2 = "65%"; | ||
final String tagvalue3 = "[email protected]"; | ||
final String tagvalue4 = "%SYSTEM_VARARG%"; | ||
Point.Builder point = Point.measurement("test").time(1, TimeUnit.NANOSECONDS).tag("cost",tagvalue).addField("a", 1.0); | ||
Assertions.assertThat(point.build().lineProtocol()).isEqualTo("test,cost=$15 a=1.0 1"); | ||
Assert.assertTrue(CheckTags.isTagValueLegal(tagvalue)); | ||
Assert.assertTrue(CheckTags.isTagValueLegal(tagvalue1)); | ||
Assert.assertTrue(CheckTags.isTagValueLegal(tagvalue2)); | ||
Assert.assertTrue(CheckTags.isTagValueLegal(tagvalue3)); | ||
Assert.assertTrue(CheckTags.isTagValueLegal(tagvalue4)); | ||
Assert.assertFalse(CheckTags.isTagValueLegal("ąćę")); | ||
final HashMap<String, String> map = new HashMap<>(); | ||
map.put("cost",tagvalue); | ||
map.put("mortgage",tagvalue1); | ||
map.put("interest",tagvalue2); | ||
map.put("email",tagvalue3); | ||
Point.Builder point1 = Point.measurement("test").time(1, TimeUnit.NANOSECONDS).tag(map).addField("a", 1.0); | ||
Assertions.assertThat(point1.build().getTags().values().equals(map.values())); | ||
map.put("phone","1-555-0101"); | ||
Assertions.assertThat(!point1.build().getTags().values().equals(map.values())); | ||
|
||
} | ||
@Test | ||
public void TagsNullOrEmpty(){ | ||
final String tagname = null; | ||
final String tagvalue = null; | ||
final String tagvalue1 = ""; | ||
|
||
Assert.assertThrows(NullPointerException.class, ()-> { | ||
Point.Builder point = Point.measurement("test").time(1, TimeUnit.NANOSECONDS).tag("cost",tagvalue).addField("a", 1.0); | ||
}); | ||
|
||
Assert.assertThrows(NullPointerException.class, ()-> { | ||
Point.Builder point = Point.measurement("test").time(1, TimeUnit.NANOSECONDS).tag(tagname,"whistle while you work").addField("a", 1.0); | ||
}); | ||
|
||
Point.Builder point1 = Point.measurement("test").time(1, TimeUnit.NANOSECONDS).tag("cost",tagvalue1).addField("a", 1.0); | ||
Assert.assertTrue(point1.build().getTags().isEmpty()); | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.