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.SignedElements;
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.assertTrue;
32  
33  public class SignedElementsTest extends AbstractTestBase {
34  
35      @Test
36      public void testSignedElements12() throws Exception {
37          String fileName = "SignedElements.xml";
38          String policyFile = loadPolicyFile("policy/model/sp12/" + fileName);
39          String serializedPolicyReferenceFile = loadPolicyFile("policy/model/sp12/serialized/" + fileName);
40          String normalizedPolicyReferenceFile = loadPolicyFile("policy/model/sp12/normalized/" + fileName);
41          Policy policy = loadPolicy(policyFile);
42          String serializedPolicy = serializePolicy(policy);
43          assertXMLisEqual(serializedPolicy, serializedPolicyReferenceFile);
44  
45          Iterator<List<Assertion>> alternativeIterator = policy.getAlternatives();
46          int count = 0;
47          while (alternativeIterator.hasNext()) {
48              List<Assertion> alternative = alternativeIterator.next();
49              assertEquals(1, alternative.size());
50              assertTrue(alternative.get(0) instanceof SignedElements);
51              SignedElements signedElements = (SignedElements) alternative.get(0);
52              assertFalse(signedElements.isNormalized());
53              assertTrue(signedElements.isIgnorable());
54              assertTrue(signedElements.isOptional());
55              assertEquals(Constants.TYPE_ASSERTION, signedElements.getType());
56              assertEquals(SP12Constants.SIGNED_ELEMENTS, signedElements.getName());
57              assertEquals("1.1", signedElements.getXPathVersion());
58              assertEquals(2, signedElements.getXPaths().size());
59              count++;
60          }
61          assertEquals(1, count);
62  
63          policy = policy.normalize(true);
64          serializedPolicy = serializePolicy(policy);
65          assertXMLisEqual(serializedPolicy, normalizedPolicyReferenceFile);
66  
67          alternativeIterator = policy.getAlternatives();
68          List<Assertion> alternative = alternativeIterator.next();
69          assertEquals(0, alternative.size());
70  
71          List<PolicyComponent> policyComponents = policy.getPolicyComponents();
72          assertEquals(1, policyComponents.size());
73          PolicyOperator policyOperator = (PolicyOperator) policyComponents.get(0);
74          policyComponents = policyOperator.getPolicyComponents();
75          assertEquals(2, policyComponents.size());
76          All all = (All) policyComponents.get(0);
77          List<PolicyComponent> policyComponentsAll = all.getAssertions();
78          assertEquals(0, policyComponentsAll.size());
79  
80          all = (All) policyComponents.get(1);
81          policyComponentsAll = all.getAssertions();
82          assertEquals(1, policyComponentsAll.size());
83  
84          Iterator<PolicyComponent> policyComponentIterator = policyComponentsAll.iterator();
85          SignedElements signedElements = (SignedElements) policyComponentIterator.next();
86          assertTrue(signedElements.isNormalized());
87          assertTrue(signedElements.isIgnorable());
88          assertFalse(signedElements.isOptional());
89          assertEquals(Constants.TYPE_ASSERTION, signedElements.getType());
90          assertEquals(SP12Constants.SIGNED_ELEMENTS, signedElements.getName());
91          assertEquals("1.1", signedElements.getXPathVersion());
92          assertEquals(2, signedElements.getXPaths().size());
93      }
94  
95      @Test
96      public void testSignedElements13() throws Exception {
97          String fileName = "SignedElements.xml";
98          String policyFile = loadPolicyFile("policy/model/sp13/" + fileName);
99          String serializedPolicyRefereneFile = loadPolicyFile("policy/model/sp13/serialized/" + fileName);
100         String normalizedPolicyReferenceFile = loadPolicyFile("policy/model/sp13/normalized/" + fileName);
101         Policy policy = loadPolicy(policyFile);
102         String serializedPolicy = serializePolicy(policy);
103         assertXMLisEqual(serializedPolicy, serializedPolicyRefereneFile);
104 
105         Iterator<List<Assertion>> alternativeIterator = policy.getAlternatives();
106         int count = 0;
107         while (alternativeIterator.hasNext()) {
108             List<Assertion> alternative = alternativeIterator.next();
109             assertEquals(1, alternative.size());
110             assertTrue(alternative.get(0) instanceof SignedElements);
111             SignedElements signedElements = (SignedElements) alternative.get(0);
112             assertFalse(signedElements.isNormalized());
113             assertTrue(signedElements.isIgnorable());
114             assertTrue(signedElements.isOptional());
115             assertEquals(Constants.TYPE_ASSERTION, signedElements.getType());
116             assertEquals(SP12Constants.SIGNED_ELEMENTS, signedElements.getName());
117             assertEquals("1.1", signedElements.getXPathVersion());
118             assertEquals(3, signedElements.getXPaths().size());
119             count++;
120         }
121         assertEquals(1, count);
122 
123         policy = policy.normalize(true);
124         serializedPolicy = serializePolicy(policy);
125         assertXMLisEqual(serializedPolicy, normalizedPolicyReferenceFile);
126 
127         alternativeIterator = policy.getAlternatives();
128         List<Assertion> alternative = alternativeIterator.next();
129         assertEquals(0, alternative.size());
130 
131         List<PolicyComponent> policyComponents = policy.getPolicyComponents();
132         assertEquals(1, policyComponents.size());
133         PolicyOperator policyOperator = (PolicyOperator) policyComponents.get(0);
134         policyComponents = policyOperator.getPolicyComponents();
135         assertEquals(2, policyComponents.size());
136         All all = (All) policyComponents.get(0);
137         List<PolicyComponent> policyComponentsAll = all.getAssertions();
138         assertEquals(0, policyComponentsAll.size());
139 
140         all = (All) policyComponents.get(1);
141         policyComponentsAll = all.getAssertions();
142         assertEquals(1, policyComponentsAll.size());
143 
144         Iterator<PolicyComponent> policyComponentIterator = policyComponentsAll.iterator();
145         SignedElements signedElements = (SignedElements) policyComponentIterator.next();
146         assertTrue(signedElements.isNormalized());
147         assertTrue(signedElements.isIgnorable());
148         assertFalse(signedElements.isOptional());
149         assertEquals(Constants.TYPE_ASSERTION, signedElements.getType());
150         assertEquals(SP12Constants.SIGNED_ELEMENTS, signedElements.getName());
151         assertEquals("1.1", signedElements.getXPathVersion());
152         assertEquals(3, signedElements.getXPaths().size());
153     }
154 }