From 300fdbf227d46185cea3f817033235560951fc00 Mon Sep 17 00:00:00 2001 From: Brenton Poke Date: Sun, 27 Sep 2020 16:33:53 -0400 Subject: [PATCH 1/2] trying cleaner commit...again --- .../java/org/influxdb/dto/BatchPoints.java | 7 +- src/main/java/org/influxdb/dto/Point.java | 7 +- .../org/influxdb/dto/utils/CheckTags.java | 36 ++++++ .../java/org/influxdb/dto/CheckTagsTest.java | 119 ++++++++++++++++++ 4 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/influxdb/dto/utils/CheckTags.java create mode 100644 src/test/java/org/influxdb/dto/CheckTagsTest.java diff --git a/src/main/java/org/influxdb/dto/BatchPoints.java b/src/main/java/org/influxdb/dto/BatchPoints.java index e32774628..5602732f2 100644 --- a/src/main/java/org/influxdb/dto/BatchPoints.java +++ b/src/main/java/org/influxdb/dto/BatchPoints.java @@ -10,6 +10,7 @@ import java.util.concurrent.TimeUnit; import org.influxdb.InfluxDB.ConsistencyLevel; +import org.influxdb.dto.utils.CheckTags; /** * {Purpose of This Type}. @@ -90,7 +91,11 @@ public Builder retentionPolicy(final String policy) { * @return the Builder instance. */ public Builder tag(final String tagName, final String value) { - this.tags.put(tagName, value); + Objects.requireNonNull(tagName, "tagName"); + Objects.requireNonNull(value, "value"); + if (CheckTags.isLegalFullCheck(tagName, value)) { + this.tags.put(tagName, value); + } return this; } diff --git a/src/main/java/org/influxdb/dto/Point.java b/src/main/java/org/influxdb/dto/Point.java index 422884e8a..b8900c70d 100755 --- a/src/main/java/org/influxdb/dto/Point.java +++ b/src/main/java/org/influxdb/dto/Point.java @@ -18,6 +18,7 @@ import org.influxdb.annotation.Column; import org.influxdb.annotation.Measurement; import org.influxdb.annotation.TimeColumn; +import org.influxdb.dto.utils.CheckTags; import org.influxdb.impl.Preconditions; /** @@ -116,7 +117,7 @@ public static final class Builder { public Builder tag(final String tagName, final String value) { Objects.requireNonNull(tagName, "tagName"); Objects.requireNonNull(value, "value"); - if (!tagName.isEmpty() && !value.isEmpty()) { + if (CheckTags.isLegalFullCheck(tagName, value)) { tags.put(tagName, value); } return this; @@ -131,7 +132,9 @@ public Builder tag(final String tagName, final String value) { */ public Builder tag(final Map tagsToAdd) { for (Entry tag : tagsToAdd.entrySet()) { - tag(tag.getKey(), tag.getValue()); + if (CheckTags.isLegalFullCheck(tag.getKey(), tag.getValue())) { + tags.put(tag.getKey(), tag.getValue()); + } } return this; } diff --git a/src/main/java/org/influxdb/dto/utils/CheckTags.java b/src/main/java/org/influxdb/dto/utils/CheckTags.java new file mode 100644 index 000000000..f61fb5ed8 --- /dev/null +++ b/src/main/java/org/influxdb/dto/utils/CheckTags.java @@ -0,0 +1,36 @@ +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 Pattern NAMEPATTERN = Pattern.compile(NAMEREGEX); + + 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 isLegalFullCheck(final String name, final String value) { + return !name.isEmpty() && !value.isEmpty() && CheckTags.isTagNameLegal(name); + } +} \ No newline at end of file diff --git a/src/test/java/org/influxdb/dto/CheckTagsTest.java b/src/test/java/org/influxdb/dto/CheckTagsTest.java new file mode 100644 index 000000000..11f4fca35 --- /dev/null +++ b/src/test/java/org/influxdb/dto/CheckTagsTest.java @@ -0,0 +1,119 @@ +package org.influxdb.dto; + +import java.util.HashMap; +import java.util.concurrent.TimeUnit; +import org.assertj.core.api.Assertions; +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 map = new HashMap<>(); + map.put("$cost","$15"); + map.put("$mortgage","$34,000"); + map.put("%interest","65%"); + map.put("@email","startrek@cbs.com"); + 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 map = new HashMap<>(); + map.put("$cost","$15"); + map.put("$mortgage","$34,000"); + map.put("%interest","65%"); + map.put("@email","startrek@cbs.com"); + 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","startrek@cbs.com").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.assertTrue(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 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()); + + } +} \ No newline at end of file From 604b1cfbe114ed1e16c1cbf45e1f5e952326544d Mon Sep 17 00:00:00 2001 From: Brenton Poke Date: Sun, 27 Sep 2020 16:40:46 -0400 Subject: [PATCH 2/2] checkstyle...again --- src/main/java/org/influxdb/dto/utils/CheckTags.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/influxdb/dto/utils/CheckTags.java b/src/main/java/org/influxdb/dto/utils/CheckTags.java index f61fb5ed8..8f77c7f69 100644 --- a/src/main/java/org/influxdb/dto/utils/CheckTags.java +++ b/src/main/java/org/influxdb/dto/utils/CheckTags.java @@ -33,4 +33,4 @@ public static Boolean isTagNameLegal(final String name) { public static Boolean isLegalFullCheck(final String name, final String value) { return !name.isEmpty() && !value.isEmpty() && CheckTags.isTagNameLegal(name); } -} \ No newline at end of file +}