001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2010 The Gridarta Developers.
004 *
005 * This program is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 2 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License along
016 * with this program; if not, write to the Free Software Foundation, Inc.,
017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018 */
019
020package net.sf.gridarta.utils;
021
022import java.util.Arrays;
023import org.jetbrains.annotations.NotNull;
024import org.junit.Assert;
025import org.junit.Test;
026
027/**
028 * Regression tests for {@link Xtea}.
029 * @author Andreas Kirschbaum
030 */
031public class XteaTest {
032
033    /**
034     * The key for the test.
035     */
036    @NotNull
037    private static final byte[] key = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef };
038
039    /**
040     * The plaintext for the test.
041     */
042    @NotNull
043    private static final byte[] plaintext = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, (byte) 0x08 };
044
045    /**
046     * The expected ciphertext corresponding to {@link #key} and {@link
047     * #plaintext}.
048     */
049    @NotNull
050    private static final byte[] ciphertext = { (byte) 0x06, (byte) 0x6a, (byte) 0xb0, (byte) 0x51, (byte) 0xf8, (byte) 0xa7, (byte) 0xa3, (byte) 0xc3 };
051
052    /**
053     * Checks that basic encryption and decryption works.
054     */
055    @Test
056    public void test() {
057        final Xtea xtea = new Xtea(key);
058        final byte[][] tmp = new byte[2][8];
059        xtea.encrypt(plaintext, tmp[0]);
060        xtea.decrypt(tmp[0], tmp[1]);
061
062        if (!Arrays.equals(tmp[0], ciphertext)) {
063            Assert.fail("encrypt failure");
064        }
065
066        if (!Arrays.equals(tmp[1], plaintext)) {
067            Assert.fail("decrypt failure");
068        }
069    }
070
071}