/*
 * 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.adaptris.security.password;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import com.adaptris.security.exc.PasswordException;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import org.junit.Test;

public class SeededAesPbeCryptoTest {

  private static final String TEXT = "MYPASSWORD";

  private static final String SEED_FILE = System.getProperty("user.dir") + "/build.gradle";
  private static final String NON_EXISTENT_FILE = "/surely/this/file/does/not/exist";

  @Test
  public void testResolveFromProperties() throws Exception {
    Properties p = new Properties();
    p.setProperty("file-exists", SEED_FILE);
    p.setProperty("non-existent-file", NON_EXISTENT_FILE);
    assertEquals(SEED_FILE, SeededAesPbeCrypto.fromProperties("file-exists", p));
    assertThrows(Exception.class, () -> SeededAesPbeCrypto.fromProperties("non-existent-file", p));
    assertThrows(Exception.class, () -> SeededAesPbeCrypto.fromProperties("no-key", p));
  }

  // Technically it's a slightly dodgy test because of the way ServiceLoaders work, so we can't
  // use the Password static functions, we have to use SeededAesPbeCrypto directly.
  @Test
  public void testSeeded() throws Exception {
    SeededAesPbeCrypto passworder = new SeededAesPbeCrypto(SEED_FILE);
    String encoded = passworder.encode(TEXT, StandardCharsets.UTF_8.name());
    assertEquals(TEXT, passworder.decode(encoded));
  }

  @Test
  public void testSeededEncodeException() throws Exception {
    assertThrows(PasswordException.class, () -> new SeededAesPbeCrypto(NON_EXISTENT_FILE).encode(TEXT));
  }

  @Test
  public void testSeededDecodeException() throws Exception {
    assertThrows(PasswordException.class, () -> new SeededAesPbeCrypto(NON_EXISTENT_FILE).decode(TEXT));
  }
}

@@ -688,16 +688,62 @@
 hrows;%0A%0A
+import com.adaptris.core.stubs.TempFileUtils;%0A
 import c
@@ -976,100 +976,8 @@
 %22;%0A%0A
-  private static final String SEED_FILE = System.getProperty(%22user.dir%22) + %22/build.gradle%22;%0A
   pr
@@ -1165,16 +1165,114 @@
 ties();%0A
+    String seedFile = TempFileUtils.createTrackedFile(p, () -%3E %22Hello World%22).getCanonicalPath();%0A
     p.se
@@ -1296,25 +1296,24 @@
 xists%22, 
-SEED_FILE
+seedFile
 );%0A    p
@@ -1383,25 +1383,24 @@
 tEquals(
-SEED_FILE
+seedFile
 , Seeded
@@ -1871,24 +1871,151 @@
 Exception %7B%0A
+    Object o = new Object();%0A    String seedFile = TempFileUtils.createTrackedFile(o, () -%3E %22Hello World%22).getCanonicalPath();%0A
     SeededAe
@@ -2065,17 +2065,16 @@
 pto(
-SEED_FILE
+seedFile
 );%0A 

use a dynamic ssed file