Skip to content

Commit b61d4ee

Browse files
authored
Fix NPE in createSyncMDN when IncomingMIC is null (#167)
1 parent 30302cc commit b61d4ee

2 files changed

Lines changed: 87 additions & 2 deletions

File tree

phase2-lib/src/main/java/com/helger/phase2/util/AS2Helper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ public static MIC createMICOnReception (@NonNull final AS2Message aMsg) throws E
298298
* @param aMsg
299299
* The source AS2 message for which the MDN is to be created. May not be <code>null</code>.
300300
* @param aIncomingMIC
301-
* The MIC of the incoming message. May not be <code>null</code>.
301+
* The MIC of the incoming message. May be <code>null</code> for unsigned messages or when
302+
* an error occurs before MIC computation.
302303
* @param aDisposition
303304
* The disposition - either success or error. May not be <code>null</code>.
304305
* @param sText
@@ -316,7 +317,6 @@ public static IMessageMDN createSyncMDN (@NonNull final IAS2Session aSession,
316317
{
317318
ValueEnforcer.notNull (aSession, "AS2Session");
318319
ValueEnforcer.notNull (aMsg, "AS2Message");
319-
ValueEnforcer.notNull (aIncomingMIC, "IncomingMIC");
320320
ValueEnforcer.notNull (aDisposition, "Disposition");
321321
ValueEnforcer.notNull (sText, "Text");
322322

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* The FreeBSD Copyright
3+
* Copyright 1994-2008 The FreeBSD Project. All rights reserved.
4+
* Copyright (C) 2013-2026 Philip Helger philip[at]helger[dot]com
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are
8+
* met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other copies of the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY
18+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR
21+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*
29+
* The views and conclusions contained in the software and documentation
30+
* are those of the authors and should not be interpreted as representing
31+
* official policies, either expressed or implied, of the FreeBSD Project.
32+
*/
33+
package com.helger.phase2.util;
34+
35+
import static org.junit.Assert.assertNotNull;
36+
import static org.junit.Assert.assertNull;
37+
38+
import org.junit.Test;
39+
40+
import com.helger.http.CHttpHeader;
41+
import com.helger.phase2.disposition.DispositionType;
42+
import com.helger.phase2.message.AS2Message;
43+
import com.helger.phase2.message.AS2MessageMDN;
44+
import com.helger.phase2.message.IMessageMDN;
45+
import com.helger.phase2.partner.Partnership;
46+
import com.helger.phase2.partner.SelfFillingPartnershipFactory;
47+
import com.helger.phase2.session.AS2Session;
48+
49+
/**
50+
* Test class for class {@link AS2Helper}
51+
*/
52+
public final class AS2HelperTest
53+
{
54+
/**
55+
* Regression test: createSyncMDN must not throw when aIncomingMIC is null.
56+
* MIC may legitimately be null for unsigned messages or when an error occurs
57+
* before MIC computation (AS2DispositionException path).
58+
*/
59+
@Test
60+
public void testCreateSyncMDNWithNullMIC () throws Exception
61+
{
62+
final AS2Session aSession = new AS2Session ();
63+
aSession.setPartnershipFactory (new SelfFillingPartnershipFactory ());
64+
65+
final Partnership aPartnership = new Partnership ("test");
66+
aPartnership.setSenderAS2ID ("sender");
67+
aPartnership.setReceiverAS2ID ("receiver");
68+
69+
final AS2Message aMsg = new AS2Message ();
70+
aMsg.setPartnership (aPartnership);
71+
aMsg.headers ().setHeader (CHttpHeader.AS2_TO, "sender");
72+
aMsg.headers ().setHeader (CHttpHeader.MESSAGE_ID, "<test@example>");
73+
74+
// Must not throw NPE
75+
final IMessageMDN aMDN = AS2Helper.createSyncMDN (aSession,
76+
aMsg,
77+
null,
78+
DispositionType.createSuccess (),
79+
"Test MDN");
80+
assertNotNull (aMDN);
81+
// No MIC algorithm configured and no Disposition-Notification-Options header
82+
// -> MDNA_MIC attribute must be absent
83+
assertNull (aMDN.attrs ().getAsString (AS2MessageMDN.MDNA_MIC));
84+
}
85+
}

0 commit comments

Comments
 (0)