Add (mostly) empty KeystoreDB struct.
The KeystoreDB struct contains the interface with sqlite.
This commit introduces the KeystoreDB object and a sqlite connection
but does not add any operations.
Bug: 159370859
Test: atest keystore2_test
Change-Id: Ie5ec091a01d25ecd520ac29be67117cc3c3fd83c
diff --git a/keystore2/Android.bp b/keystore2/Android.bp
index bf78574..dd40589 100644
--- a/keystore2/Android.bp
+++ b/keystore2/Android.bp
@@ -21,13 +21,14 @@
"libanyhow",
"libkeystore_aidl_generated",
"liblog_rust",
+ "librusqlite",
"libthiserror",
],
}
rust_test {
name: "keystore2_test",
- crate_name: "keystore2_test",
+ crate_name: "keystore2",
srcs: ["src/lib.rs"],
test_suites: ["general-tests"],
auto_gen_config: true,
@@ -36,6 +37,7 @@
"libanyhow",
"libkeystore_aidl_generated",
"liblog_rust",
+ "librusqlite",
"libthiserror",
],
}
diff --git a/keystore2/src/database.rs b/keystore2/src/database.rs
new file mode 100644
index 0000000..c108446
--- /dev/null
+++ b/keystore2/src/database.rs
@@ -0,0 +1,60 @@
+// Copyright 2020, The Android Open Source Project
+//
+// Licensed 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.
+
+// TODO: Once this is stable, remove this and document everything public.
+#![allow(missing_docs)]
+
+use anyhow::{Context, Result};
+use rusqlite::Connection;
+
+pub struct KeystoreDB {
+ #[allow(dead_code)]
+ conn: Connection,
+}
+
+impl KeystoreDB {
+ pub fn new() -> Result<KeystoreDB> {
+ Ok(KeystoreDB {
+ conn: Connection::open_in_memory()
+ .context("Failed to initialize sqlite connection.")?,
+ })
+ }
+}
+
+#[cfg(test)]
+mod tests {
+
+ use super::*;
+ use rusqlite::params;
+
+ // Ensure we can initialize the database.
+ #[test]
+ fn test_new() -> Result<()> {
+ KeystoreDB::new()?;
+ Ok(())
+ }
+
+ // Test that we have the correct tables.
+ #[test]
+ fn test_tables() -> Result<()> {
+ let db = KeystoreDB::new()?;
+ let tables = db
+ .conn
+ .prepare("SELECT name from sqlite_master WHERE type='table' ORDER BY name;")?
+ .query_map(params![], |row| row.get(0))?
+ .collect::<rusqlite::Result<Vec<String>>>()?;
+ assert_eq!(tables.len(), 0);
+ Ok(())
+ }
+}
diff --git a/keystore2/src/lib.rs b/keystore2/src/lib.rs
index d2c55a7..dfdd228 100644
--- a/keystore2/src/lib.rs
+++ b/keystore2/src/lib.rs
@@ -14,4 +14,5 @@
//! This crate implements the Android Keystore 2.0 service.
+pub mod database;
pub mod error;