package org.jgroups.protocols;


import org.jgroups.auth.MD5Token;
import org.jgroups.auth.SimpleToken;
import org.jgroups.tests.ChannelTestBase;
import org.testng.annotations.Test;

import java.util.Properties;

/**
 * A set of JUnit tests for the AUTH protocol
 * @author Chris Mills
 */
@Test(sequential=false)
public class AUTHTest extends ChannelTestBase {

    /**
     * Creates two SimpleToken objects with identical auth_values and authenticates one against the other
     * Test fails if an exception is thrown or authentication fails
     */
    public static void testSimpleToken() {
        Properties properties=new Properties();
        properties.put("auth_value", "chris");

        SimpleToken token1=new SimpleToken();
        token1.setValue(properties);

        properties.put("auth_value", "chris");

        SimpleToken token2=new SimpleToken();
        token2.setValue(properties);
        assertTrue(token1.authenticate(token2, null));
    }

    /**
     * Creates two SimpleToken objects with different auth_values and authenticates one against the other
     * <p/>
     * Test fails if an exception is thrown or authentication passes
     */
    public static void testSimpleTokenMismatch() {
        Properties properties=new Properties();
        properties.put("auth_value", "chris");

        SimpleToken token1=new SimpleToken();
        token1.setValue(properties);

        properties.put("auth_value", "chrismills");

        SimpleToken token2=new SimpleToken();
        token2.setValue(properties);

        assertTrue(!token1.authenticate(token2, null));
    }

    /**
     * Creates two MD5Token objects with identical auth_values and authenticates one against the other
     * <p/>
     * Uses an MD5 hash type
     * <p/>
     * Test fails if an exception is thrown or authentication fails
     */
    public static void testMD5Token() {
        Properties properties=new Properties();
        properties.put("auth_value", "chris");
        properties.put("token_hash", "MD5");

        MD5Token token1=new MD5Token();
        token1.setValue(properties);

        properties.put("auth_value", "chris");
        properties.put("token_hash", "MD5");

        MD5Token token2=new MD5Token();
        token2.setValue(properties);

        assertTrue(token1.authenticate(token2, null));
    }

    /**
     * Creates two MD5Token objects with different auth_values and authenticates one against the other
     * <p/>
     * Uses an MD5 hash type
     * <p/>
     * Test fails if an exception is thrown or authentication passes
     */
    public static void testMD5TokenMismatch() {
        Properties properties=new Properties();
        properties.put("auth_value", "chris");
        properties.put("token_hash", "MD5");

        MD5Token token1=new MD5Token();
        token1.setValue(properties);

        properties.put("auth_value", "chrismills");
        properties.put("token_hash", "MD5");

        MD5Token token2=new MD5Token();
        token2.setValue(properties);

        assertTrue(!token1.authenticate(token2, null));
    }

    /**
     * Creates two MD5Token objects with identical auth_values and authenticates one against the other
     * <p/>
     * Uses an SHA hash type
     * <p/>
     * Test fails if an exception is thrown or authentication fails
     */
    public static void testSHAToken() {
        Properties properties=new Properties();
        properties.put("auth_value", "chris");
        properties.put("token_hash", "SHA");

        MD5Token token1=new MD5Token();
        token1.setValue(properties);

        properties.put("auth_value", "chris");
        properties.put("token_hash", "SHA");

        MD5Token token2=new MD5Token();
        token2.setValue(properties);

        assertTrue(token1.authenticate(token2, null));
    }

    /**
     * Creates two MD5Token objects with different auth_values and authenticates one against the other
     * <p/>
     * Uses an SHA hash type
     * <p/>
     * Test fails if an exception is thrown or authentication passes
     */
    public static void testSHATokenMismatch() {
        Properties properties=new Properties();
        properties.put("auth_value", "chris");
        properties.put("token_hash", "SHA");

        MD5Token token1=new MD5Token();
        token1.setValue(properties);

        properties.put("auth_value", "chrismills");
        properties.put("token_hash", "SHA");

        MD5Token token2=new MD5Token();
        token2.setValue(properties);

        assertTrue(!token1.authenticate(token2, null));
    }

    /**
     * Test to create an AuthHeader object and set and get the Token object
     * <p/>
     * Fails if an exception is thrown or the set and get don't equal the same object
     */
    public static void testAuthHeader() {
        Properties properties=new Properties();
        properties.put("auth_value", "chris");

        SimpleToken token1=new SimpleToken();
        token1.setValue(properties);

        AuthHeader header=new AuthHeader();
        header.setToken(token1);

        assertTrue(token1 == header.getToken());
    }

    /**
     * Test to create an AuthHeader object and set and get the Token object
     * <p/>
     * Fails if an exception is thrown or the set and get equal the same object
     */
    public static void testAuthHeaderDifferent() {
        Properties properties=new Properties();
        properties.put("auth_value", "chris");

        SimpleToken token1=new SimpleToken();
        token1.setValue(properties);

        properties.put("auth_value", "chris");

        SimpleToken token2=new SimpleToken();
        token2.setValue(properties);

        AuthHeader header=new AuthHeader();
        header.setToken(token1);

        assertTrue(!(token2 == header.getToken()));
    }


}

@@ -906,37 +906,33 @@
 ;%0A        assert
-True(
+ 
 token1.authentic
@@ -940,33 +940,32 @@
 te(token2, null)
-)
 ;%0A    %7D%0A%0A    /**
@@ -1534,37 +1534,33 @@
 %0A%0A        assert
-True(
+ 
 !token1.authenti
@@ -1569,33 +1569,32 @@
 te(token2, null)
-)
 ;%0A    %7D%0A%0A    /**
@@ -2262,37 +2262,33 @@
 %0A%0A        assert
-True(
+ 
 token1.authentic
@@ -2296,33 +2296,32 @@
 te(token2, null)
-)
 ;%0A    %7D%0A%0A    /**
@@ -3003,37 +3003,33 @@
 %0A%0A        assert
-True(
+ 
 !token1.authenti
@@ -3038,33 +3038,32 @@
 te(token2, null)
-)
 ;%0A    %7D%0A%0A    /**
@@ -3735,29 +3735,25 @@
       assert
-True(
+ 
 token1.authe
@@ -3765,33 +3765,32 @@
 te(token2, null)
-)
 ;%0A    %7D%0A%0A    /**
@@ -4480,21 +4480,17 @@
   assert
-True(
+ 
 !token1.
@@ -4515,17 +4515,16 @@
 2, null)
-)
 ;%0A    %7D%0A
@@ -5028,21 +5028,17 @@
   assert
-True(
+ 
 token1 =
@@ -5056,17 +5056,16 @@
 tToken()
-)
 ;%0A    %7D%0A
@@ -5708,13 +5708,9 @@
 sert
-True(
+ 
 !(to
@@ -5735,17 +5735,16 @@
 Token())
-)
 ;%0A    %7D%0A

replaced assertXXX() with assert