1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.services.sns;
17 
18 import static org.junit.jupiter.api.Assertions.assertTrue;
19 
20 import java.io.IOException;
21 import java.net.URISyntaxException;
22 import java.security.PublicKey;
23 import java.security.cert.CertificateException;
24 import java.security.cert.CertificateFactory;
25 import java.security.cert.X509Certificate;
26 import org.junit.jupiter.api.Test;
27 import software.amazon.awssdk.testutils.service.AwsTestBase;
28 
29 public class SnsSignatureCheckerTest extends AwsTestBase {
30 
31     @Test
validateMessageTest()32     public void validateMessageTest() throws Exception {
33         final String jsonMessage = getResourceAsString(getClass(), SnsTestResources.SAMPLE_MESSAGE);
34         SignatureChecker checker = new SignatureChecker();
35         assertTrue(checker.verifyMessageSignature(jsonMessage, getPublicKey()));
36     }
37 
getPublicKey()38     private PublicKey getPublicKey() throws URISyntaxException, IOException, CertificateException {
39         CertificateFactory cf = CertificateFactory.getInstance("X.509");
40         X509Certificate cert = (X509Certificate) cf
41                 .generateCertificate(getClass().getResourceAsStream(SnsTestResources.FIXED_PUBLIC_CERT));
42         return cert.getPublicKey();
43     }
44 }
45