001package csheets.core.ext.share;
002
003import static org.junit.Assert.*;
004
005import org.junit.Test;
006
007import csheets.ext.share.core.Validate;
008
009/**
010 * Class that will test the class Validate
011 * 
012 * @see Validate
013 * @author Andre
014 * 
015 */
016public class ValidateTest {
017
018        /**
019         * Test if the introduced ip is correct
020         */
021        @Test
022        public void testCheckIFIPIsCorrect() {
023                String ipTestValid1 = "localhost";
024                String ipTestInvalid1 = "localehoste";
025                String ipTestValid2 = "127.0.0.1";
026                String ipTestInvalid2 = "256.256.256.0";
027
028                assertFalse(Validate.checkIFIPIsCorrect(ipTestInvalid1));
029                assertFalse(Validate.checkIFIPIsCorrect(ipTestInvalid2));
030                assertTrue(Validate.checkIFIPIsCorrect(ipTestValid1));
031                assertTrue(Validate.checkIFIPIsCorrect(ipTestValid2));
032        }
033
034        /**
035         * Test if the introduced port is allowed. Must be between 49152 and 65535
036         */
037        @Test
038        public void testCheckPort() {
039                int portValid1 = 50000;
040                int portValid2 = 65535;
041                int portValid3 = 49152;
042                int portInvalid1 = 1;
043                int portInvalid2 = 49151;
044                int portInvalid3 = 65536;
045
046                assertFalse(Validate.checkPort(portInvalid1));
047                assertFalse(Validate.checkPort(portInvalid2));
048                assertFalse(Validate.checkPort(portInvalid3));
049                assertTrue(Validate.checkPort(portValid1));
050                assertTrue(Validate.checkPort(portValid2));
051                assertTrue(Validate.checkPort(portValid3));
052        }
053
054        /**
055         * Test if the introduced port is a number
056         */
057        @Test
058        public void testCheckIfANumber() {
059                String portValid1 = "12312";
060                String portValid2 = "11";
061                String portValid3 = "123456789";
062                String portInvalid1 = "a";
063                String portInvalid2 = "a1";
064                String portInvalid3 = "aadasd123";
065
066                assertFalse(Validate.checkIfANumber(portInvalid1));
067                assertFalse(Validate.checkIfANumber(portInvalid2));
068                assertFalse(Validate.checkIfANumber(portInvalid3));
069                assertTrue(Validate.checkIfANumber(portValid1));
070                assertTrue(Validate.checkIfANumber(portValid2));
071                assertTrue(Validate.checkIfANumber(portValid3));
072        }
073
074        /**
075         * Test if the method to remove a message from datagram works
076         */
077        @Test
078        public void testRemoveMessage() {
079                String ori1 = "3-ola";
080                String esp1 = "ola";
081                String ori2 = "7-benfica";
082                String esp2 = "benfica";
083
084                assertEquals(esp1, Validate.removeMessage(ori1));
085                assertEquals(esp2, Validate.removeMessage(ori2));
086        }
087
088        /**
089     * Test the encrypt method
090     */
091        @Test
092        public void testEncrypt() {
093                String pass1 = "secret";
094                String hash1 = "4p4t4u0b4n0e410a4s0f0123x4u070f01701301c054o";
095
096                String pass2 = "benfica";
097                String hash2 = "0901c4m0d0d3o3u054l4w3s090a0133t4l500190123p";
098
099                String pass3 = "it051S8X)33%y7s";
100                String hash3 = "0f0184s0f3o0d3w3r403v040174u3u3s0w3q4r0c4u";
101
102                assertEquals(Validate.encrypt(pass1.getBytes()), hash1);
103                assertEquals(Validate.encrypt(pass2.getBytes()), hash2);
104                assertEquals(Validate.encrypt(pass3.getBytes()), hash3);
105        }
106}