Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/src/main/assets/secrets.json.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"hostname": "",
"username": "",
"password": ""
"password": "",
"apiKey": ""
}
5 changes: 3 additions & 2 deletions app/src/main/java/io/sqlitecloud/sampleapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ class MainActivity : ComponentActivity() {
appContext = applicationContext,
config = SQLiteCloudConfig(
hostname = secrets?.hostname ?: "",
username = secrets?.username ?: "",
password = secrets?.password ?: "",
username = secrets?.username,
password = secrets?.password,
apiKey = secrets?.apiKey
),
)

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/io/sqlitecloud/sampleapp/Secrets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ data class Secrets(
val hostname: String,
val username: String,
val password: String,
val apiKey: String,
)
22 changes: 22 additions & 0 deletions sqlitecloud/src/androidTest/java/io/sqlitecloud/ConnectionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.junit.runner.RunWith
class ConnectionTest {
companion object {
private val sql: SQLiteCloud = TestContext.sqliteCloud()
private val sqlApiKey: SQLiteCloud = TestContext.sqliteCloudApiKey()
}

@Test
Expand All @@ -25,6 +26,14 @@ class ConnectionTest {
assertFalse(sql.isConnected)
}

@Test
fun connectWithValidApiKeyCredentialsSucceeds() = runBlocking {
sqlApiKey.connect()
assertTrue(sqlApiKey.isConnected)
sqlApiKey.disconnect()
assertFalse(sqlApiKey.isConnected)
}

@Test
fun connectWithInvalidCredentialsThrowsSQLiteCloudError() {
val invalidSql = SQLiteCloud(
Expand All @@ -38,6 +47,19 @@ class ConnectionTest {
}
}

@Test
fun connectWithInvalidCredentialsApiKeyThrowsSQLiteCloudError() {
val invalidSql = SQLiteCloud(
appContext = TestContext.context,
config = sqlApiKey.config.copy(apiKey = "INVALID APIKEY"),
)
assertThrows(SQLiteCloudError::class.java) {
runBlocking {
invalidSql.connect()
}
}
}

@Test
fun disconnectWhenNotConnectedThrowsSQLiteCloudError() {
assertThrows(SQLiteCloudError::class.java) {
Expand Down
1 change: 1 addition & 0 deletions sqlitecloud/src/androidTest/java/io/sqlitecloud/Secrets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ data class Secrets(
val hostname: String,
val username: String,
val password: String,
val apiKey: String
)
16 changes: 16 additions & 0 deletions sqlitecloud/src/androidTest/java/io/sqlitecloud/TestContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ object TestContext {
hostname = hostname,
username = username,
password = password,
apiKey = null,
rootCertificate = rootCertificatePath,
)
)

fun sqliteCloudApiKey(
hostname: String = secrets?.hostname ?: "",
apiKey: String = secrets?.apiKey ?: "",
rootCertificatePath: String = certFile.path,
) = SQLiteCloud(
appContext = context,
config = SQLiteCloudConfig(
hostname = hostname,
username = null,
password = null,
apiKey = apiKey,
rootCertificate = rootCertificatePath,
)
)
Expand Down
2 changes: 2 additions & 0 deletions sqlitecloud/src/main/cpp/sqcloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct tls;
struct tls_config;
struct tls *tls_client(void);
struct tls_config *tls_config_new(void);
void tls_config_free(struct tls_config *config);
int tls_init(void);
int tls_configure(struct tls *_ctx, struct tls_config *_config);
int tls_connect_socket(struct tls *_ctx, int _s, const char *_servername);
Expand Down Expand Up @@ -531,6 +532,7 @@ static bool internal_setup_tls (SQCloudConnection *connection, SQCloudConfig *co

// apply configuration to context
rc = tls_configure(tls_context, tls_conf);
tls_config_free(tls_conf);
if (rc < 0) {
return internal_set_error(connection, INTERNAL_ERRCODE_TLS, "Error in tls_configure: %s.", tls_error(tls_context));
}
Expand Down
6 changes: 3 additions & 3 deletions sqlitecloud/src/main/cpp/sqlitecloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ Java_io_sqlitecloud_SQLiteCloudBridge_doConnect(
jstring username,
jstring password,
jstring database,
jstring apiKey,
jint timeout,
jint family,
jboolean compression,
jboolean sqlite_mode,
jboolean zero_text,
jboolean password_hashed,
jboolean nonlinearizable,
Expand All @@ -77,13 +77,13 @@ Java_io_sqlitecloud_SQLiteCloudBridge_doConnect(
.username = cString(env, username),
.password = cString(env, password),
.database = database ? cString(env, database) : nullptr,
.api_key = cString(env, apiKey),
.timeout = timeout,
.family = family,
.compression = static_cast<bool>(compression),
.sqlite_mode = static_cast<bool>(sqlite_mode),
.zero_text = static_cast<bool>(zero_text),
.password_hashed = static_cast<bool>(password_hashed),
.nonlinearizable = static_cast<bool>(nonlinearizable),
.non_linearizable = static_cast<bool>(nonlinearizable),
.db_memory = static_cast<bool>(db_memory),
.no_blob = static_cast<bool>(no_blob),
.db_create = static_cast<bool>(db_create),
Expand Down
2 changes: 1 addition & 1 deletion sqlitecloud/src/main/java/io/sqlitecloud/SQLiteCloud.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ class SQLiteCloud(
username = config.username,
password = config.password,
database = config.dbname,
apiKey = config.apiKey,
timeout = config.timeout,
family = config.family.value,
compression = config.compression,
sqliteMode = config.sqliteMode,
zeroText = config.zerotext,
passwordHashed = config.passwordHashed,
nonlinearizable = config.nonlinearizable,
Expand Down
14 changes: 7 additions & 7 deletions sqlitecloud/src/main/java/io/sqlitecloud/SQLiteCloudBridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ internal class SQLiteCloudBridge(val logger: SQLiteCloudLogger?) {
private external fun doConnect(
hostname: String,
port: Int,
username: String,
password: String,
username: String?,
password: String?,
database: String?,
apiKey: String?,
timeout: Int,
family: Int,
compression: Boolean,
sqliteMode: Boolean,
zeroText: Boolean,
passwordHashed: Boolean,
nonlinearizable: Boolean,
Expand All @@ -67,13 +67,13 @@ internal class SQLiteCloudBridge(val logger: SQLiteCloudLogger?) {
fun connect(
hostname: String,
port: Int,
username: String,
password: String,
username: String?,
password: String?,
database: String?,
apiKey: String?,
timeout: Int,
family: Int,
compression: Boolean,
sqliteMode: Boolean,
zeroText: Boolean,
passwordHashed: Boolean,
nonlinearizable: Boolean,
Expand All @@ -94,10 +94,10 @@ internal class SQLiteCloudBridge(val logger: SQLiteCloudLogger?) {
username = username,
password = password,
database = database,
apiKey = apiKey,
timeout = timeout,
family = family,
compression = compression,
sqliteMode = sqliteMode,
zeroText = zeroText,
passwordHashed = passwordHashed,
nonlinearizable = nonlinearizable,
Expand Down
19 changes: 14 additions & 5 deletions sqlitecloud/src/main/java/io/sqlitecloud/SQLiteCloudConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import kotlin.io.path.Path

data class SQLiteCloudConfig(
val hostname: String,
val username: String,
val password: String,
val username: String?,
val password: String?,
val apiKey: String?,
val port: Int = defaultPort,
val family: Family = Family.IPv4,
val passwordHashed: Boolean = false,
Expand All @@ -29,7 +30,13 @@ data class SQLiteCloudConfig(
val clientCertificateKey: String? = null,
) {
val connectionString: String
get() = "sqlitecloud://$username:****@$hostname:$port/${dbname ?: ""}"
get() {
if (apiKey != null) {
return "sqlitecloud://$hostname:$port/${dbname ?: ""}?apikey=$apiKey"
} else {
return "sqlitecloud://${username ?: ""}:****@$hostname:$port/${dbname ?: ""}"
}
}

companion object {
const val defaultPort = 8860
Expand Down Expand Up @@ -62,6 +69,7 @@ data class SQLiteCloudConfig(
key to value
} ?: emptyMap()

val apiKey = queryItems["apikey"]
val family = queryItems["family"]
val passwordHashed = queryItems["passwordHashed"]
val nonlinearizable = queryItems["nonlinearizable"]
Expand All @@ -82,8 +90,9 @@ data class SQLiteCloudConfig(

return SQLiteCloudConfig(
hostname = connectionUri.host ?: "",
username = userInfo?.get(0) ?: "",
password = userInfo?.get(1) ?: "",
username = userInfo?.get(0),
password = userInfo?.get(1),
apiKey = apiKey,
port = port,
dbname = dbname,
family = family?.toIntOrNull()
Expand Down
13 changes: 13 additions & 0 deletions sqlitecloud/src/test/java/io/sqlitecloud/SQLiteCloudTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ class SQLiteCloudTest {
assertEquals("dbname", config.dbname)
assertEquals("path", config.rootCertificate)
}

@Test
fun creationWithApiKeyFromStringIsCorrect() {
val connectionString = "sqlitecloud://hostname.com:1234/dbname?root_certificate=path&apikey=apikey"

val config = SQLiteCloudConfig.fromString(connectionString)

assertEquals("apiKey", config.apiKey)
assertEquals("hostname.com", config.hostname)
assertEquals(1234, config.port)
assertEquals("dbname", config.dbname)
assertEquals("path", config.rootCertificate)
}
}