1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.util;
35
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.FileNotFoundException;
39 import java.io.FileOutputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.io.OutputStream;
43 import java.util.Properties;
44
45 import fr.paris.lutece.portal.service.datastore.DatastoreService;
46 import fr.paris.lutece.test.LuteceTestCase;
47
48 public class CryptoServiceTest extends LuteceTestCase
49 {
50 private boolean bCryptoKeyExists;
51 private String strOrigCrytoKey;
52
53 @Override
54 protected void setUp( ) throws Exception
55 {
56 super.setUp( );
57 bCryptoKeyExists = DatastoreService.existsKey( CryptoService.DSKEY_CRYPTO_KEY );
58 strOrigCrytoKey = DatastoreService.getDataValue( CryptoService.DSKEY_CRYPTO_KEY, null );
59 DatastoreService.removeData( CryptoService.DSKEY_CRYPTO_KEY );
60 }
61
62 public void testGetCryptoKey( )
63 {
64 String strCryptoKey = CryptoService.getCryptoKey( );
65 assertNotNull( strCryptoKey );
66 assertFalse( strCryptoKey.equals( "A23A5C78H" ) );
67 assertTrue( strCryptoKey.length( ) >= 64 );
68 assertEquals( strCryptoKey, CryptoService.getCryptoKey( ) );
69 }
70
71 public void testGetCryptoKeyLegacy( ) throws FileNotFoundException, IOException
72 {
73 final String strLegacyKey = "LEGACY";
74 setLegacyKey( strLegacyKey );
75 try
76 {
77 assertEquals( strLegacyKey, CryptoService.getCryptoKey( ) );
78 }
79 finally
80 {
81 removeLegacyKey( );
82 }
83 }
84
85 private void removeLegacyKey( ) throws IOException, FileNotFoundException
86 {
87 File luteceProperties = new File( getResourcesDir( ), "WEB-INF/conf/lutece.properties" );
88 Properties props = new Properties( );
89 try ( InputStream is = new FileInputStream( luteceProperties ) )
90 {
91 props.load( is );
92 }
93 props.remove( CryptoService.PROPERTY_CRYPTO_KEY );
94 try ( OutputStream os = new FileOutputStream( luteceProperties ) )
95 {
96 props.store( os, "saved for junit " + this.getClass( ).getCanonicalName( ) );
97 }
98 AppPropertiesService.reloadAll( );
99 }
100
101 private void setLegacyKey( final String strLegacyKey ) throws IOException, FileNotFoundException
102 {
103 File luteceProperties = new File( getResourcesDir( ), "WEB-INF/conf/lutece.properties" );
104 Properties props = new Properties( );
105 try ( InputStream is = new FileInputStream( luteceProperties ) )
106 {
107 props.load( is );
108 }
109 props.setProperty( CryptoService.PROPERTY_CRYPTO_KEY, strLegacyKey );
110 try ( OutputStream os = new FileOutputStream( luteceProperties ) )
111 {
112 props.store( os, "saved for junit " + this.getClass( ).getCanonicalName( ) );
113 }
114 AppPropertiesService.reloadAll( );
115 }
116
117 public void testHmacSHA256( )
118 {
119 assertNotNull( CryptoService.hmacSHA256( "message" ) );
120 }
121
122 @Override
123 protected void tearDown( ) throws Exception
124 {
125 if ( bCryptoKeyExists )
126 {
127 DatastoreService.setDataValue( CryptoService.DSKEY_CRYPTO_KEY, strOrigCrytoKey );
128 }
129 else
130 {
131 DatastoreService.removeData( CryptoService.DSKEY_CRYPTO_KEY );
132 }
133 super.tearDown( );
134 }
135
136 }