Joel Galenson | 26f4d01 | 2020-07-17 14:57:21 -0700 | [diff] [blame^] | 1 | // Copyright 2020, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // TODO: Once this is stable, remove this and document everything public. |
| 16 | #![allow(missing_docs)] |
| 17 | |
| 18 | use anyhow::{Context, Result}; |
| 19 | use rusqlite::Connection; |
| 20 | |
| 21 | pub struct KeystoreDB { |
| 22 | #[allow(dead_code)] |
| 23 | conn: Connection, |
| 24 | } |
| 25 | |
| 26 | impl KeystoreDB { |
| 27 | pub fn new() -> Result<KeystoreDB> { |
| 28 | Ok(KeystoreDB { |
| 29 | conn: Connection::open_in_memory() |
| 30 | .context("Failed to initialize sqlite connection.")?, |
| 31 | }) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[cfg(test)] |
| 36 | mod tests { |
| 37 | |
| 38 | use super::*; |
| 39 | use rusqlite::params; |
| 40 | |
| 41 | // Ensure we can initialize the database. |
| 42 | #[test] |
| 43 | fn test_new() -> Result<()> { |
| 44 | KeystoreDB::new()?; |
| 45 | Ok(()) |
| 46 | } |
| 47 | |
| 48 | // Test that we have the correct tables. |
| 49 | #[test] |
| 50 | fn test_tables() -> Result<()> { |
| 51 | let db = KeystoreDB::new()?; |
| 52 | let tables = db |
| 53 | .conn |
| 54 | .prepare("SELECT name from sqlite_master WHERE type='table' ORDER BY name;")? |
| 55 | .query_map(params![], |row| row.get(0))? |
| 56 | .collect::<rusqlite::Result<Vec<String>>>()?; |
| 57 | assert_eq!(tables.len(), 0); |
| 58 | Ok(()) |
| 59 | } |
| 60 | } |