View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.provisioning.api.serialization;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  import static org.mockito.Mockito.when;
24  
25  import com.fasterxml.jackson.core.JsonParser;
26  import com.fasterxml.jackson.databind.DeserializationContext;
27  import com.fasterxml.jackson.databind.JsonNode;
28  import com.fasterxml.jackson.databind.node.JsonNodeFactory;
29  import com.fasterxml.jackson.databind.node.ObjectNode;
30  import java.io.IOException;
31  import java.util.Base64;
32  import java.util.HashMap;
33  import java.util.Map;
34  import org.apache.commons.lang3.builder.EqualsBuilder;
35  import org.apache.syncope.core.provisioning.api.AbstractTest;
36  import org.identityconnectors.common.security.EncryptorFactory;
37  import org.identityconnectors.common.security.GuardedString;
38  import org.junit.jupiter.api.Test;
39  import org.mockito.Mock;
40  import org.springframework.test.util.ReflectionTestUtils;
41  
42  public class GuardedStringDeserializerTest extends AbstractTest {
43  
44      private static final String READONLY = "readOnly";
45  
46      private static final String DISPOSED = "disposed";
47  
48      private static final String ENCRYPTED_BYTES = "encryptedBytes";
49  
50      private static final String BASE64_SHA1_HASH = "base64SHA1Hash";
51  
52      private final GuardedStringDeserializer deserializer = new GuardedStringDeserializer();
53  
54      @Mock
55      private JsonParser jp;
56  
57      @Mock
58      private DeserializationContext ctx;
59  
60      @Mock
61      private JsonNode node;
62  
63      @Test
64      public void deserialize() throws IOException {
65          Map<String, JsonNode> kids = new HashMap<>();
66          kids.put(READONLY, node);
67          kids.put(DISPOSED, node);
68          kids.put(ENCRYPTED_BYTES, node);
69          kids.put(BASE64_SHA1_HASH, node);
70          ObjectNode tree = new ObjectNode(JsonNodeFactory.instance, kids);
71          String testString = "randomTestString";
72          byte[] encryptedBytes = EncryptorFactory.getInstance().getDefaultEncryptor().encrypt(testString.getBytes());
73          String encryptedString = Base64.getEncoder().encodeToString(encryptedBytes);
74  
75          when(jp.readValueAsTree()).thenReturn(tree);
76          when(node.asText()).thenReturn(encryptedString);
77          assertEquals(Boolean.FALSE, ReflectionTestUtils.getField(deserializer.deserialize(jp, ctx), READONLY));
78          kids.remove(READONLY);
79          assertEquals(Boolean.FALSE, ReflectionTestUtils.getField(deserializer.deserialize(jp, ctx), DISPOSED));
80          kids.remove(DISPOSED);
81          assertEquals(encryptedString, 
82                  ReflectionTestUtils.getField(deserializer.deserialize(jp, ctx), BASE64_SHA1_HASH));
83  
84          kids.remove(BASE64_SHA1_HASH);
85          GuardedString expected = new GuardedString(new String(testString.getBytes()).toCharArray());
86          assertTrue(EqualsBuilder.reflectionEquals(ReflectionTestUtils.getField(expected, ENCRYPTED_BYTES),
87                  ReflectionTestUtils.getField(deserializer.deserialize(jp, ctx), ENCRYPTED_BYTES)));
88      }
89  }