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 RegexTest {
010:
011: private PasswordFile passwords;
012: private File passwordFile;
013: private PasswordCipher cipher = new HashingCipher();
014: private String passwordFilename = "testDir/password.txt";
015:
016: public void setUp() throws Exception {
017: new File("testDir").mkdir();
018:
019: passwords = new PasswordFile(passwordFilename, cipher);
020: passwordFile = new File(passwordFilename);
021: }
022:
023: public void tearDown() throws Exception {
024: FileUtil.deleteFileSystemDirectory("testDir");
025: }
026:
027: public void testSavePasswordForFirstUser() throws Exception {
028: passwords.savePassword("Aladdin", "open sesame");
029: assertTrue(passwordFile.exists());
030: String contents = FileUtil.getFileContent(passwordFile);
031: assertSubString("Aladdin:" + cipher.encrypt("open sesame"),
032: contents);
033: }
034:
035: public void testChangePasswordForFirstUser() throws Exception {
036: passwords.savePassword("Aladdin", "open sesame");
037: passwords.savePassword("Aladdin", "open please");
038: String contents = FileUtil.getFileContent(passwordFile);
039: assertNotSubString("Aladdin:" + cipher.encrypt("open sesame"),
040: contents);
041: assertSubString("Aladdin:" + cipher.encrypt("open please"),
042: contents);
043: }
044:
045: public void testMultipleUsers() throws Exception {
046: addTMNTUsers();
047: String contents = FileUtil.getFileContent(passwordFile);
048: assertSubString("Leonardo:" + cipher.encrypt("katana"),
049: contents);
050: assertSubString("Donatello:" + cipher.encrypt("bo"), contents);
051: assertSubString("Michaelangelo:" + cipher.encrypt("nunchaku"),
052: contents);
053: assertSubString("Rafael:" + cipher.encrypt("sai"), contents);
054: }
055:
056: public void testAddChangePasswordWithMultipleUsers()
057: throws Exception {
058: addTMNTUsers();
059: passwords.savePassword("Donatello", "manrikigusari");
060: String contents = FileUtil.getFileContent(passwordFile);
061: assertSubString("Leonardo:" + cipher.encrypt("katana"),
062: contents);
063: assertSubString("Donatello:" + cipher.encrypt("manrikigusari"),
064: contents);
065: assertSubString("Michaelangelo:" + cipher.encrypt("nunchaku"),
066: contents);
067: assertSubString("Rafael:" + cipher.encrypt("sai"), contents);
068: }
069:
070: private void addTMNTUsers() throws Exception {
071: passwords.savePassword("Leonardo", "katana");
072: passwords.savePassword("Donatello", "bo");
073: passwords.savePassword("Michaelangelo", "nunchaku");
074: passwords.savePassword("Rafael", "sai");
075: }
076:
077: public void testWritesAndReadsCipherType1() throws Exception {
078: passwords.savePassword("rocksteady", "horn");
079: String contents = FileUtil.getFileContent(passwordFile);
080: assertSubString("!fitnesse.authentication.HashingCipher",
081: contents);
082:
083: passwords = new PasswordFile(passwordFilename);
084: assertEquals(HashingCipher.class, passwords.getCipher()
085: .getClass());
086: }
087:
088: public void testWritesAndReadsCipherType2() throws Exception {
089: passwordFilename = "testDir/passwords2.txt";
090: setUp();
091: passwords = new PasswordFile(passwordFilename);
092: passwords.savePassword("rocksteady", "horn");
093: String contents = FileUtil.getFileContent(passwordFile);
094: assertSubString("!fitnesse.authentication.TransparentCipher",
095: contents);
096:
097: passwords = new PasswordFile(passwordFilename);
098: assertEquals(TransparentCipher.class, passwords.getCipher()
099: .getClass());
100: }
101: }
|