OLD | NEW |
| (Empty) |
1 function cleanup(db) | |
2 { | |
3 db.transaction(function(tx) { | |
4 tx.executeSql("DROP TABLE IF EXISTS Test"); | |
5 tx.executeSql("DROP INDEX IF EXISTS TestIndex"); | |
6 tx.executeSql("DROP VIEW IF EXISTS TestView"); | |
7 tx.executeSql("DROP TRIGGER IF EXISTS TestTrigger"); | |
8 }); | |
9 } | |
10 | |
11 function executeStatement(tx, statement, operation) | |
12 { | |
13 try { | |
14 tx.executeSql(statement); | |
15 postMessage(operation + " allowed."); | |
16 } catch (err) { | |
17 postMessage(operation + " not allowed: " + err + " (" + err.code + ")"); | |
18 } | |
19 } | |
20 | |
21 function createTableCallback(tx) | |
22 { | |
23 executeStatement(tx, "CREATE TABLE Test (Foo int)", "SQLITE_CREATE_TABLE"); | |
24 } | |
25 | |
26 function createStatementsCallback(tx) | |
27 { | |
28 executeStatement(tx, "CREATE INDEX TestIndex ON Test (Foo)", "SQLITE_CREATE_
INDEX"); | |
29 | |
30 // Even though the following query should trigger a SQLITE_CREATE_TEMP_INDEX
operation | |
31 // (according to http://www.sqlite.org/tempfiles.html), it doesn't, and I'm
not aware | |
32 // of any other way to trigger this operation. So we'll skip it for now. | |
33 //executeStatement(tx, "SELECT * FROM Test WHERE Foo IN (1, 2, 3)", "SQLITE_
CREATE_TEMP_INDEX"); | |
34 | |
35 executeStatement(tx, "CREATE TEMP TABLE TestTempTable (Foo int)", "SQLITE_CR
EATE_TEMP_TABLE"); | |
36 executeStatement(tx, "CREATE TEMP TRIGGER TestTempTrigger INSERT ON Test BEG
IN SELECT COUNT(*) FROM Test; END", "SQLITE_CREATE_TEMP_TRIGGER"); | |
37 executeStatement(tx, "CREATE TEMP VIEW TestTempView AS SELECT COUNT(*) FROM
Test", "SQLITE_CREATE_TEMP_VIEW"); | |
38 executeStatement(tx, "CREATE TRIGGER TestTrigger INSERT ON Test BEGIN SELECT
COUNT(*) FROM Test; END", "SQLITE_CREATE_TRIGGER"); | |
39 executeStatement(tx, "CREATE VIEW TestView AS SELECT COUNT(*) FROM Test", "S
QLITE_CREATE_VIEW"); | |
40 | |
41 // We should try to create a virtual table using fts3, when WebKit's sqlite
library supports it. | |
42 executeStatement(tx, "CREATE VIRTUAL TABLE TestVirtualTable USING Unsupporte
dModule", "SQLITE_CREATE_VTABLE"); | |
43 } | |
44 | |
45 function otherStatementsCallback(tx) | |
46 { | |
47 executeStatement(tx, "SELECT COUNT(*) FROM Test", "SQLITE_READ"); | |
48 executeStatement(tx, "SELECT COUNT(*) FROM Test", "SQLITE_SELECT"); | |
49 executeStatement(tx, "DELETE FROM Test", "SQLITE_DELETE"); | |
50 executeStatement(tx, "INSERT INTO Test VALUES (1)", "SQLITE_INSERT"); | |
51 executeStatement(tx, "UPDATE Test SET Foo = 2 WHERE Foo = 1", "SQLITE_UPDATE
"); | |
52 executeStatement(tx, "PRAGMA cache_size", "SQLITE_PRAGMA"); | |
53 | |
54 executeStatement(tx, "ALTER TABLE Test RENAME TO TestTable", "SQLITE_ALTER_T
ABLE"); | |
55 // Rename the table back to its original name | |
56 executeStatement(tx, "ALTER TABLE TestTable RENAME To Test", "SQLITE_ALTER_T
ABLE"); | |
57 | |
58 executeStatement(tx, "BEGIN TRANSACTION", "SQLITE_TRANSACTION"); | |
59 executeStatement(tx, "ATTACH main AS TestMain", "SQLITE_ATTACH"); | |
60 executeStatement(tx, "DETACH TestMain", "SQLITE_DETACH"); | |
61 executeStatement(tx, "REINDEX", "SQLITE_REINDEX"); | |
62 executeStatement(tx, "ANALYZE", "SQLITE_ANALYZE"); | |
63 | |
64 // SQLITE_FUNCTION: allowed in write mode | |
65 // There is no SQL/Javascript API to add user-defined functions to SQLite, | |
66 // so we cannot test this operation | |
67 } | |
68 | |
69 function dropStatementsCallback(tx) | |
70 { | |
71 executeStatement(tx, "DROP INDEX TestIndex", "SQLITE_DROP_INDEX"); | |
72 | |
73 // SQLITE_DROP_TEMP_INDEX: allowed in write mode | |
74 // Not sure how to test this: temp indexes are automatically dropped when | |
75 // the database is closed, but HTML5 doesn't specify a closeDatabase() call. | |
76 | |
77 executeStatement(tx, "DROP TABLE TestTempTable", "SQLITE_DROP_TEMP_TABLE"); | |
78 executeStatement(tx, "DROP TRIGGER TestTempTrigger", "SQLITE_DROP_TEMP_TRIGG
ER"); | |
79 executeStatement(tx, "DROP VIEW TestTempView", "SQLITE_DROP_TEMP_VIEW"); | |
80 executeStatement(tx, "DROP TRIGGER TestTrigger", "SQLITE_DROP_TRIGGER"); | |
81 executeStatement(tx, "DROP VIEW TestView", "SQLITE_DROP_VIEW"); | |
82 | |
83 // SQLITE_DROP_VTABLE: allowed in write mode | |
84 // Not sure how to test this: we cannot create a virtual table because we do
not | |
85 // have SQL/Javascript APIs to register a module that implements a virtual t
able. | |
86 // Therefore, trying to drop a virtual table will always fail (no such table
) | |
87 // before even getting to the authorizer. | |
88 | |
89 executeStatement(tx, "DROP TABLE Test", "SQLITE_DROP_TABLE"); | |
90 } | |
91 | |
92 function testReadWriteMode(db) | |
93 { | |
94 db.transaction(function(tx) { | |
95 postMessage("Beginning write transaction:"); | |
96 createTableCallback(tx); | |
97 createStatementsCallback(tx); | |
98 otherStatementsCallback(tx); | |
99 dropStatementsCallback(tx); | |
100 postMessage("Write transaction succeeded.\n"); | |
101 }); | |
102 } | |
103 | |
104 function testReadOnlyMode(db) | |
105 { | |
106 postMessage("Beginning read transactions:"); | |
107 // Test the 'CREATE TABLE' operation; it should be disallowed | |
108 db.readTransaction(createTableCallback); | |
109 | |
110 // In order to test all other 'CREATE' operations, we must create the table
first | |
111 db.transaction(createTableCallback); | |
112 db.readTransaction(createStatementsCallback); | |
113 | |
114 // In order to test the 'DROP' and 'other' operations, we need to first crea
te the respective entities | |
115 db.transaction(createStatementsCallback); | |
116 db.readTransaction(otherStatementsCallback); | |
117 db.readTransaction(dropStatementsCallback); | |
118 postMessage("Read transactions succeeded."); | |
119 } | |
120 | |
121 var db = openDatabaseSync("TestAuthorizerTest", "1.0", "Test the database author
izer.", 1); | |
122 cleanup(db); | |
123 testReadWriteMode(db); | |
124 testReadOnlyMode(db); | |
125 | |
126 postMessage("done"); | |
OLD | NEW |