001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.authentication;
004:
005: import java.io.File;
006: import fitnesse.testutil.*;
007: import fitnesse.util.FileUtil;
008:
009: public class PasswordFileTest extends AbstractRegex {
010:
011: private PasswordFile passwords;
012:
013: private File passwordFile;
014:
015: private PasswordCipher cipher = new HashingCipher();
016:
017: private String passwordFilename = "testDir/password.txt";
018:
019: public void setUp() throws Exception {
020: new File("testDir").mkdir();
021:
022: passwords = new PasswordFile(passwordFilename, cipher);
023: passwordFile = new File(passwordFilename);
024: }
025:
026: public void tearDown() throws Exception {
027: FileUtil.deleteFileSystemDirectory("testDir");
028: }
029:
030: public void testSavePasswordForFirstUser() throws Exception {
031: passwords.savePassword("Aladdin", "open sesame");
032: assertTrue(passwordFile.exists());
033: String contents = FileUtil.getFileContent(passwordFile);
034: assertSubString("Aladdin:" + cipher.encrypt("open sesame"),
035: contents);
036: }
037:
038: public void testChangePasswordForFirstUser() throws Exception {
039: passwords.savePassword("Aladdin", "open sesame");
040: passwords.savePassword("Aladdin", "open please");
041: String contents = FileUtil.getFileContent(passwordFile);
042: assertNotSubString("Aladdin:" + cipher.encrypt("open sesame"),
043: contents);
044: assertSubString("Aladdin:" + cipher.encrypt("open please"),
045: contents);
046: }
047:
048: public void testMultipleUsers() throws Exception {
049: addTMNTUsers();
050: String contents = FileUtil.getFileContent(passwordFile);
051: assertSubString("Leonardo:" + cipher.encrypt("katana"),
052: contents);
053: assertSubString("Donatello:" + cipher.encrypt("bo"), contents);
054: assertSubString("Michaelangelo:" + cipher.encrypt("nunchaku"),
055: contents);
056: assertSubString("Rafael:" + cipher.encrypt("sai"), contents);
057: }
058:
059: public void testAddChangePasswordWithMultipleUsers()
060: throws Exception {
061: addTMNTUsers();
062: passwords.savePassword("Donatello", "manrikigusari");
063: String contents = FileUtil.getFileContent(passwordFile);
064: assertSubString("Leonardo:" + cipher.encrypt("katana"),
065: contents);
066: assertSubString("Donatello:" + cipher.encrypt("manrikigusari"),
067: contents);
068: assertSubString("Michaelangelo:" + cipher.encrypt("nunchaku"),
069: contents);
070: assertSubString("Rafael:" + cipher.encrypt("sai"), contents);
071: }
072:
073: private void addTMNTUsers() throws Exception {
074: passwords.savePassword("Leonardo", "katana");
075: passwords.savePassword("Donatello", "bo");
076: passwords.savePassword("Michaelangelo", "nunchaku");
077: passwords.savePassword("Rafael", "sai");
078: }
079:
080: public void testWritesAndReadsCipherType1() throws Exception {
081: passwords.savePassword("rocksteady", "horn");
082: String contents = FileUtil.getFileContent(passwordFile);
083: assertSubString("!fitnesse.authentication.HashingCipher",
084: contents);
085:
086: passwords = new PasswordFile(passwordFilename);
087: assertEquals(HashingCipher.class, passwords.getCipher()
088: .getClass());
089: }
090:
091: public void testWritesAndReadsCipherType2() throws Exception {
092: passwordFilename = "testDir/passwords2.txt";
093: setUp();
094: passwords = new PasswordFile(passwordFilename);
095: passwords.savePassword("rocksteady", "horn");
096: String contents = FileUtil.getFileContent(passwordFile);
097: assertSubString("!fitnesse.authentication.TransparentCipher",
098: contents);
099:
100: passwords = new PasswordFile(passwordFilename);
101: assertEquals(TransparentCipher.class, passwords.getCipher()
102: .getClass());
103: }
104: }
|