Changeset d6e3501


Ignore:
Timestamp:
Nov 23, 2018 2:22:08 PM (17 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
43e0d4f9, d4caafb5
Parents:
535f2da
Message:

Test: Move ElGamalTest? from core to router to follow tested class

Files:
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • core/java/test/junit/net/i2p/crypto/CryptoTestSuite.java

    r535f2da rd6e3501  
    2525        suite.addTestSuite(CryptixRijndael_AlgorithmTest.class);
    2626        suite.addTestSuite(DSATest.class);
    27         suite.addTestSuite(ElGamalTest.class);
    2827        suite.addTestSuite(HMACSHA256Test.class);
    2928        suite.addTestSuite(KeyGeneratorTest.class);
  • router/java/test/junit/net/i2p/router/crypto/ElGamalTest.java

    r535f2da rd6e3501  
    1 package net.i2p.crypto;
     1package net.i2p.router.crypto;
    22/*
    33 * free (adj.): unencumbered; not under the control of others
     
    1717import junit.framework.TestCase;
    1818import net.i2p.I2PAppContext;
     19import net.i2p.crypto.KeyGenerator;
     20import net.i2p.crypto.SHA256Generator;
    1921import net.i2p.data.Base64;
    2022import net.i2p.data.DataFormatException;
     
    2527import net.i2p.data.SessionKey;
    2628import net.i2p.data.SessionTag;
     29import net.i2p.router.crypto.ElGamalAESEngine;
    2730import net.i2p.util.RandomSource;
    2831
     
    127130    protected void setUp() {
    128131        _context = I2PAppContext.getGlobalContext();
    129         Object o = YKGenerator.class;
     132        //Object o = YKGenerator.class;
    130133    }
    131134   
     
    156159        String msg = "Hello world";
    157160       
    158         byte encrypted[] = _context.elGamalAESEngine().encryptAESBlock(DataHelper.getASCII(msg), sessionKey, iv, null, null, 64);
     161        ElGamalAESEngine e = new ElGamalAESEngine(_context);
     162        byte encrypted[] = e.encryptAESBlock(DataHelper.getASCII(msg), sessionKey, iv, null, null, 64);
    159163        Set<SessionTag> foundTags = new HashSet<SessionTag>();
    160164        SessionKey foundKey = new SessionKey();
    161165        byte decrypted[] = null;
    162166        try{
    163             decrypted = _context.elGamalAESEngine().decryptAESBlock(encrypted, 0, encrypted.length, sessionKey, iv, null, foundTags, foundKey);
     167            decrypted = e.decryptAESBlock(encrypted, 0, encrypted.length, sessionKey, iv, null, foundTags, foundKey);
    164168        }catch(DataFormatException dfe){
    165169            dfe.printStackTrace();
     
    181185        if (key == null)
    182186            key = _context.sessionKeyManager().createSession(pubKey);
    183         byte[] encrypted = _context.elGamalAESEngine().encrypt(DataHelper.getASCII(msg), pubKey, key, null, null, 64);
     187        ElGamalAESEngine e = new ElGamalAESEngine(_context);
     188        byte[] encrypted = e.encrypt(DataHelper.getASCII(msg), pubKey, key, null, null, 64);
    184189        byte[] decrypted = null;
    185190        try{
    186             decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey, _context.sessionKeyManager());
     191            decrypted = e.decrypt(encrypted, privKey, _context.sessionKeyManager());
    187192        }catch(DataFormatException dfe){
    188193            dfe.printStackTrace();
     
    257262   
    258263    public void testLoop(){
     264        ElGamalAESEngine e = new ElGamalAESEngine(_context);
    259265        for(int i = 0; i < 5; i++){
    260266            Object keys[] = KeyGenerator.getInstance().generatePKIKeypair();
     
    268274                key = _context.sessionKeyManager().createSession(pubKey);
    269275           
    270             byte[] encrypted = _context.elGamalAESEngine().encrypt(msg, pubKey, key, null, null, 1024);
     276            byte[] encrypted = e.encrypt(msg, pubKey, key, null, null, 1024);
    271277            byte[] decrypted = null;
    272278            try{
    273                 decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey, _context.sessionKeyManager());
     279                decrypted = e.decrypt(encrypted, privKey, _context.sessionKeyManager());
    274280            }catch(DataFormatException dfe){
    275281                dfe.printStackTrace();
     
    371377    }
    372378   
     379/****
     380Package private, move back to net.i2p.crypto if we want to test it
    373381    public void testYKGen(){
    374382        RandomSource.getInstance().nextBoolean();
     
    379387        }
    380388    }
     389****/
    381390}
  • router/java/test/junit/net/i2p/router/crypto/SessionEncryptionTest.java

    r535f2da rd6e3501  
    2222import net.i2p.data.SessionKey;
    2323import net.i2p.data.SessionTag;
     24import net.i2p.router.crypto.ElGamalAESEngine;
    2425
    2526/**
     
    4950        byte[] msg = DataHelper.getASCII("msg 1");
    5051       
    51         byte emsg[] = _context.elGamalAESEngine().encrypt(msg, pubKey, curKey, null, null, 64);
    52         byte dmsg[] = _context.elGamalAESEngine().decrypt(emsg, privKey, skm);
     52        ElGamalAESEngine e = new ElGamalAESEngine(_context);
     53        byte emsg[] = e.encrypt(msg, pubKey, curKey, null, null, 64);
     54        byte dmsg[] = e.decrypt(emsg, privKey, skm);
    5355        assertTrue(DataHelper.eq(dmsg, msg));
    5456    }
     
    6365        byte[] msg = DataHelper.getASCII("msg 2");
    6466       
    65         byte emsg[] = _context.elGamalAESEngine().encrypt(msg, pubKey, curKey, null, null, 64);
    66         byte dmsg[] = _context.elGamalAESEngine().decrypt(emsg, privKey, skm);
     67        ElGamalAESEngine e = new ElGamalAESEngine(_context);
     68        byte emsg[] = e.encrypt(msg, pubKey, curKey, null, null, 64);
     69        byte dmsg[] = e.decrypt(emsg, privKey, skm);
    6770        assertTrue(DataHelper.eq(dmsg, msg));
    6871    }
     
    102105        byte[] msg5 = DataHelper.getASCII("msg 5");
    103106       
    104         byte emsg1[] = _context.elGamalAESEngine().encrypt(msg1, pubKey, curKey, firstTags, null, 64);
    105        
    106         byte dmsg1[] = _context.elGamalAESEngine().decrypt(emsg1, privKey, skm);
     107        ElGamalAESEngine e = new ElGamalAESEngine(_context);
     108        byte emsg1[] = e.encrypt(msg1, pubKey, curKey, firstTags, null, 64);
     109       
     110        byte dmsg1[] = e.decrypt(emsg1, privKey, skm);
    107111        assertTrue(DataHelper.eq(dmsg1, msg1));
    108112       
     
    117121        assertNotNull(curTag);
    118122       
    119         byte emsg2[] = _context.elGamalAESEngine().encrypt(msg2, pubKey, curKey, null, curTag, 64);
    120        
    121         byte dmsg2[] = _context.elGamalAESEngine().decrypt(emsg2, privKey, skm);
     123        byte emsg2[] = e.encrypt(msg2, pubKey, curKey, null, curTag, 64);
     124       
     125        byte dmsg2[] = e.decrypt(emsg2, privKey, skm);
    122126        assertTrue(DataHelper.eq(dmsg2, msg2));
    123127       
     
    131135        assertNotNull(curKey);
    132136       
    133         byte emsg3[] = _context.elGamalAESEngine().encrypt(msg3, pubKey, curKey, secondTags, curTag, 64);
    134        
    135         byte dmsg3[] = _context.elGamalAESEngine().decrypt(emsg3, privKey, skm);
     137        byte emsg3[] = e.encrypt(msg3, pubKey, curKey, secondTags, curTag, 64);
     138       
     139        byte dmsg3[] = e.decrypt(emsg3, privKey, skm);
    136140        assertTrue(DataHelper.eq(dmsg3, msg3));
    137141       
     
    147151        assertNotNull(curKey);
    148152       
    149         byte emsg4[] = _context.elGamalAESEngine().encrypt(msg4, pubKey, curKey, null, curTag, 64);
    150        
    151         byte dmsg4[] = _context.elGamalAESEngine().decrypt(emsg4, privKey, skm);
     153        byte emsg4[] = e.encrypt(msg4, pubKey, curKey, null, curTag, 64);
     154       
     155        byte dmsg4[] = e.decrypt(emsg4, privKey, skm);
    152156        assertTrue(DataHelper.eq(dmsg4, msg4));
    153157       
     
    159163        assertNotNull(curKey);
    160164       
    161         byte emsg5[] = _context.elGamalAESEngine().encrypt(msg5, pubKey, curKey, null, curTag, 64);
    162        
    163         byte dmsg5[] = _context.elGamalAESEngine().decrypt(emsg5, privKey, skm);
     165        byte emsg5[] = e.encrypt(msg5, pubKey, curKey, null, curTag, 64);
     166       
     167        byte dmsg5[] = e.decrypt(emsg5, privKey, skm);
    164168        assertTrue(DataHelper.eq(dmsg5, msg5));
    165169       
     
    202206        byte[] msg5 = DataHelper.getASCII("msg 5");
    203207       
    204         byte emsg1[] = _context.elGamalAESEngine().encrypt(msg1, pubKey, curKey, firstTags, null, 64);
    205        
    206         byte dmsg1[] = _context.elGamalAESEngine().decrypt(emsg1, privKey, skm);
     208        ElGamalAESEngine e = new ElGamalAESEngine(_context);
     209        byte emsg1[] = e.encrypt(msg1, pubKey, curKey, firstTags, null, 64);
     210       
     211        byte dmsg1[] = e.decrypt(emsg1, privKey, skm);
    207212        assertTrue(DataHelper.eq(dmsg1, msg1));
    208213       
     
    217222        assertNotNull(curTag);
    218223       
    219         byte emsg2[] = _context.elGamalAESEngine().encrypt(msg2, pubKey, curKey, null, curTag, 64);
    220        
    221         byte dmsg2[] = _context.elGamalAESEngine().decrypt(emsg2, privKey, skm);
     224        byte emsg2[] = e.encrypt(msg2, pubKey, curKey, null, curTag, 64);
     225       
     226        byte dmsg2[] = e.decrypt(emsg2, privKey, skm);
    222227        assertTrue(DataHelper.eq(dmsg2, msg2));
    223228       
     
    230235        assertNotNull(curKey);
    231236       
    232         byte emsg3[] = _context.elGamalAESEngine().encrypt(msg3, pubKey, curKey, secondTags, curTag, nextKey, 64);
    233        
    234         byte dmsg3[] = _context.elGamalAESEngine().decrypt(emsg3, privKey, skm);
     237        byte emsg3[] = e.encrypt(msg3, pubKey, curKey, secondTags, curTag, nextKey, 64);
     238       
     239        byte dmsg3[] = e.decrypt(emsg3, privKey, skm);
    235240        assertTrue(DataHelper.eq(dmsg3, msg3));
    236241       
     
    246251        assertNotNull(curKey);
    247252       
    248         byte emsg4[] = _context.elGamalAESEngine().encrypt(msg4, pubKey, curKey, null, curTag, 64);
    249        
    250         byte dmsg4[] = _context.elGamalAESEngine().decrypt(emsg4, privKey, skm);
     253        byte emsg4[] = e.encrypt(msg4, pubKey, curKey, null, curTag, 64);
     254       
     255        byte dmsg4[] = e.decrypt(emsg4, privKey, skm);
    251256        assertTrue(DataHelper.eq(dmsg4, msg4));
    252257       
     
    259264        assertNotNull(curKey);
    260265       
    261         byte emsg5[] = _context.elGamalAESEngine().encrypt(msg5, pubKey, curKey, null, curTag, 64);
    262        
    263         byte dmsg5[] = _context.elGamalAESEngine().decrypt(emsg5, privKey, skm);
     266        byte emsg5[] = e.encrypt(msg5, pubKey, curKey, null, curTag, 64);
     267       
     268        byte dmsg5[] = e.decrypt(emsg5, privKey, skm);
    264269        assertTrue(DataHelper.eq(dmsg5, msg5));
    265270       
     
    279284        SessionKey curKey = skm.createSession(pubKey);
    280285       
     286        ElGamalAESEngine e = new ElGamalAESEngine(_context);
    281287        for (int i = 0; i < 1000; i++) {
    282288            Set<SessionTag> tags = null;
     
    294300            byte[] msg = DataHelper.getASCII("msg " + i);
    295301           
    296             byte emsg[] = _context.elGamalAESEngine().encrypt(msg, pubKey, curKey, tags, curTag, nextKey, 64);
    297            
    298             byte dmsg[] = _context.elGamalAESEngine().decrypt(emsg, privKey, skm);
     302            byte emsg[] = e.encrypt(msg, pubKey, curKey, tags, curTag, nextKey, 64);
     303           
     304            byte dmsg[] = e.decrypt(emsg, privKey, skm);
    299305            assertTrue(DataHelper.eq(dmsg, msg));
    300306           
Note: See TracChangeset for help on using the changeset viewer.