1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.server.accounts;
18
19import android.content.Context;
20import android.database.sqlite.SQLiteDatabase;
21import android.database.sqlite.SQLiteOpenHelper;
22
23/**
24 * Helper class for emulating pre-N database
25 */
26class PreNTestDatabaseHelper extends SQLiteOpenHelper {
27
28    public static final String TOKEN_STRING = "token-string-123";
29    public static final String ACCOUNT_TYPE = "type1";
30    public static final String ACCOUNT_NAME = "account@" + ACCOUNT_TYPE;
31    public static final String ACCOUNT_PASSWORD = "Password";
32    public static final String TOKEN_TYPE = "SID";
33
34    public PreNTestDatabaseHelper(Context context, String name) {
35        super(context, name, null, 4);
36    }
37
38    @Override
39    public void onCreate(SQLiteDatabase db) {
40        db.execSQL("CREATE TABLE accounts ( "
41                + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
42                + "name TEXT NOT NULL, "
43                + "type TEXT NOT NULL, "
44                + "password TEXT, "
45                + "UNIQUE(name, type))");
46        db.execSQL("INSERT INTO accounts (name, type, password) VALUES "
47                + "('" + ACCOUNT_NAME + "', '" + ACCOUNT_TYPE + "', '" + ACCOUNT_PASSWORD + "')");
48
49        db.execSQL("CREATE TABLE authtokens (  "
50                + "_id INTEGER PRIMARY KEY AUTOINCREMENT,  "
51                + "accounts_id INTEGER NOT NULL, "
52                + "type TEXT NOT NULL,  "
53                + "authtoken TEXT, "
54                + "UNIQUE (accounts_id, type ))");
55        db.execSQL("INSERT INTO authtokens (accounts_id, type, authtoken) VALUES "
56                + "(1, '" + TOKEN_TYPE + "', '" + TOKEN_STRING + "')");
57
58        db.execSQL("CREATE TABLE grants (  "
59                + "accounts_id INTEGER NOT NULL, "
60                + "auth_token_type STRING NOT NULL,  "
61                + "uid INTEGER NOT NULL,  "
62                + "UNIQUE (accounts_id,auth_token_type,uid))");
63
64        db.execSQL("CREATE TABLE extras ( "
65                + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
66                + "accounts_id INTEGER, "
67                + "key TEXT NOT NULL, "
68                + "value TEXT, "
69                + "UNIQUE(accounts_id , key))");
70
71        db.execSQL("CREATE TABLE meta ( "
72                + "key TEXT PRIMARY KEY NOT NULL, "
73                + "value TEXT)");
74
75        db.execSQL(""
76                + " CREATE TRIGGER accountsDelete DELETE ON accounts "
77                + " BEGIN"
78                + "   DELETE FROM authtokens"
79                + "     WHERE accounts_id=OLD._id;"
80                + "   DELETE FROM extras"
81                + "     WHERE accounts_id=OLD._id;"
82                + "   DELETE FROM grants"
83                + "     WHERE accounts_id=OLD._id;"
84                + " END");
85    }
86
87    @Override
88    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
89        throw new UnsupportedOperationException("Upgrade of test database is not supported");
90    }
91
92    public static void createV4Database(Context context, String name) {
93        PreNTestDatabaseHelper helper = new PreNTestDatabaseHelper(context, name);
94        helper.getWritableDatabase();
95        helper.close();
96    }
97
98}
99