View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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" ) ); // legacy key
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 }