Skip to content

Commit

Permalink
Checks that legacy_page_size is power of 2 and in range
Browse files Browse the repository at this point in the history
closes #91
  • Loading branch information
Willena committed Sep 30, 2023
1 parent 4e5cd3b commit 000e050
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/sqlite/mc/SQLiteMCConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ public Builder setFastKdfIter(int value) {
}

public Builder setLegacyPageSize(int value) {
if (value < 0 || value > 65536 || (value & (value - 1)) != 0){
throw new IllegalArgumentException("legacy_page_size must be a power of 2 between 0 and 65536");
}
setPragma(Pragma.LEGACY_PAGE_SIZE, String.valueOf(value));
return this;
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/sqlite/mc/SQLiteMCConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.sqlite.mc;

import org.junit.jupiter.api.Test;

import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.sqlite.SQLiteConfig.Pragma.LEGACY_PAGE_SIZE;

class SQLiteMCConfigTest {

@Test
public void builderSetLegacyPageSizeTest() {
Properties p = new SQLiteMCConfig.Builder().setLegacyPageSize(0).build().toProperties();
assertThat(p.get(LEGACY_PAGE_SIZE.getPragmaName())).isEqualTo("0");

p = new SQLiteMCConfig.Builder().setLegacyPageSize(65536).build().toProperties();
assertThat(p.get(LEGACY_PAGE_SIZE.getPragmaName())).isEqualTo("65536");

p = new SQLiteMCConfig.Builder().setLegacyPageSize(1024).build().toProperties();
assertThat(p.get(LEGACY_PAGE_SIZE.getPragmaName())).isEqualTo("1024");

assertThatIllegalArgumentException().isThrownBy(() -> new SQLiteMCConfig.Builder().setLegacyPageSize(-1));
assertThatIllegalArgumentException().isThrownBy(() -> new SQLiteMCConfig.Builder().setLegacyPageSize(65637));
assertThatIllegalArgumentException().isThrownBy(() -> new SQLiteMCConfig.Builder().setLegacyPageSize(1233));
}
}

0 comments on commit 000e050

Please sign in to comment.