From edc95c8a8e1cb0ab890b81894c378ac3f2fd2ab9 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Thu, 10 Jan 2019 18:13:05 -0800 Subject: [PATCH] Add validation for retention lease construction (#37312) This commit adds some simple validation that the values input to the retention lease constructor our valid values. We will later rely on these values being within the validated range. --- .../index/seqno/RetentionLease.java | 6 +++ .../index/seqno/RetentionLeaseTests.java | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 server/src/test/java/org/elasticsearch/index/seqno/RetentionLeaseTests.java diff --git a/server/src/main/java/org/elasticsearch/index/seqno/RetentionLease.java b/server/src/main/java/org/elasticsearch/index/seqno/RetentionLease.java index 2881aac73c2..076b707a5df 100644 --- a/server/src/main/java/org/elasticsearch/index/seqno/RetentionLease.java +++ b/server/src/main/java/org/elasticsearch/index/seqno/RetentionLease.java @@ -81,6 +81,12 @@ public final class RetentionLease { * @param source the source of the retention lease */ public RetentionLease(final String id, final long retainingSequenceNumber, final long timestamp, final String source) { + if (retainingSequenceNumber < SequenceNumbers.UNASSIGNED_SEQ_NO) { + throw new IllegalArgumentException("retention lease retaining sequence number [" + retainingSequenceNumber + "] out of range"); + } + if (timestamp < 0) { + throw new IllegalArgumentException("retention lease timestamp [" + timestamp + "] out of range"); + } this.id = id; this.retainingSequenceNumber = retainingSequenceNumber; this.timestamp = timestamp; diff --git a/server/src/test/java/org/elasticsearch/index/seqno/RetentionLeaseTests.java b/server/src/test/java/org/elasticsearch/index/seqno/RetentionLeaseTests.java new file mode 100644 index 00000000000..a5e4af5d0e6 --- /dev/null +++ b/server/src/test/java/org/elasticsearch/index/seqno/RetentionLeaseTests.java @@ -0,0 +1,50 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.seqno; + +import org.elasticsearch.test.ESTestCase; + +import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasToString; + +public class RetentionLeaseTests extends ESTestCase { + + public void testRetainingSequenceNumberOutOfRange() { + final long retainingSequenceNumber = randomLongBetween(Long.MIN_VALUE, UNASSIGNED_SEQ_NO - 1); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new RetentionLease("id", retainingSequenceNumber, randomNonNegativeLong(), "source")); + assertThat( + e, + hasToString(containsString("retention lease retaining sequence number [" + retainingSequenceNumber + "] out of range"))); + } + + public void testTimestampOutOfRange() { + final long timestamp = randomLongBetween(Long.MIN_VALUE, -1); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new RetentionLease("id", randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, Long.MAX_VALUE), timestamp, "source")); + assertThat( + e, + hasToString(containsString("retention lease timestamp [" + timestamp + "] out of range"))); + } + +}