Skip to content

Commit 2a26588

Browse files
authored
Prevent setting the format version to an unsupported version (#3309)
FDBRecordStore.Builder.setFormatVersion takes an integer, but only a limited set are supported, we should prevent setting it to a value that is unsupported. Resolves: #3308
1 parent 2db756d commit 2a26588

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/FDBRecordStore.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5184,6 +5184,10 @@ public int getFormatVersion() {
51845184
@Override
51855185
@Nonnull
51865186
public Builder setFormatVersion(int formatVersion) {
5187+
if (formatVersion < MIN_FORMAT_VERSION || formatVersion > MAX_SUPPORTED_FORMAT_VERSION) {
5188+
throw new UnsupportedFormatVersionException("Invalid Format Version")
5189+
.addLogInfo(LogMessageKeys.FORMAT_VERSION, formatVersion);
5190+
}
51875191
this.formatVersion = formatVersion;
51885192
return this;
51895193
}

fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/FDBRecordStoreFormatVersionTest.java

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@
2727
import com.google.common.base.Charsets;
2828
import org.junit.jupiter.api.Tag;
2929
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.MethodSource;
32+
33+
import java.util.stream.IntStream;
3034

3135
import static org.junit.jupiter.api.Assertions.assertEquals;
3236
import static org.junit.jupiter.api.Assertions.assertNull;
3337
import static org.junit.jupiter.api.Assertions.assertThrows;
3438

3539
/**
36-
* Tests related to upgrading/downgrading the format version.
40+
* Tests related to interacting with the format version.
3741
*/
3842
@Tag(Tags.RequiresFDB)
3943
public class FDBRecordStoreFormatVersionTest extends FDBRecordStoreTestBase {
@@ -109,4 +113,65 @@ public void testAccessUserFieldAtOldFormatVersion() {
109113
}
110114
}
111115

116+
public static IntStream testOpenWithProvidedBadVersion() {
117+
return IntStream.of(-1, 0, FDBRecordStore.MAX_SUPPORTED_FORMAT_VERSION + 1);
118+
}
119+
120+
@ParameterizedTest
121+
@MethodSource
122+
void testOpenWithProvidedBadVersion(int version) {
123+
try (FDBRecordContext context = openContext()) {
124+
assertThrows(UnsupportedFormatVersionException.class,
125+
() -> getStoreBuilder(context, simpleMetaData(NO_HOOK))
126+
.setFormatVersion(version));
127+
}
128+
}
129+
130+
public static IntStream testOpenWithExistingBadVersion() {
131+
return IntStream.of(-1, 0, FDBRecordStore.MAX_SUPPORTED_FORMAT_VERSION + 1);
132+
}
133+
134+
@ParameterizedTest
135+
@MethodSource
136+
void testOpenWithExistingBadVersion(int version) {
137+
try (FDBRecordContext context = openContext()) {
138+
recordStore = getStoreBuilder(context, simpleMetaData(NO_HOOK))
139+
.setFormatVersion(FDBRecordStore.MAX_SUPPORTED_FORMAT_VERSION)
140+
.create();
141+
recordStore.saveStoreHeader(recordStore.getRecordStoreState().getStoreHeader()
142+
.toBuilder()
143+
.setFormatVersion(version)
144+
.build());
145+
commit(context);
146+
}
147+
148+
try (FDBRecordContext context = openContext()) {
149+
final FDBRecordStore.Builder storeBuilder = getStoreBuilder(context, simpleMetaData(NO_HOOK));
150+
assertThrows(UnsupportedFormatVersionException.class, () -> storeBuilder
151+
.setFormatVersion(FDBRecordStore.MAX_SUPPORTED_FORMAT_VERSION)
152+
.uncheckedOpen());
153+
}
154+
try (FDBRecordContext context = openContext()) {
155+
final FDBRecordStore.Builder storeBuilder = getStoreBuilder(context, simpleMetaData(NO_HOOK));
156+
assertThrows(UnsupportedFormatVersionException.class, () -> storeBuilder
157+
.setFormatVersion(FDBRecordStore.MAX_SUPPORTED_FORMAT_VERSION)
158+
.open());
159+
}
160+
}
161+
162+
static IntStream testUnopenedVersion() {
163+
return IntStream.of(FDBRecordStore.DEFAULT_FORMAT_VERSION, FDBRecordStore.INFO_ADDED_FORMAT_VERSION,
164+
FDBRecordStore.MAX_SUPPORTED_FORMAT_VERSION);
165+
}
166+
167+
@ParameterizedTest
168+
@MethodSource
169+
void testUnopenedVersion(int version) {
170+
try (FDBRecordContext context = openContext()) {
171+
recordStore = getStoreBuilder(context, simpleMetaData(NO_HOOK))
172+
.setFormatVersion(version)
173+
.build();
174+
assertEquals(version, recordStore.getFormatVersion());
175+
}
176+
}
112177
}

0 commit comments

Comments
 (0)