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.wss4j.policy.tests;
20  
21  import org.apache.neethi.*;
22  import org.apache.wss4j.policy.SP12Constants;
23  import org.apache.wss4j.policy.model.EncryptedParts;
24  import org.junit.jupiter.api.Test;
25  
26  import java.util.Iterator;
27  import java.util.List;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertFalse;
31  import static org.junit.jupiter.api.Assertions.assertNotNull;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  public class EncryptedPartsTest extends AbstractTestBase {
35  
36      @Test
37      public void testEncryptedParts12() throws Exception {
38          String fileName = "EncryptedParts.xml";
39          String policyFile = loadPolicyFile("policy/model/sp12/" + fileName);
40          String serializedPolicyReferenceFile = loadPolicyFile("policy/model/sp12/serialized/" + fileName);
41          String normalizedPolicyReferenceFile = loadPolicyFile("policy/model/sp12/normalized/" + fileName);
42          Policy policy = loadPolicy(policyFile);
43          String serializedPolicy = serializePolicy(policy);
44          assertXMLisEqual(serializedPolicy, serializedPolicyReferenceFile);
45  
46          Iterator<List<Assertion>> alternativeIterator = policy.getAlternatives();
47          int count = 0;
48          while (alternativeIterator.hasNext()) {
49              List<Assertion> alternative = alternativeIterator.next();
50              assertEquals(1, alternative.size());
51              assertTrue(alternative.get(0) instanceof EncryptedParts);
52              EncryptedParts encryptedParts = (EncryptedParts) alternative.get(0);
53              assertFalse(encryptedParts.isNormalized());
54              assertTrue(encryptedParts.isIgnorable());
55              assertTrue(encryptedParts.isOptional());
56              assertEquals(Constants.TYPE_ASSERTION, encryptedParts.getType());
57              assertEquals(SP12Constants.ENCRYPTED_PARTS, encryptedParts.getName());
58              assertTrue(encryptedParts.isBody());
59              assertNotNull(encryptedParts.getAttachments());
60              assertEquals(2, encryptedParts.getHeaders().size());
61              count++;
62          }
63          assertEquals(1, count);
64  
65          policy = policy.normalize(true);
66          serializedPolicy = serializePolicy(policy);
67          assertXMLisEqual(serializedPolicy, normalizedPolicyReferenceFile);
68  
69          alternativeIterator = policy.getAlternatives();
70          List<Assertion> alternative = alternativeIterator.next();
71          assertEquals(0, alternative.size());
72  
73          List<PolicyComponent> policyComponents = policy.getPolicyComponents();
74          assertEquals(1, policyComponents.size());
75          PolicyOperator policyOperator = (PolicyOperator) policyComponents.get(0);
76          policyComponents = policyOperator.getPolicyComponents();
77          assertEquals(2, policyComponents.size());
78          All all = (All) policyComponents.get(0);
79          List<PolicyComponent> policyComponentsAll = all.getAssertions();
80          assertEquals(0, policyComponentsAll.size());
81  
82          all = (All) policyComponents.get(1);
83          policyComponentsAll = all.getAssertions();
84          assertEquals(1, policyComponentsAll.size());
85  
86          Iterator<PolicyComponent> policyComponentIterator = policyComponentsAll.iterator();
87          EncryptedParts encryptedParts = (EncryptedParts) policyComponentIterator.next();
88          assertTrue(encryptedParts.isNormalized());
89          assertTrue(encryptedParts.isIgnorable());
90          assertFalse(encryptedParts.isOptional());
91          assertEquals(Constants.TYPE_ASSERTION, encryptedParts.getType());
92          assertEquals(SP12Constants.ENCRYPTED_PARTS, encryptedParts.getName());
93          assertTrue(encryptedParts.isBody());
94          assertNotNull(encryptedParts.getAttachments());
95          assertEquals(2, encryptedParts.getHeaders().size());
96      }
97  
98      @Test
99      public void testEncryptedParts13() throws Exception {
100         String fileName = "EncryptedParts.xml";
101         String policyFile = loadPolicyFile("policy/model/sp13/" + fileName);
102         String serializedPolicyRefereneFile = loadPolicyFile("policy/model/sp13/serialized/" + fileName);
103         String normalizedPolicyReferenceFile = loadPolicyFile("policy/model/sp13/normalized/" + fileName);
104         Policy policy = loadPolicy(policyFile);
105         String serializedPolicy = serializePolicy(policy);
106         assertXMLisEqual(serializedPolicy, serializedPolicyRefereneFile);
107 
108         Iterator<List<Assertion>> alternativeIterator = policy.getAlternatives();
109         int count = 0;
110         while (alternativeIterator.hasNext()) {
111             List<Assertion> alternative = alternativeIterator.next();
112             assertEquals(1, alternative.size());
113             assertTrue(alternative.get(0) instanceof EncryptedParts);
114             EncryptedParts encryptedParts = (EncryptedParts) alternative.get(0);
115             assertFalse(encryptedParts.isNormalized());
116             assertTrue(encryptedParts.isIgnorable());
117             assertTrue(encryptedParts.isOptional());
118             assertEquals(Constants.TYPE_ASSERTION, encryptedParts.getType());
119             assertEquals(SP12Constants.ENCRYPTED_PARTS, encryptedParts.getName());
120             assertTrue(encryptedParts.isBody());
121             assertNotNull(encryptedParts.getAttachments());
122             assertTrue(encryptedParts.getAttachments().isContentSignatureTransform());
123             assertTrue(encryptedParts.getAttachments().isAttachmentCompleteSignatureTransform());
124             assertEquals(2, encryptedParts.getHeaders().size());
125             count++;
126         }
127         assertEquals(1, count);
128 
129         policy = policy.normalize(true);
130         serializedPolicy = serializePolicy(policy);
131         assertXMLisEqual(serializedPolicy, normalizedPolicyReferenceFile);
132 
133         alternativeIterator = policy.getAlternatives();
134         List<Assertion> alternative = alternativeIterator.next();
135         assertEquals(0, alternative.size());
136 
137         List<PolicyComponent> policyComponents = policy.getPolicyComponents();
138         assertEquals(1, policyComponents.size());
139         PolicyOperator policyOperator = (PolicyOperator) policyComponents.get(0);
140         policyComponents = policyOperator.getPolicyComponents();
141         assertEquals(2, policyComponents.size());
142         All all = (All) policyComponents.get(0);
143         List<PolicyComponent> policyComponentsAll = all.getAssertions();
144         assertEquals(0, policyComponentsAll.size());
145 
146         all = (All) policyComponents.get(1);
147         policyComponentsAll = all.getAssertions();
148         assertEquals(1, policyComponentsAll.size());
149 
150         Iterator<PolicyComponent> policyComponentIterator = policyComponentsAll.iterator();
151         EncryptedParts encryptedParts = (EncryptedParts) policyComponentIterator.next();
152         assertTrue(encryptedParts.isNormalized());
153         assertTrue(encryptedParts.isIgnorable());
154         assertFalse(encryptedParts.isOptional());
155         assertEquals(Constants.TYPE_ASSERTION, encryptedParts.getType());
156         assertEquals(SP12Constants.ENCRYPTED_PARTS, encryptedParts.getName());
157         assertTrue(encryptedParts.isBody());
158         assertNotNull(encryptedParts.getAttachments());
159         assertTrue(encryptedParts.getAttachments().isContentSignatureTransform());
160         assertTrue(encryptedParts.getAttachments().isAttachmentCompleteSignatureTransform());
161         assertEquals(2, encryptedParts.getHeaders().size());
162     }
163 }