View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.cache;
28  
29  
30  import static org.hamcrest.MatcherAssert.assertThat;
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertFalse;
33  import static org.junit.jupiter.api.Assertions.assertTrue;
34  
35  import java.time.Instant;
36  import java.util.Date;
37  import java.util.HashSet;
38  import java.util.Set;
39  
40  import org.apache.hc.client5.http.cache.HttpCacheEntry;
41  import org.apache.hc.client5.http.cache.HttpCacheStorageEntry;
42  import org.apache.hc.client5.http.cache.Resource;
43  import org.apache.hc.core5.http.Header;
44  import org.apache.hc.core5.http.HttpStatus;
45  import org.apache.hc.core5.http.message.BasicHeader;
46  import org.apache.hc.core5.http.message.StatusLine;
47  import org.junit.jupiter.api.BeforeEach;
48  import org.junit.jupiter.api.Test;
49  
50  @SuppressWarnings("deprecation")
51  class TestByteArrayCacheEntrySerializer {
52  
53      private ByteArrayCacheEntrySerializer impl;
54  
55      @BeforeEach
56      void setUp() {
57          impl = new ByteArrayCacheEntrySerializer();
58      }
59  
60      @Test
61      void canSerializeEntriesWithVariantMapsDeprecatedConstructor() throws Exception {
62          readWriteVerify(makeCacheEntryDeprecatedConstructorWithVariantMap("somekey"));
63      }
64  
65      @Test
66      void canSerializeEntriesWithVariantMapsAndInstant() throws Exception {
67          readWriteVerify(makeCacheEntryWithVariantMap("somekey"));
68      }
69  
70      @Test
71      void isAllowedClassNameStringTrue() {
72          assertIsAllowedClassNameTrue(String.class.getName());
73      }
74  
75      @Test
76      void isAllowedClassNameStringArrayTrue() {
77          assertIsAllowedClassNameTrue("[L" + String.class.getName());
78      }
79  
80      @Test
81      void isAllowedClassNameStringArrayArrayTrue() {
82          assertIsAllowedClassNameTrue("[[L" + String.class.getName());
83      }
84  
85      @Test
86      void isAllowedClassNameDataTrue() {
87          assertIsAllowedClassNameTrue(Date.class.getName());
88      }
89  
90      @Test
91      void isAllowedClassNameInstantTrue() {
92          assertIsAllowedClassNameTrue(Instant.class.getName());
93      }
94  
95      @Test
96      void isAllowedClassNameStatusLineTrue() {
97          assertIsAllowedClassNameTrue(StatusLine.class.getName());
98      }
99  
100     @Test
101     void isAllowedClassNameResourceTrue() {
102         assertIsAllowedClassNameTrue(Resource.class.getName());
103     }
104 
105     @Test
106     void isAllowedClassNameByteArrayTrue() {
107         assertIsAllowedClassNameTrue("[B");
108     }
109 
110     @Test
111     void isAllowedClassNameByteArrayArrayTrue() {
112         assertIsAllowedClassNameTrue("[[B");
113     }
114 
115     @Test
116     void isAllowedClassNameCharArrayTrue() {
117         assertIsAllowedClassNameTrue("[C");
118     }
119 
120     @Test
121     void isAllowedClassNameCharArrayArrayTrue() {
122         assertIsAllowedClassNameTrue("[[C");
123     }
124 
125     @Test
126     void isAllowedClassNameDoubleArrayTrue() {
127         assertIsAllowedClassNameTrue("[D");
128     }
129 
130     @Test
131     void isAllowedClassNameDoubleArrayArrayTrue() {
132         assertIsAllowedClassNameTrue("[[D");
133     }
134 
135     @Test
136     void isAllowedClassNameFloatArrayTrue() {
137         assertIsAllowedClassNameTrue("[F");
138     }
139 
140     @Test
141     void isAllowedClassNameFloatArrayArrayTrue() {
142         assertIsAllowedClassNameTrue("[[F");
143     }
144 
145     @Test
146     void isAllowedClassNameIntArrayTrue() {
147         assertIsAllowedClassNameTrue("[I");
148     }
149 
150     @Test
151     void isAllowedClassNameIntArrayArrayTrue() {
152         assertIsAllowedClassNameTrue("[[I");
153     }
154 
155     @Test
156     void isAllowedClassNameLongArrayTrue() {
157         assertIsAllowedClassNameTrue("[J");
158     }
159 
160     @Test
161     void isAllowedClassNameLongArrayArrayTrue() {
162         assertIsAllowedClassNameTrue("[[J");
163     }
164 
165     @Test
166     void isAllowedClassNameShortArrayTrue() {
167         assertIsAllowedClassNameTrue("[S");
168     }
169 
170     @Test
171     void isAllowedClassNameShortArrayArrayTrue() {
172         assertIsAllowedClassNameTrue("[[S");
173     }
174 
175     @Test
176     void isAllowedClassNameCollectionsInvokerTransformerFalse() {
177         assertIsAllowedClassNameFalse("org.apache.commons.collections.functors.InvokerTransformer");
178     }
179 
180     @Test
181     void isAllowedClassNameCollections4InvokerTransformerFalse() {
182         assertIsAllowedClassNameFalse("org.apache.commons.collections4.functors.InvokerTransformer");
183     }
184 
185     @Test
186     void isAllowedClassNameCollectionsInstantiateTransformerFalse() {
187         assertIsAllowedClassNameFalse("org.apache.commons.collections.functors.InstantiateTransformer");
188     }
189 
190     @Test
191     void isAllowedClassNameCollections4InstantiateTransformerFalse() {
192         assertIsAllowedClassNameFalse("org.apache.commons.collections4.functors.InstantiateTransformer");
193     }
194 
195     @Test
196     void isAllowedClassNameGroovyConvertedClosureFalse() {
197         assertIsAllowedClassNameFalse("org.codehaus.groovy.runtime.ConvertedClosure");
198     }
199 
200     @Test
201     void isAllowedClassNameGroovyMethodClosureFalse() {
202         assertIsAllowedClassNameFalse("org.codehaus.groovy.runtime.MethodClosure");
203     }
204 
205     @Test
206     void isAllowedClassNameSpringObjectFactoryFalse() {
207         assertIsAllowedClassNameFalse("org.springframework.beans.factory.ObjectFactory");
208     }
209 
210     @Test
211     void isAllowedClassNameCalanTemplatesImplFalse() {
212         assertIsAllowedClassNameFalse("com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl");
213     }
214 
215     @Test
216     void isAllowedClassNameCalanTemplatesImplArrayFalse() {
217         assertIsAllowedClassNameFalse("[Lcom.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl");
218     }
219 
220     @Test
221     void isAllowedClassNameJavaRmiRegistryFalse() {
222         assertIsAllowedClassNameFalse("java.rmi.registry.Registry");
223     }
224 
225     @Test
226     void isAllowedClassNameJavaRmiServerRemoteObjectInvocationHandlerFalse() {
227         assertIsAllowedClassNameFalse("java.rmi.server.RemoteObjectInvocationHandler");
228     }
229 
230     @Test
231     void isAllowedClassNameJavaxXmlTransformTemplatesFalse() {
232         assertIsAllowedClassNameFalse("javax.xml.transform.Templates");
233     }
234 
235     @Test
236     void isAllowedClassNameJavaxManagementMBeanServerInvocationHandlerFalse() {
237         assertIsAllowedClassNameFalse("javax.management.MBeanServerInvocationHandler");
238     }
239 
240     private static void assertIsAllowedClassNameTrue(final String className) {
241         assertTrue(ByteArrayCacheEntrySerializer.RestrictedObjectInputStream.isAllowedClassName(className));
242     }
243 
244     private static void assertIsAllowedClassNameFalse(final String className) {
245         assertFalse(ByteArrayCacheEntrySerializer.RestrictedObjectInputStream.isAllowedClassName(className));
246     }
247 
248     public void readWriteVerify(final HttpCacheStorageEntry writeEntry) throws Exception {
249         // write the entry
250         final byte[] bytes = impl.serialize(writeEntry);
251         // read the entry
252         final HttpCacheStorageEntry readEntry = impl.deserialize(bytes);
253         // compare
254         assertEquals(readEntry.getKey(), writeEntry.getKey());
255         assertThat(readEntry.getContent(), HttpCacheEntryMatcher.equivalent(writeEntry.getContent()));
256     }
257 
258 
259     private HttpCacheStorageEntry makeCacheEntryDeprecatedConstructorWithVariantMap(final String key) {
260         final Header[] headers = new Header[5];
261         for (int i = 0; i < headers.length; i++) {
262             headers[i] = new BasicHeader("header" + i, "value" + i);
263         }
264         final Set<String> variants = new HashSet<>();
265         variants.add("test variant 1");
266         variants.add("test variant 2");
267         final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(
268                 Instant.now(),
269                 Instant.now(),
270                 HttpStatus.SC_OK,
271                 headers,
272                 variants);
273 
274         return new HttpCacheStorageEntry(key, cacheEntry);
275     }
276 
277     private HttpCacheStorageEntry makeCacheEntryWithVariantMap(final String key) {
278         final Header[] headers = new Header[5];
279         for (int i = 0; i < headers.length; i++) {
280             headers[i] = new BasicHeader("header" + i, "value" + i);
281         }
282         final Set<String> variants = new HashSet<>();
283         variants.add("test variant 1");
284         variants.add("test variant 2");
285         final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(
286                 Instant.now(),
287                 Instant.now(),
288                 HttpStatus.SC_OK,
289                 headers,
290                 variants);
291 
292         return new HttpCacheStorageEntry(key, cacheEntry);
293     }
294 
295 }