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/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(())
+ }
+}