Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read database with CORRECT password directly after attempt to open with INCORRECT password #43

Open
brodycj opened this issue Oct 10, 2016 · 1 comment

Comments

@brodycj
Copy link
Collaborator

brodycj commented Oct 10, 2016

On Android/iOS the workaround seems to be to close and then open again. Here is the test case:

      it(suiteName + ' Attempt to open encrypted DB with INCORRECT password THEN OPEN & READ with CORRECT PASSWORD [PLUGIN BROKEN: MUST CLOSE THEN TRY AGAIN]', function (done) {
        if (isWindows) pending('SKIP for Windows: CALLBACK NOT RECEIVED');
        var dbName = 'Encrypted-DB-attempt-incorrect-password-then-correct-password.db';
        var test_data = 'test-data';

        window.sqlitePlugin.openDatabase({name: dbName, key: 'test-password', location: 'default'}, function (db1) {
            expect(db1).toBeDefined();
            // CREATE TABLE to put some contents into the DB:
            db1.transaction(function(tx) {
              tx.executeSql('DROP TABLE IF EXISTS tt');
              tx.executeSql('CREATE TABLE IF NOT EXISTS tt (test_data)');
              tx.executeSql('INSERT INTO tt (test_data) VALUES (?)', [test_data]);
            }, function(error) {
              // NOT EXPECTED:
              expect(false).toBe(true);
              expect(error.message).toBe('--');
              done();
            }, function() {
              db1.close(function () {

                window.sqlitePlugin.openDatabase({name: dbName, key: 'another-password', location: 'default'}, function (db2) {
                  // NOT EXPECTED:
                  expect(false).toBe(true);
                  done();
                }, function (error) {
                  // EXPECTED RESULT:
                  expect(error).toBeDefined();
                  // FUTURE TBD CHECK code/message

                  window.sqlitePlugin.openDatabase({name: dbName, key: 'test-password', location: 'default'}, function (db3) {
                    // EXPECTED RESULT:
                    expect(db3).toBeDefined();
                    //* ** ALT 1:
                    db3.transaction(function(tx) {
                      tx.executeSql('SELECT * FROM tt', null, function(ignored, rs) {
                        expect('PLUGIN FIXED PLEASE UPDATE THIS TEST').toBe('--');
                        expect(rs).toBeDefined();
                        expect(rs.rows).toBeDefined();
                        expect(rs.rows.length).toBe(1);
                        expect(rs.rows.item(0).test_data).toBe(test_data);
                        done();
                      }, function (ignored, error) {
                        // NOT EXPECTED:
                        expect(false).toBe(true);
                        expect(error.message).toBe('--');
                        done();
                      });

                    }, function (error) {
                      // TEST GETS HERE [Android/iOS/macOS]:
                      //expect(false).toBe(true);
                      //expect(error.message).toBe('--');
                      expect(error).toBeDefined();
                      db3.close(function() {
                        expect('PLUGIN BEHAVIOR CHANGED PLEASE UPDATE THIS TEST AND CHECK STORED DATA HERE').toBe('--');
                        done();
                      }, function(error) {
                        // TBD ???:
                        //expect(error).toBeDefined();
                        expect(error).not.toBeDefined();
                        // TRY AGAIN:
                        window.sqlitePlugin.openDatabase({name: dbName, key: 'test-password', location: 'default'}, function (db4) {
                          // EXPECTED RESULT:
                          expect(db4).toBeDefined();
                          //* ** ALT 1:
                          db4.transaction(function(tx) {
                            tx.executeSql('SELECT * FROM tt', null, function(ignored, rs) {
                              expect(rs).toBeDefined();
                              expect(rs.rows).toBeDefined();
                              expect(rs.rows.length).toBe(1);
                              expect(rs.rows.item(0).test_data).toBe(test_data);
                              done();
                            }, function (ignored, error) {
                              // NOT EXPECTED:
                              expect(false).toBe(true);
                              expect(error.message).toBe('--');
                              done();
                            });
                          }, function (error) {
                            // NOT EXPECTED:
                            expect(false).toBe(true);
                            expect(error.message).toBe('--');
                            done();
                          });
                        });
                      });
                    });
                    // */
                    /* ** FUTURE TBD ALT 2 [NO SQL CALLBACK RECEIVED]:
                    expect('check1').toBe('--'); // TEST ALT 2 GETS HERE
                    db3.executeSql('SELECT * FROM tt', null, function(rs) {
                      // NOT TRIGGERED Android/iOS:
                      expect(rs).toBeDefined();
                      // FUTURE TBD CHECK rs
                      done();
                    }, function (error) {
                      // NOT TRIGGERED Android/iOS:
                      // NOT EXPECTED:
                      expect(false).toBe(true);
                      expect(error.message).toBe('--');
                      done();
                    });
                    expect('check2').toBe('--'); // TEST ALT 2 GETS HERE
                    // */
                  });

                });

              }, function (error) {
                // NOT EXPECTED:
                expect(false).toBe(true);
                expect(error.message).toBe('--');
                done();
              });
            });
        }, function (error) {
          // NOT EXPECTED:
          expect(false).toBe(true);
          expect(error.message).toBe('--');
          done();
        });
      });
@brodycj brodycj changed the title Cannot read database with CORRECT password after attempt to open with INCORRECT password Cannot read database with CORRECT password directly after attempt to open with INCORRECT password Oct 11, 2016
brodycj pushed a commit to cbforks/cordova-sqlcipher-adapter-dev that referenced this issue Oct 11, 2016
@shanlin2dltk
Copy link

@brodybits is there any way to support changing password scenario like the following?
Scenario: user first created a database 'mydb' with key 'mykey1' using openDatabase() function. Later, due to a use case, he wants to change to key 'mykey2'. Is this possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants