summaryrefslogtreecommitdiff
path: root/platform/platform-tests/testSrc/com/intellij/ide/passwordSafe/impl/providers/masterKey/MasterKeyPasswordSafeTest.java
blob: 0e2355f5c3c189c2e975e196b1f5b467cc11bbb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * Copyright 2000-2010 JetBrains s.r.o.
 *
 * 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.
 */
package com.intellij.ide.passwordSafe.impl.providers.masterKey;

import com.intellij.ide.passwordSafe.PasswordSafeException;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * The test for master key password safe
 */
public class MasterKeyPasswordSafeTest {

  @Test
  public void testMasterKey() throws PasswordSafeException {
    PasswordDatabase db = new PasswordDatabase();
    MasterKeyPasswordSafe s1 = testProvider(db);
    s1.resetMasterPassword("pass1", false);
    s1.storePassword(null, MasterKeyPasswordSafeTest.class, "TEST", "test");
    assertEquals("test", s1.getPassword(null, MasterKeyPasswordSafeTest.class, "TEST"));
    assertTrue(s1.changeMasterPassword("pass1", "pass2", false));
    assertEquals("test", s1.getPassword(null, MasterKeyPasswordSafeTest.class, "TEST"));
    MasterKeyPasswordSafe s2 = testProvider(db);
    assertFalse(s2.setMasterPassword("pass1"));
    assertTrue(s2.setMasterPassword("pass2"));
    assertEquals("test", s2.getPassword(null, MasterKeyPasswordSafeTest.class, "TEST"));
    assertTrue(s2.changeMasterPassword("pass2", "pass3", false));
    assertTrue(s2.changeMasterPassword("pass3", "pass4", false));
    assertTrue(s2.setMasterPassword("pass4"));
    assertEquals("test", s2.getPassword(null, MasterKeyPasswordSafeTest.class, "TEST"));
    s2.resetMasterPassword("fail", false);
    assertNull(s2.getPassword(null, MasterKeyPasswordSafeTest.class, "TEST"));
  }

  /**
   * Get test instance of the provider
   * @param db the database to use
   * @return a instance of the provider
   */
  private static MasterKeyPasswordSafe testProvider(final PasswordDatabase db) {
    return new MasterKeyPasswordSafe(db) {
      @Override
      protected boolean isTestMode() {
        return true;
      }
    };
  }

}