LightJason - AgentSpeak(L++)
TestCActionCrypto.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.action.builtin;
25 
26 import com.codepoetics.protonpack.StreamUtils;
27 import com.tngtech.java.junit.dataprovider.DataProvider;
28 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
29 import com.tngtech.java.junit.dataprovider.UseDataProvider;
30 import org.apache.commons.lang3.tuple.ImmutablePair;
31 import org.apache.commons.lang3.tuple.ImmutableTriple;
32 import org.apache.commons.lang3.tuple.Pair;
33 import org.apache.commons.lang3.tuple.Triple;
34 import org.junit.Assert;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
47 
48 import javax.crypto.KeyGenerator;
49 import java.security.Key;
50 import java.security.NoSuchAlgorithmException;
51 import java.util.ArrayList;
52 import java.util.Collections;
53 import java.util.List;
54 import java.util.stream.Collectors;
55 import java.util.stream.Stream;
56 
57 
61 @RunWith( DataProviderRunner.class )
62 public final class TestCActionCrypto extends IBaseTest
63 {
64 
70  @DataProvider
71  public static Object[] generatehash()
72  {
73  return StreamUtils.zip(
74  Stream.of( "adler-32", "crc-32", "crc-32c", "murmur3-32", "murmur3-128", "siphash-2-4" ),
75  Stream.of( new String[]{"7804c01a", "911c63b0"}, new String[]{"45154713", "29369833"},
76  new String[]{"387e0716", "4411bf68"}, new String[]{"306202a8", "08b9852d"},
77  new String[]{"636cc4ff5f7ed59b51f29d6d949b4709", "f4459439308d1248efc0532fb4cd6d79"},
78  new String[]{"4f27c08e5981bc5a", "82ee572bf0a0dde4"}
79  ),
80 
81  ImmutablePair::new
82  ).toArray();
83  }
84 
90  @Test
91  @UseDataProvider( "generatecrypt" )
92  public final void createkey( final Triple<String, Integer, Integer> p_crypt )
93  {
94  final List<ITerm> l_return = new ArrayList<>();
95 
96  new CCreateKey().execute(
97  false, IContext.EMPTYPLAN,
98  Stream.of( CRawTerm.from( p_crypt.getLeft() ) ).collect( Collectors.toList() ),
99  l_return
100  );
101 
102  Assert.assertEquals( l_return.size(), p_crypt.getMiddle().intValue() );
103  }
104 
105 
111  @Test
112  public final void wrongalgorithm() throws NoSuchAlgorithmException
113  {
114  final Key l_key = KeyGenerator.getInstance( "HmacSHA1" ).generateKey();
115 
116  Assert.assertFalse(
117  new CEncrypt().execute(
118  false, IContext.EMPTYPLAN,
119  Stream.of( l_key ).map( CRawTerm::from ).collect( Collectors.toList() ),
120  Collections.emptyList()
121  ).value()
122  );
123 
124 
125  Assert.assertFalse(
126  new CDecrypt().execute(
127  false, IContext.EMPTYPLAN,
128  Stream.of( l_key ).map( CRawTerm::from ).collect( Collectors.toList() ),
129  Collections.emptyList()
130  ).value()
131  );
132  }
133 
139  @Test
140  public final void decryptexecutionerror() throws NoSuchAlgorithmException
141  {
142  final Pair<Key, Key> l_key = EAlgorithm.RSA.generateKey();
143  final List<ITerm> l_return = new ArrayList<>();
144 
145  new CEncrypt().execute(
146  false, IContext.EMPTYPLAN,
147  Stream.of( l_key.getLeft(), "xxx" ).map( CRawTerm::from ).collect( Collectors.toList() ),
148  l_return
149  );
150 
151  Assert.assertEquals( l_return.size(), 1 );
152  Assert.assertFalse(
153  new CDecrypt().execute(
154  false, IContext.EMPTYPLAN,
155  Stream.of( l_key.getLeft(), l_return.get( 0 ).<String>raw() ).map( CRawTerm::from ).collect( Collectors.toList() ),
156  l_return
157  ).value()
158  );
159  }
160 
166  @Test
167  @UseDataProvider( "generatehash" )
168  public final void hash( final Pair<String, String[]> p_hash )
169  {
170  final List<ITerm> l_return = new ArrayList<>();
171 
172  new CHash().execute(
173  false, IContext.EMPTYPLAN,
174  Stream.of( CRawTerm.from( p_hash.getLeft() ), CRawTerm.from( "test string" ), CRawTerm.from( 1234 ) ).collect( Collectors.toList() ),
175  l_return
176  );
177 
178  Assert.assertArrayEquals( l_return.stream().map( ITerm::<String>raw ).toArray( String[]::new ), p_hash.getRight() );
179  }
180 
184  @Test( expected = CRuntimeException.class )
185  public final void hashexception()
186  {
187  new CHash().execute(
188  false, IContext.EMPTYPLAN,
189  Stream.of( CRawTerm.from( "xxx" ), CRawTerm.from( 1234 ) ).collect( Collectors.toList() ),
190  Collections.emptyList()
191  );
192  }
193 
199  @DataProvider
200  public static Object[] generatecrypt()
201  {
202  return Stream.of(
203  new ImmutableTriple<>( "des", 1, 0 ),
204  new ImmutableTriple<>( "aes", 1, 0 ),
205  new ImmutableTriple<>( "rsa", 2, 1 )
206  ).toArray();
207  }
208 
212  @Test
213  public final void createkeyError()
214  {
215  Assert.assertFalse(
216 
217  new CCreateKey().execute(
218  false, IContext.EMPTYPLAN,
219  Stream.of( CRawTerm.from( "test" ) ).collect( Collectors.toList() ),
220  Collections.emptyList()
221  ).value()
222  );
223  }
224 
230  @Test
231  @UseDataProvider( "generatecrypt" )
232  public final void encryptdecreypt( final Triple<String, Integer, Integer> p_crypt )
233  {
234  final List<ITerm> l_returnkey = new ArrayList<>();
235 
236  new CCreateKey().execute(
237  false, IContext.EMPTYPLAN,
238  Stream.of( CRawTerm.from( p_crypt.getLeft() ) ).collect( Collectors.toList() ),
239  l_returnkey
240  );
241 
242  Assert.assertEquals( l_returnkey.size(), p_crypt.getMiddle().intValue() );
243 
244 
245  final List<ITerm> l_returnencrypt = new ArrayList<>();
246 
247  new CEncrypt().execute(
248  false, IContext.EMPTYPLAN,
249  Stream.of( l_returnkey.get( 0 ), CRawTerm.from( "test string" ), CRawTerm.from( 12345 ) ).collect( Collectors.toList() ),
250  l_returnencrypt
251  );
252 
253 
254  final List<ITerm> l_return = new ArrayList<>();
255 
256  new CDecrypt().execute(
257  false, IContext.EMPTYPLAN,
258  Stream.concat( Stream.of( l_returnkey.get( p_crypt.getRight() ) ), l_returnencrypt.stream() ).collect( Collectors.toList() ),
259  l_return
260  );
261 
262 
263  Assert.assertEquals( l_return.size(), 2 );
264  Assert.assertEquals( l_return.get( 0 ).raw(), "test string" );
265  Assert.assertEquals( l_return.get( 1 ).<Number>raw(), 12345 );
266  }
267 
268 }
static Object [] generatecrypt()
data provider generator of crypt key definition
base test class with helpers
Definition: IBaseTest.java:33
creates an encrypting / decrypting key pair.
Definition: CCreateKey.java:52
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
dencrypting algorithm for decrypting data.
Definition: CDecrypt.java:55
execution context with local data
Definition: IContext.java:42
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CEncrypt.java:72
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
final void decryptexecutionerror()
test decrypt execution array
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CCreateKey.java:68
final void createkeyError()
test key generation on error call
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
static Object [] generatehash()
data provider generator of hash definition
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CDecrypt.java:71
term structure for simple datatypes
Definition: CRawTerm.java:45