1+ package com .github .freeclimbapi ;
2+
3+ import org .junit .Assert ;
4+ import org .junit .Ignore ;
5+ import org .junit .Test ;
6+
7+ import java .util .ArrayList ;
8+ import java .util .HashMap ;
9+ import java .util .List ;
10+ import java .util .Map ;
11+ import java .io .File ;
12+ import java .math .BigDecimal ;
13+ import com .github .freeclimbapi .utils .*;
14+
15+ import static org .junit .Assert .assertEquals ;
16+ import static org .junit .Assert .assertThrows ;
17+ import java .security .NoSuchAlgorithmException ;
18+ import java .security .InvalidKeyException ;
19+
20+ public class RequestVerifierTest {
21+
22+ private final RequestVerifier instance = new RequestVerifier ();
23+
24+ @ Test
25+ public void checkRequestBodyTest () throws Exception {
26+ String requestBody = "" ;
27+ String signingSecret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793" ;
28+ String requestHeader = "t=1679931346,v1=4945505e46930b6e31df721c069f10cd3a4cfb3c8e2ec67d2663fae49f95644f,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8" ;
29+ Integer tolerance = 5 * 60 * 1000 ;
30+ RuntimeException exception = assertThrows (
31+ RuntimeException .class ,
32+ () -> {
33+ RequestVerifier .verifyRequestSignature (requestBody , requestHeader , signingSecret , tolerance );
34+ });
35+
36+ assertEquals ("Request Body cannot be empty or null" , exception .getMessage ());
37+
38+ }
39+
40+ @ Test
41+ public void checkRequestHeaderTest () {
42+ String requestBody = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }" ;
43+ String signingSecret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793" ;
44+ String requestHeader = "v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8" ;
45+ Integer tolerance = 5 * 60 * 1000 ;
46+ RuntimeException exception = assertThrows (
47+ RuntimeException .class ,
48+ () -> {
49+ RequestVerifier .verifyRequestSignature (requestBody , requestHeader , signingSecret , tolerance );
50+ });
51+
52+ assertEquals ("Error with request header, timestamp is not present" , exception .getMessage ());
53+ }
54+
55+ @ Test
56+ public void checkRequestHeaderTest2 () {
57+ String requestBody = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }" ;
58+ String signingSecret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793" ;
59+ String requestHeader = "t=1679944186" ;
60+ Integer tolerance = 5 * 60 * 1000 ;
61+ RuntimeException exception = assertThrows (
62+ RuntimeException .class ,
63+ () -> {
64+ RequestVerifier .verifyRequestSignature (requestBody , requestHeader , signingSecret , tolerance );
65+ });
66+
67+ assertEquals ("Error with request header, signatures are not present" , exception .getMessage ());
68+ }
69+
70+ @ Test
71+ public void checkRequestHeaderTest3 () {
72+ String requestBody = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }" ;
73+ String signingSecret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793" ;
74+ String requestHeader = "" ;
75+ int tolerance = 5 * 60 ;
76+ RuntimeException exception = assertThrows (
77+ RuntimeException .class ,
78+ () -> {
79+ RequestVerifier .verifyRequestSignature (requestBody , requestHeader , signingSecret , tolerance );
80+ });
81+
82+ assertEquals ("Error with request header, Request header is empty" , exception .getMessage ());
83+ }
84+
85+ @ Test
86+ public void checkSigningSecretTest () {
87+ String requestBody = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }" ;
88+ String signingSecret = "" ;
89+ String requestHeader = "t=1679944186,v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8" ;
90+ Integer tolerance = 5 * 60 ;
91+ RuntimeException exception = assertThrows (
92+ RuntimeException .class ,
93+ () -> {
94+ RequestVerifier .verifyRequestSignature (requestBody , requestHeader , signingSecret , tolerance );
95+ });
96+
97+ assertEquals ("Signing secret cannot be empty or null" , exception .getMessage ());
98+ }
99+
100+ @ Test
101+ public void checkToleranceTest () {
102+ String requestBody = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }" ;
103+ String signingSecret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793" ;
104+ String requestHeader = "t=1679944186,v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8" ;
105+ Integer tolerance = Integer .MAX_VALUE ;
106+ RuntimeException exception = assertThrows (
107+ RuntimeException .class ,
108+ () -> {
109+ RequestVerifier .verifyRequestSignature (requestBody , requestHeader , signingSecret , tolerance );
110+ });
111+
112+ assertEquals ("Tolerance value must be a positive integer" , exception .getMessage ());
113+ }
114+
115+ @ Test
116+ public void checkToleranceTest2 () {
117+ String requestBody = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }" ;
118+ String signingSecret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793" ;
119+ String requestHeader = "t=1679944186,v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8" ;
120+ Integer tolerance = -5 ;
121+ RuntimeException exception = assertThrows (
122+ RuntimeException .class ,
123+ () -> {
124+ RequestVerifier .verifyRequestSignature (requestBody , requestHeader , signingSecret , tolerance );
125+ });
126+
127+ assertEquals ("Tolerance value must be a positive integer" , exception .getMessage ());
128+ }
129+
130+ @ Test
131+ public void checkToleranceTest3 () {
132+ String requestBody = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }" ;
133+ String signingSecret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793" ;
134+ String requestHeader = "t=1679944186,v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8" ;
135+ int tolerance = 0 ;
136+ RuntimeException exception = assertThrows (
137+ RuntimeException .class ,
138+ () -> {
139+ RequestVerifier .verifyRequestSignature (requestBody , requestHeader , signingSecret , tolerance );
140+ });
141+
142+ assertEquals ("Tolerance value must be a positive integer" , exception .getMessage ());
143+ }
144+
145+ @ Test
146+ public void verifyToleranceTest () {
147+ String requestBody = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }" ;
148+ String signingSecret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793" ;
149+ Integer currentTime = (int ) (System .currentTimeMillis () / 1000L );
150+ Integer timeCalcuation = currentTime - (6 * 60 );
151+ String requestHeader = "t=" + timeCalcuation .toString ()
152+ + ",v1=1d798c86e977ff734dec3a8b8d67fe8621dcc1df46ef4212e0bfe2e122b01bfd,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8" ;
153+ Integer tolerance = 5 * 60 ;
154+
155+ RuntimeException exception = assertThrows (
156+ RuntimeException .class ,
157+ () -> {
158+ RequestVerifier .verifyRequestSignature (requestBody , requestHeader , signingSecret , tolerance );
159+ });
160+
161+ assertEquals (
162+ "Request time exceeded tolerance threshold. Request: " + timeCalcuation .toString () + ", CurrentTime: "
163+ + Integer .toString (currentTime ) + ", tolerance: " + tolerance ,
164+ exception .getMessage ());
165+ }
166+
167+ @ Test
168+ public void verifySignatureTest () {
169+ String requestBody = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }" ;
170+ String signingSecret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7794" ;
171+ Integer currentTime = (int ) (System .currentTimeMillis () / 1000L );
172+ String requestHeader = "t=" + currentTime .toString ()
173+ + ",v1=1d798c86e977ff734dec3a8b8d67fe8621dcc1df46ef4212e0bfe2e122b01bfd,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8" ;
174+ Integer tolerance = 5 * 60 ;
175+ RuntimeException exception = assertThrows (
176+ RuntimeException .class ,
177+ () -> {
178+ RequestVerifier .verifyRequestSignature (requestBody , requestHeader , signingSecret , tolerance );
179+ });
180+
181+ assertEquals (
182+ "Unverified signature request, If this request was unexpected, it may be from a bad actor. Please proceed with caution. If the request was exepected, please check any typos or issues with the signingSecret" ,
183+ exception .getMessage ());
184+ }
185+
186+ @ Test
187+ public void verifyRequestSignatureTest () throws NoSuchAlgorithmException , InvalidKeyException {
188+ String requestBody = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }" ;
189+ String signingSecret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793" ;
190+ String requestHeader = "t=2130000000,v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=6835006e70c9b9f610e8fb3a8b36b52b3f28c12d0a2dab75091c46ca7ec11b20" ;
191+ Integer tolerance = 5 * 60 ;
192+ RequestVerifier .verifyRequestSignature (requestBody , requestHeader , signingSecret , tolerance );
193+ }
194+ }
0 commit comments