1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
//! Core Veracode API client implementation.
//!
//! This module contains the foundational client for making authenticated requests
//! to the Veracode API, including HMAC authentication and HTTP request handling.
use bytes::Bytes;
use hex;
use hmac::{Hmac, Mac};
use log::{info, warn};
use reqwest::{Body, Client, multipart};
use secrecy::ExposeSecret;
use serde::Serialize;
use sha2::Sha256;
use std::borrow::Cow;
use std::collections::HashMap;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use tokio::fs::File as TokioFile;
use url::Url;
use crate::json_validator::{MAX_JSON_DEPTH, validate_json_depth};
use crate::{VeracodeConfig, VeracodeError};
// Type aliases for HMAC
type HmacSha256 = Hmac<Sha256>;
// Constants for authentication error messages to avoid repeated allocations
const INVALID_URL_MSG: &str = "Invalid URL";
const INVALID_API_KEY_MSG: &str = "Invalid API key format - must be hex string";
const INVALID_NONCE_MSG: &str = "Invalid nonce format";
const HMAC_CREATION_FAILED_MSG: &str = "Failed to create HMAC";
/// Core Veracode API client.
///
/// This struct provides the foundational HTTP client with HMAC authentication
/// for making requests to any Veracode API endpoint.
#[derive(Clone)]
pub struct VeracodeClient {
config: VeracodeConfig,
client: Client,
}
impl VeracodeClient {
/// Build URL with query parameters - centralized helper
fn build_url_with_params(&self, endpoint: &str, query_params: &[(&str, &str)]) -> String {
// Pre-allocate string capacity for better performance
let estimated_capacity = self
.config
.base_url
.len()
.saturating_add(endpoint.len())
.saturating_add(query_params.len().saturating_mul(32)); // Rough estimate for query params
let mut url = String::with_capacity(estimated_capacity);
url.push_str(&self.config.base_url);
url.push_str(endpoint);
if !query_params.is_empty() {
url.push('?');
for (i, (key, value)) in query_params.iter().enumerate() {
if i > 0 {
url.push('&');
}
url.push_str(&urlencoding::encode(key));
url.push('=');
url.push_str(&urlencoding::encode(value));
}
}
url
}
/// Create a new Veracode API client.
///
/// # Arguments
///
/// * `config` - Configuration containing API credentials and settings
///
/// # Returns
///
/// A new `VeracodeClient` instance ready to make API calls.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub fn new(config: VeracodeConfig) -> Result<Self, VeracodeError> {
let mut client_builder = Client::builder();
// Use the certificate validation setting from config
if !config.validate_certificates {
client_builder = client_builder
.danger_accept_invalid_certs(true)
.danger_accept_invalid_hostnames(true);
}
// Configure HTTP timeouts from config
client_builder = client_builder
.connect_timeout(Duration::from_secs(config.connect_timeout))
.timeout(Duration::from_secs(config.request_timeout));
// Configure proxy if specified
if let Some(proxy_url) = &config.proxy_url {
let mut proxy = reqwest::Proxy::all(proxy_url)
.map_err(|e| VeracodeError::InvalidConfig(format!("Invalid proxy URL: {e}")))?;
// Add basic authentication if credentials are provided
if let (Some(username), Some(password)) =
(&config.proxy_username, &config.proxy_password)
{
proxy = proxy.basic_auth(username.expose_secret(), password.expose_secret());
}
client_builder = client_builder.proxy(proxy);
}
let client = client_builder.build().map_err(VeracodeError::Http)?;
Ok(Self { config, client })
}
/// Create an XML API variant of this client, reusing the same underlying HTTP connection pool.
///
/// Rather than building a new `reqwest::Client` (which reloads system CA certificates and
/// discards pooled connections), this clones the existing client — `reqwest::Client` is
/// internally `Arc`-backed, so the clone is cheap and shares the connection pool.
#[must_use]
pub fn new_xml_variant(&self) -> Self {
let mut xml_config = self.config.clone();
xml_config.base_url = xml_config.xml_base_url.clone();
Self {
config: xml_config,
client: self.client.clone(),
}
}
/// Get the base URL for API requests.
#[must_use]
pub fn base_url(&self) -> &str {
&self.config.base_url
}
/// Get access to the configuration
#[must_use]
pub fn config(&self) -> &VeracodeConfig {
&self.config
}
/// Get access to the underlying reqwest client
#[must_use]
pub fn client(&self) -> &Client {
&self.client
}
/// Execute an HTTP request with retry logic and exponential backoff.
///
/// This method implements the retry strategy defined in the client's configuration.
/// It will retry requests that fail due to transient errors (network issues,
/// server errors, rate limiting) using exponential backoff. For rate limiting (429),
/// it uses intelligent delays based on Veracode's minute-window rate limits.
///
/// # Arguments
///
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
/// * `request_builder` - A closure that creates the `reqwest::RequestBuilder`
/// * `operation_name` - A human-readable name for logging/error messages
///
/// # Returns
///
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
/// A `Result` containing the HTTP response or a `VeracodeError`.
async fn execute_with_retry<F>(
&self,
request_builder: F,
operation_name: Cow<'_, str>,
) -> Result<reqwest::Response, VeracodeError>
where
F: Fn() -> reqwest::RequestBuilder,
{
let retry_config = &self.config.retry_config;
let start_time = Instant::now();
let mut total_delay = std::time::Duration::from_millis(0);
// If retries are disabled, make a single attempt
if retry_config.max_attempts == 0 {
return match request_builder().send().await {
Ok(response) => Ok(response),
Err(e) => Err(VeracodeError::Http(e)),
};
}
let mut last_error = None;
let mut rate_limit_attempts: u32 = 0;
for attempt in 1..=retry_config.max_attempts.saturating_add(1) {
// Build and send the request
match request_builder().send().await {
Ok(response) => {
// Check for rate limiting before treating as success
if response.status().as_u16() == 429 {
// Extract Retry-After header if present
let retry_after_seconds = response
.headers()
.get("retry-after")
.and_then(|h| h.to_str().ok())
.and_then(|s| s.parse::<u64>().ok());
let message = "HTTP 429: Rate limit exceeded".to_string();
let veracode_error = VeracodeError::RateLimited {
retry_after_seconds,
message,
};
// Increment rate limit attempt counter
rate_limit_attempts = rate_limit_attempts.saturating_add(1);
// Check if we should retry based on rate limit specific limits
if attempt > retry_config.max_attempts
|| rate_limit_attempts > retry_config.rate_limit_max_attempts
{
last_error = Some(veracode_error);
break;
}
// Calculate rate limit specific delay
let delay = retry_config.calculate_rate_limit_delay(retry_after_seconds);
total_delay = total_delay.saturating_add(delay);
// Check total delay limit
if total_delay.as_millis() > retry_config.max_total_delay_ms as u128 {
let msg = format!(
"{} exceeded maximum total retry time of {}ms after {} attempts",
operation_name, retry_config.max_total_delay_ms, attempt
);
last_error = Some(VeracodeError::RetryExhausted(msg));
break;
}
// Log rate limit with specific formatting
let wait_time = match retry_after_seconds {
Some(seconds) => format!("{seconds}s (from Retry-After header)"),
None => format!("{}s (until next minute window)", delay.as_secs()),
};
warn!(
"🚦 {operation_name} rate limited on attempt {attempt}, waiting {wait_time}"
);
// Wait and continue to next attempt
tokio::time::sleep(delay).await;
last_error = Some(veracode_error);
continue;
}
if attempt > 1 {
// Log successful retry for debugging
info!("✅ {operation_name} succeeded on attempt {attempt}");
}
return Ok(response);
}
Err(e) => {
// For connection errors, network issues, etc., use normal retry logic
let veracode_error = VeracodeError::Http(e);
// Check if this is the last attempt or if the error is not retryable
if attempt > retry_config.max_attempts
|| !retry_config.is_retryable_error(&veracode_error)
{
last_error = Some(veracode_error);
break;
}
// Use normal exponential backoff for non-429 errors
let delay = retry_config.calculate_delay(attempt);
total_delay = total_delay.saturating_add(delay);
// Check if we've exceeded the maximum total delay
if total_delay.as_millis() > retry_config.max_total_delay_ms as u128 {
// Format error message once
let msg = format!(
"{} exceeded maximum total retry time of {}ms after {} attempts",
operation_name, retry_config.max_total_delay_ms, attempt
);
last_error = Some(VeracodeError::RetryExhausted(msg));
break;
}
// Log retry attempt for debugging
warn!(
"⚠️ {operation_name} failed on attempt {attempt}, retrying in {}ms: {veracode_error}",
delay.as_millis()
);
// Wait before next attempt
tokio::time::sleep(delay).await;
last_error = Some(veracode_error);
}
}
}
// All attempts failed - create error message efficiently
match last_error {
Some(error) => {
let elapsed = start_time.elapsed();
match error {
VeracodeError::RetryExhausted(_) => Err(error),
VeracodeError::Http(_)
| VeracodeError::Serialization(_)
| VeracodeError::Authentication(_)
| VeracodeError::InvalidResponse(_)
| VeracodeError::HttpStatus { .. }
| VeracodeError::InvalidConfig(_)
| VeracodeError::NotFound(_)
| VeracodeError::RateLimited { .. }
| VeracodeError::Validation(_) => {
let msg = format!(
"{} failed after {} attempts over {}ms: {}",
operation_name,
retry_config.max_attempts.saturating_add(1),
elapsed.as_millis(),
error
);
Err(VeracodeError::RetryExhausted(msg))
}
}
}
None => {
let msg = format!(
"{} failed after {} attempts with unknown error",
operation_name,
retry_config.max_attempts.saturating_add(1)
);
Err(VeracodeError::RetryExhausted(msg))
}
}
}
/// Generate HMAC signature for authentication based on official Veracode JavaScript implementation
fn generate_hmac_signature(
&self,
method: &str,
url: &str,
timestamp: u64,
nonce: &str,
) -> Result<String, VeracodeError> {
let url_parsed = Url::parse(url)
.map_err(|_| VeracodeError::Authentication(INVALID_URL_MSG.to_string()))?;
let path_and_query = match url_parsed.query() {
Some(query) => format!("{}?{}", url_parsed.path(), query),
None => url_parsed.path().to_string(),
};
let host = url_parsed.host_str().unwrap_or("");
// Based on the official Veracode JavaScript implementation:
// var data = `id=${id}&host=${host}&url=${url}&method=${method}`;
let data = format!(
"id={}&host={}&url={}&method={}",
self.config.credentials.expose_api_id(),
host,
path_and_query,
method
);
let timestamp_str = timestamp.to_string();
let ver_str = "vcode_request_version_1";
// Convert hex strings to bytes
let key_bytes = hex::decode(self.config.credentials.expose_api_key())
.map_err(|_| VeracodeError::Authentication(INVALID_API_KEY_MSG.to_string()))?;
let nonce_bytes = hex::decode(nonce)
.map_err(|_| VeracodeError::Authentication(INVALID_NONCE_MSG.to_string()))?;
// Step 1: HMAC(nonce, key)
let mut mac1 = HmacSha256::new_from_slice(&key_bytes)
.map_err(|_| VeracodeError::Authentication(HMAC_CREATION_FAILED_MSG.to_string()))?;
mac1.update(&nonce_bytes);
let hashed_nonce = mac1.finalize().into_bytes();
// Step 2: HMAC(timestamp, hashed_nonce)
let mut mac2 = HmacSha256::new_from_slice(&hashed_nonce)
.map_err(|_| VeracodeError::Authentication(HMAC_CREATION_FAILED_MSG.to_string()))?;
mac2.update(timestamp_str.as_bytes());
let hashed_timestamp = mac2.finalize().into_bytes();
// Step 3: HMAC(ver_str, hashed_timestamp)
let mut mac3 = HmacSha256::new_from_slice(&hashed_timestamp)
.map_err(|_| VeracodeError::Authentication(HMAC_CREATION_FAILED_MSG.to_string()))?;
mac3.update(ver_str.as_bytes());
let hashed_ver_str = mac3.finalize().into_bytes();
// Step 4: HMAC(data, hashed_ver_str)
let mut mac4 = HmacSha256::new_from_slice(&hashed_ver_str)
.map_err(|_| VeracodeError::Authentication(HMAC_CREATION_FAILED_MSG.to_string()))?;
mac4.update(data.as_bytes());
let signature = mac4.finalize().into_bytes();
// Return the hex-encoded signature (lowercase)
Ok(hex::encode(signature).to_lowercase())
}
/// Generate authorization header for HMAC authentication
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub fn generate_auth_header(&self, method: &str, url: &str) -> Result<String, VeracodeError> {
#[allow(clippy::cast_possible_truncation)]
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|e| VeracodeError::Authentication(format!("System time error: {e}")))?
.as_millis() as u64; // Use milliseconds like JavaScript
// Generate a 16-byte random nonce and convert to hex string
let nonce_bytes: [u8; 16] = rand::random();
let nonce = hex::encode(nonce_bytes);
let signature = self.generate_hmac_signature(method, url, timestamp, &nonce)?;
Ok(format!(
"VERACODE-HMAC-SHA-256 id={},ts={},nonce={},sig={}",
self.config.credentials.expose_api_id(),
timestamp,
nonce,
signature
))
}
/// Make a GET request to the specified endpoint.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint path (e.g., "/appsec/v1/applications")
/// * `query_params` - Optional query parameters as key-value pairs
///
/// # Returns
///
/// A `Result` containing the HTTP response.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn get(
&self,
endpoint: &str,
query_params: Option<&[(String, String)]>,
) -> Result<reqwest::Response, VeracodeError> {
// Pre-allocate URL capacity
let param_count = query_params.map_or(0, |p| p.len());
let estimated_capacity = self
.config
.base_url
.len()
.saturating_add(endpoint.len())
.saturating_add(param_count.saturating_mul(32));
let mut url = String::with_capacity(estimated_capacity);
url.push_str(&self.config.base_url);
url.push_str(endpoint);
if let Some(params) = query_params
&& !params.is_empty()
{
url.push('?');
for (i, (key, value)) in params.iter().enumerate() {
if i > 0 {
url.push('&');
}
url.push_str(key);
url.push('=');
url.push_str(value);
}
}
// Create request builder closure for retry logic
let request_builder = || {
// Re-generate auth header for each attempt to avoid signature expiry
let Ok(auth_header) = self.generate_auth_header("GET", &url) else {
return self.client.get("invalid://url");
};
self.client
.get(&url)
.header("Authorization", auth_header)
.header("Content-Type", "application/json")
};
// Use Cow::Borrowed for simple operations when possible
let operation_name = if endpoint.len() < 50 {
Cow::Owned(format!("GET {endpoint}"))
} else {
Cow::Borrowed("GET [long endpoint]")
};
self.execute_with_retry(request_builder, operation_name)
.await
}
/// Make a POST request to the specified endpoint.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint path (e.g., "/appsec/v1/applications")
/// * `body` - Optional request body that implements Serialize
///
/// # Returns
///
/// A `Result` containing the HTTP response.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn post<T: Serialize>(
&self,
endpoint: &str,
body: Option<&T>,
) -> Result<reqwest::Response, VeracodeError> {
let mut url =
String::with_capacity(self.config.base_url.len().saturating_add(endpoint.len()));
url.push_str(&self.config.base_url);
url.push_str(endpoint);
// Serialize body once outside the retry loop for efficiency
let serialized_body = if let Some(body) = body {
Some(serde_json::to_string(body)?)
} else {
None
};
// Create request builder closure for retry logic
let request_builder = || {
// Re-generate auth header for each attempt to avoid signature expiry
let Ok(auth_header) = self.generate_auth_header("POST", &url) else {
return self.client.post("invalid://url");
};
let mut request = self
.client
.post(&url)
.header("Authorization", auth_header)
.header("Content-Type", "application/json");
if let Some(ref body_str) = serialized_body {
request = request.body(body_str.clone());
}
request
};
let operation_name = if endpoint.len() < 50 {
Cow::Owned(format!("POST {endpoint}"))
} else {
Cow::Borrowed("POST [long endpoint]")
};
self.execute_with_retry(request_builder, operation_name)
.await
}
/// Make a PUT request to the specified endpoint.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint path (e.g., "/appsec/v1/applications/guid")
/// * `body` - Optional request body that implements Serialize
///
/// # Returns
///
/// A `Result` containing the HTTP response.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn put<T: Serialize>(
&self,
endpoint: &str,
body: Option<&T>,
) -> Result<reqwest::Response, VeracodeError> {
let mut url =
String::with_capacity(self.config.base_url.len().saturating_add(endpoint.len()));
url.push_str(&self.config.base_url);
url.push_str(endpoint);
// Serialize body once outside the retry loop for efficiency
let serialized_body = if let Some(body) = body {
Some(serde_json::to_string(body)?)
} else {
None
};
// Create request builder closure for retry logic
let request_builder = || {
// Re-generate auth header for each attempt to avoid signature expiry
let Ok(auth_header) = self.generate_auth_header("PUT", &url) else {
return self.client.put("invalid://url");
};
let mut request = self
.client
.put(&url)
.header("Authorization", auth_header)
.header("Content-Type", "application/json");
if let Some(ref body_str) = serialized_body {
request = request.body(body_str.clone());
}
request
};
let operation_name = if endpoint.len() < 50 {
Cow::Owned(format!("PUT {endpoint}"))
} else {
Cow::Borrowed("PUT [long endpoint]")
};
self.execute_with_retry(request_builder, operation_name)
.await
}
/// Make a DELETE request to the specified endpoint.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint path (e.g., "/appsec/v1/applications/guid")
///
/// # Returns
///
/// A `Result` containing the HTTP response.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn delete(&self, endpoint: &str) -> Result<reqwest::Response, VeracodeError> {
let mut url =
String::with_capacity(self.config.base_url.len().saturating_add(endpoint.len()));
url.push_str(&self.config.base_url);
url.push_str(endpoint);
// Create request builder closure for retry logic
let request_builder = || {
// Re-generate auth header for each attempt to avoid signature expiry
let Ok(auth_header) = self.generate_auth_header("DELETE", &url) else {
return self.client.delete("invalid://url");
};
self.client
.delete(&url)
.header("Authorization", auth_header)
.header("Content-Type", "application/json")
};
let operation_name = if endpoint.len() < 50 {
Cow::Owned(format!("DELETE {endpoint}"))
} else {
Cow::Borrowed("DELETE [long endpoint]")
};
self.execute_with_retry(request_builder, operation_name)
.await
}
/// Helper method to handle common response processing.
///
/// Checks if the response is successful and returns an error if not.
///
/// # Arguments
///
/// * `response` - The HTTP response to check
/// * `context` - A description of the operation being performed (e.g., "get application")
///
/// # Returns
///
/// A `Result` containing the response if successful, or an error if not.
///
/// # Error Context
///
/// This method enhances error messages with context about the failed operation
/// to improve debugging and user experience.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn handle_response(
response: reqwest::Response,
context: &str,
) -> Result<reqwest::Response, VeracodeError> {
if !response.status().is_success() {
let status = response.status();
let status_code = status.as_u16();
let url = response.url().to_string();
let error_text = response.text().await?;
// Use structured HttpStatus error for better error handling
return Err(VeracodeError::HttpStatus {
status_code,
url,
message: format!("Failed to {context}: {error_text}"),
});
}
Ok(response)
}
/// Make a GET request with full URL construction and query parameter handling.
///
/// This is a higher-level method that builds the full URL and handles query parameters.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint path
/// * `query_params` - Optional query parameters
///
/// # Returns
///
/// A `Result` containing the HTTP response, pre-processed for success/failure.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn get_with_query(
&self,
endpoint: &str,
query_params: Option<Vec<(String, String)>>,
) -> Result<reqwest::Response, VeracodeError> {
let query_slice = query_params.as_deref();
let response = self.get(endpoint, query_slice).await?;
Self::handle_response(response, &format!("GET {endpoint}")).await
}
/// Make a POST request with automatic response handling.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint path
/// * `body` - Optional request body
///
/// # Returns
///
/// A `Result` containing the HTTP response, pre-processed for success/failure.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn post_with_response<T: Serialize>(
&self,
endpoint: &str,
body: Option<&T>,
) -> Result<reqwest::Response, VeracodeError> {
let response = self.post(endpoint, body).await?;
Self::handle_response(response, &format!("POST {endpoint}")).await
}
/// Make a PUT request with automatic response handling.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint path
/// * `body` - Optional request body
///
/// # Returns
///
/// A `Result` containing the HTTP response, pre-processed for success/failure.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn put_with_response<T: Serialize>(
&self,
endpoint: &str,
body: Option<&T>,
) -> Result<reqwest::Response, VeracodeError> {
let response = self.put(endpoint, body).await?;
Self::handle_response(response, &format!("PUT {endpoint}")).await
}
/// Make a DELETE request with automatic response handling.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint path
///
/// # Returns
///
/// A `Result` containing the HTTP response, pre-processed for success/failure.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn delete_with_response(
&self,
endpoint: &str,
) -> Result<reqwest::Response, VeracodeError> {
let response = self.delete(endpoint).await?;
Self::handle_response(response, &format!("DELETE {endpoint}")).await
}
/// Make paginated GET requests to collect all results.
///
/// This method automatically handles pagination by making multiple requests
/// and combining all results into a single response.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint path
/// * `base_query_params` - Base query parameters (non-pagination)
/// * `page_size` - Number of items per page (default: 500)
///
/// # Returns
///
/// A `Result` containing all paginated results as a single response body string.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn get_paginated(
&self,
endpoint: &str,
base_query_params: Option<Vec<(String, String)>>,
page_size: Option<u32>,
) -> Result<String, VeracodeError> {
let size = page_size.unwrap_or(500);
let mut page: u32 = 0;
let mut all_items = Vec::new();
let mut page_info = None;
loop {
let mut query_params = base_query_params.clone().unwrap_or_default();
query_params.push(("page".to_string(), page.to_string()));
query_params.push(("size".to_string(), size.to_string()));
let response = self.get_with_query(endpoint, Some(query_params)).await?;
let response_text = response.text().await?;
// Validate JSON depth before parsing to prevent DoS attacks
validate_json_depth(&response_text, MAX_JSON_DEPTH).map_err(|e| {
VeracodeError::InvalidResponse(format!("JSON validation failed: {}", e))
})?;
// Try to parse as JSON to extract items and pagination info
if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(&response_text) {
// Handle embedded response format
if let Some(embedded) = json_value.get("_embedded") {
if let Some(items_array) =
embedded.as_object().and_then(|obj| obj.values().next())
&& let Some(items) = items_array.as_array()
{
if items.is_empty() {
break; // No more items
}
all_items.extend(items.clone());
}
} else if let Some(items) = json_value.as_array() {
// Handle direct array response
if items.is_empty() {
break;
}
all_items.extend(items.clone());
} else {
// Single page response, return as-is
return Ok(response_text);
}
// Check pagination info
if let Some(page_obj) = json_value.get("page") {
page_info = Some(page_obj.clone());
if let (Some(current), Some(total)) = (
page_obj.get("number").and_then(|n| n.as_u64()),
page_obj.get("totalPages").and_then(|n| n.as_u64()),
) && current.saturating_add(1) >= total
{
break; // Last page reached
}
}
} else {
// Not JSON or parsing failed, return single response
return Ok(response_text);
}
page = page.saturating_add(1);
// Safety check to prevent infinite loops
if page > 100 {
break;
}
}
// Reconstruct response with all items
let combined_response = if let Some(page_info) = page_info {
// Use embedded format
serde_json::json!({
"_embedded": {
"roles": all_items // This key might need to be dynamic
},
"page": page_info
})
} else {
// Use direct array format
serde_json::Value::Array(all_items)
};
Ok(combined_response.to_string())
}
/// Make a GET request with query parameters
///
/// # Arguments
///
/// * `endpoint` - The API endpoint to call
/// * `params` - Query parameters as a slice of tuples
///
/// # Returns
///
/// A `Result` containing the response or an error.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn get_with_params(
&self,
endpoint: &str,
params: &[(&str, &str)],
) -> Result<reqwest::Response, VeracodeError> {
let mut url =
String::with_capacity(self.config.base_url.len().saturating_add(endpoint.len()));
url.push_str(&self.config.base_url);
url.push_str(endpoint);
let mut request_url =
Url::parse(&url).map_err(|e| VeracodeError::InvalidConfig(e.to_string()))?;
// Add query parameters
if !params.is_empty() {
let mut query_pairs = request_url.query_pairs_mut();
for (key, value) in params {
query_pairs.append_pair(key, value);
}
}
let auth_header = self.generate_auth_header("GET", request_url.as_str())?;
let response = self
.client
.get(request_url)
.header("Authorization", auth_header)
.header("User-Agent", "Veracode Rust Client")
.send()
.await?;
Ok(response)
}
/// Make a POST request with form data
///
/// # Arguments
///
/// * `endpoint` - The API endpoint to call
/// * `params` - Form parameters as a slice of tuples
///
/// # Returns
///
/// A `Result` containing the response or an error.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn post_form(
&self,
endpoint: &str,
params: &[(&str, &str)],
) -> Result<reqwest::Response, VeracodeError> {
let mut url =
String::with_capacity(self.config.base_url.len().saturating_add(endpoint.len()));
url.push_str(&self.config.base_url);
url.push_str(endpoint);
// Build form data - avoid unnecessary allocations
let form_data: Vec<(&str, &str)> = params.to_vec();
let auth_header = self.generate_auth_header("POST", &url)?;
let response = self
.client
.post(&url)
.header("Authorization", auth_header)
.header("User-Agent", "Veracode Rust Client")
.form(&form_data)
.send()
.await?;
Ok(response)
}
/// Upload a file using multipart form data
///
/// # Arguments
///
/// * `endpoint` - The API endpoint to call
/// * `params` - Additional form parameters
/// * `file_field_name` - Name of the file field
/// * `filename` - Name of the file
/// * `file_data` - File data as bytes
///
/// # Returns
///
/// A `Result` containing the response or an error.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn upload_file_multipart(
&self,
endpoint: &str,
params: HashMap<&str, &str>,
file_field_name: &str,
filename: &str,
file_data: Vec<u8>,
) -> Result<reqwest::Response, VeracodeError> {
let mut url =
String::with_capacity(self.config.base_url.len().saturating_add(endpoint.len()));
url.push_str(&self.config.base_url);
url.push_str(endpoint);
// Build multipart form
let mut form = multipart::Form::new();
// Add regular form fields
for (key, value) in params {
form = form.text(key.to_string(), value.to_string());
}
// Add file
let part = multipart::Part::bytes(file_data)
.file_name(filename.to_string())
.mime_str("application/octet-stream")
.map_err(|e| VeracodeError::InvalidConfig(e.to_string()))?;
form = form.part(file_field_name.to_string(), part);
let auth_header = self.generate_auth_header("POST", &url)?;
let response = self
.client
.post(&url)
.header("Authorization", auth_header)
.header("User-Agent", "Veracode Rust Client")
.multipart(form)
.send()
.await?;
Ok(response)
}
/// Upload a file using multipart form data with PUT method (for pipeline scans)
///
/// # Arguments
///
/// * `url` - The full URL to upload to
/// * `file_field_name` - Name of the file field
/// * `filename` - Name of the file
/// * `file_data` - File data as bytes
/// * `additional_headers` - Additional headers to include
///
/// # Returns
///
/// A `Result` containing the response or an error.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn upload_file_multipart_put(
&self,
url: &str,
file_field_name: &str,
filename: &str,
file_data: Vec<u8>,
additional_headers: Option<HashMap<&str, &str>>,
) -> Result<reqwest::Response, VeracodeError> {
// Build multipart form
let part = multipart::Part::bytes(file_data)
.file_name(filename.to_string())
.mime_str("application/octet-stream")
.map_err(|e| VeracodeError::InvalidConfig(e.to_string()))?;
let form = multipart::Form::new().part(file_field_name.to_string(), part);
let auth_header = self.generate_auth_header("PUT", url)?;
let mut request = self
.client
.put(url)
.header("Authorization", auth_header)
.header("User-Agent", "Veracode Rust Client")
.multipart(form);
// Add any additional headers
if let Some(headers) = additional_headers {
for (key, value) in headers {
request = request.header(key, value);
}
}
let response = request.send().await?;
Ok(response)
}
/// Upload a file with query parameters (like Java implementation)
///
/// This method mimics the Java API wrapper's approach where parameters
/// are added to the query string and the file is uploaded separately.
///
/// Memory optimization: Uses Cow for strings and Arc for file data to minimize cloning
/// during retry attempts. Automatically retries on transient failures.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint to call
/// * `query_params` - Query parameters as key-value pairs
/// * `file_field_name` - Name of the file field
/// * `filename` - Name of the file
/// * `file_data` - File data as bytes
///
/// # Returns
///
/// A `Result` containing the response or an error.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn upload_file_with_query_params(
&self,
endpoint: &str,
query_params: &[(&str, &str)],
file_field_name: &str,
filename: &str,
file_data: Vec<u8>,
) -> Result<reqwest::Response, VeracodeError> {
// Build URL with query parameters using centralized helper for consistency
let url = self.build_url_with_params(endpoint, query_params);
// Wrap file data in Arc to avoid cloning during retries
let file_data_arc = Arc::new(file_data);
// Use Cow for strings to minimize allocations - borrow for short strings, own for long ones
let filename_cow: Cow<str> = if filename.len() < 128 {
Cow::Borrowed(filename)
} else {
Cow::Owned(filename.to_string())
};
let field_name_cow: Cow<str> = if file_field_name.len() < 32 {
Cow::Borrowed(file_field_name)
} else {
Cow::Owned(file_field_name.to_string())
};
// Create request builder closure for retry logic
let request_builder = || {
// Clone Arc (cheap - just increments reference count)
let file_data_clone = Arc::clone(&file_data_arc);
// Re-create multipart form for each attempt
let Ok(part) = multipart::Part::bytes((*file_data_clone).clone())
.file_name(filename_cow.to_string())
.mime_str("application/octet-stream")
else {
return self.client.post("invalid://url");
};
let form = multipart::Form::new().part(field_name_cow.to_string(), part);
// Re-generate auth header for each attempt to avoid signature expiry
let Ok(auth_header) = self.generate_auth_header("POST", &url) else {
return self.client.post("invalid://url");
};
self.client
.post(&url)
.header("Authorization", auth_header)
.header("User-Agent", "Veracode Rust Client")
.multipart(form)
};
// Use Cow for operation name based on endpoint length to minimize allocations
let operation_name: Cow<str> = if endpoint.len() < 50 {
Cow::Owned(format!("File Upload POST {endpoint}"))
} else {
Cow::Borrowed("File Upload POST [long endpoint]")
};
self.execute_with_retry(request_builder, operation_name)
.await
}
/// Make a POST request with query parameters (like Java implementation for XML API)
///
/// This method mimics the Java API wrapper's approach for POST operations
/// where parameters are added to the query string rather than form data.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint to call
/// * `query_params` - Query parameters as key-value pairs
///
/// # Returns
///
/// A `Result` containing the response or an error.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn post_with_query_params(
&self,
endpoint: &str,
query_params: &[(&str, &str)],
) -> Result<reqwest::Response, VeracodeError> {
// Build URL with query parameters using centralized helper
let url = self.build_url_with_params(endpoint, query_params);
let auth_header = self.generate_auth_header("POST", &url)?;
let response = self
.client
.post(&url)
.header("Authorization", auth_header)
.header("User-Agent", "Veracode Rust Client")
.send()
.await?;
Ok(response)
}
/// Make a GET request with query parameters (like Java implementation for XML API)
///
/// This method mimics the Java API wrapper's approach for GET operations
/// where parameters are added to the query string.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint to call
/// * `query_params` - Query parameters as key-value pairs
///
/// # Returns
///
/// A `Result` containing the response or an error.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn get_with_query_params(
&self,
endpoint: &str,
query_params: &[(&str, &str)],
) -> Result<reqwest::Response, VeracodeError> {
// Build URL with query parameters using centralized helper
let url = self.build_url_with_params(endpoint, query_params);
let auth_header = self.generate_auth_header("GET", &url)?;
let response = self
.client
.get(&url)
.header("Authorization", auth_header)
.header("User-Agent", "Veracode Rust Client")
.send()
.await?;
Ok(response)
}
/// Upload a large file using chunked streaming (for uploadlargefile.do)
///
/// This method implements chunked upload functionality similar to the Java API wrapper.
/// It uploads files in chunks and provides progress tracking capabilities.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint to call
/// * `query_params` - Query parameters as key-value pairs
/// * `file_path` - Path to the file to upload
/// * `content_type` - Content type for the file (default: binary/octet-stream)
/// * `progress_callback` - Optional callback for progress tracking
///
/// # Returns
///
/// A `Result` containing the response or an error.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn upload_large_file_chunked<F>(
&self,
endpoint: &str,
query_params: &[(&str, &str)],
file_path: &str,
content_type: Option<&str>,
progress_callback: Option<F>,
) -> Result<reqwest::Response, VeracodeError>
where
F: Fn(u64, u64, f64) + Send + Sync,
{
// Build URL with query parameters using centralized helper
let url = self.build_url_with_params(endpoint, query_params);
// Open file and get size
let mut file = File::open(file_path)
.map_err(|e| VeracodeError::InvalidConfig(format!("Failed to open file: {e}")))?;
let file_size = file
.metadata()
.map_err(|e| VeracodeError::InvalidConfig(format!("Failed to get file size: {e}")))?
.len();
// Check file size limit (2GB for uploadlargefile.do)
#[allow(clippy::arithmetic_side_effects)]
const MAX_FILE_SIZE: u64 = 2 * 1024 * 1024 * 1024; // 2GB
if file_size > MAX_FILE_SIZE {
return Err(VeracodeError::InvalidConfig(format!(
"File size ({file_size} bytes) exceeds maximum limit of {MAX_FILE_SIZE} bytes"
)));
}
// Read entire file into memory for progress tracking and retry support
file.seek(SeekFrom::Start(0))
.map_err(|e| VeracodeError::InvalidConfig(format!("Failed to seek file: {e}")))?;
#[allow(clippy::cast_possible_truncation)]
let mut file_data_vec = Vec::with_capacity(file_size as usize);
file.read_to_end(&mut file_data_vec)
.map_err(|e| VeracodeError::InvalidConfig(format!("Failed to read file: {e}")))?;
// Convert to Bytes for cheap cloning (Arc-backed internally)
// This prevents expensive memory copies on each retry attempt
let file_data = Bytes::from(file_data_vec);
let content_type_cow: Cow<str> =
content_type.map_or(Cow::Borrowed("binary/octet-stream"), |ct| {
if ct.len() < 64 {
Cow::Borrowed(ct)
} else {
Cow::Owned(ct.to_string())
}
});
// Create request builder closure for retry logic
let request_builder = || {
// Clone Bytes (cheap - Arc-backed internally, just increments reference count)
let body_data = file_data.clone();
// Re-generate auth header for each attempt to avoid signature expiry
let Ok(auth_header) = self.generate_auth_header("POST", &url) else {
return self.client.post("invalid://url");
};
self.client
.post(&url)
.header("Authorization", auth_header)
.header("User-Agent", "Veracode Rust Client")
.header("Content-Type", content_type_cow.as_ref())
.header("Content-Length", file_size.to_string())
.body(body_data)
};
// Track progress if callback provided (before upload starts)
if let Some(ref callback) = progress_callback {
callback(0, file_size, 0.0);
}
// Use optimized operation name
let operation_name: Cow<str> = if endpoint.len() < 50 {
Cow::Owned(format!("Large File Upload POST {endpoint}"))
} else {
Cow::Borrowed("Large File Upload POST [long endpoint]")
};
let response = self
.execute_with_retry(request_builder, operation_name)
.await?;
// Track progress if callback provided (after upload completes)
if let Some(callback) = progress_callback {
callback(file_size, file_size, 100.0);
}
Ok(response)
}
/// Upload a file with binary data (optimized for uploadlargefile.do)
///
/// This method uploads a file as raw binary data without multipart encoding,
/// which is the expected format for the uploadlargefile.do endpoint.
///
/// Memory optimization: Uses Arc for file data and Cow for strings to minimize
/// allocations during retry attempts. Automatically retries on transient failures.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint to call
/// * `query_params` - Query parameters as key-value pairs
/// * `file_data` - File data as bytes
/// * `content_type` - Content type for the file
///
/// # Returns
///
/// A `Result` containing the response or an error.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn upload_file_binary(
&self,
endpoint: &str,
query_params: &[(&str, &str)],
file_data: Vec<u8>,
content_type: &str,
) -> Result<reqwest::Response, VeracodeError> {
// Build URL with query parameters using centralized helper
let url = self.build_url_with_params(endpoint, query_params);
// Convert to Bytes for cheap cloning (Arc-backed internally)
// This prevents expensive memory copies on each retry attempt
let file_data = Bytes::from(file_data);
let file_size = file_data.len();
// Use Cow for content type to minimize allocations
let content_type_cow: Cow<str> = if content_type.len() < 64 {
Cow::Borrowed(content_type)
} else {
Cow::Owned(content_type.to_string())
};
// Create request builder closure for retry logic
let request_builder = || {
// Clone Bytes (cheap - Arc-backed internally, just increments reference count)
let body_data = file_data.clone();
// Re-generate auth header for each attempt to avoid signature expiry
let Ok(auth_header) = self.generate_auth_header("POST", &url) else {
return self.client.post("invalid://url");
};
self.client
.post(&url)
.header("Authorization", auth_header)
.header("User-Agent", "Veracode Rust Client")
.header("Content-Type", content_type_cow.as_ref())
.header("Content-Length", file_size.to_string())
.body(body_data)
};
// Use optimized operation name based on endpoint length
let operation_name: Cow<str> = if endpoint.len() < 50 {
Cow::Owned(format!("Binary File Upload POST {endpoint}"))
} else {
Cow::Borrowed("Binary File Upload POST [long endpoint]")
};
self.execute_with_retry(request_builder, operation_name)
.await
}
/// Upload a file with streaming (memory-efficient for large files)
///
/// This method streams the file directly from disk without loading it entirely into memory.
/// This is the recommended approach for large files (>100MB) to avoid high memory usage.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint to call
/// * `query_params` - Query parameters as key-value pairs
/// * `file_path` - Path to the file to upload
/// * `file_size` - Size of the file in bytes
/// * `content_type` - Content type for the file
///
/// # Returns
///
/// A `Result` containing the response or an error.
///
/// # Errors
///
/// Returns an error if the API request fails, the resource is not found,
/// or authentication/authorization fails.
pub async fn upload_file_streaming(
&self,
endpoint: &str,
query_params: &[(&str, &str)],
file_path: &str,
file_size: u64,
content_type: &str,
) -> Result<reqwest::Response, VeracodeError> {
// Build URL with query parameters using centralized helper
let url = self.build_url_with_params(endpoint, query_params);
// Open file using tokio for async streaming
let file = TokioFile::open(file_path)
.await
.map_err(|e| VeracodeError::InvalidConfig(format!("Failed to open file: {e}")))?;
// Create a stream from the file using tokio_util
let stream = tokio_util::io::ReaderStream::new(file);
let body = Body::wrap_stream(stream);
// Generate auth header
let auth_header = self.generate_auth_header("POST", &url)?;
// Build and send the request (streaming upload - no retry support)
// Note: Streaming bodies cannot be retried because the stream is consumed
let response = self
.client
.post(&url)
.header("Authorization", auth_header)
.header("User-Agent", "Veracode Rust Client")
.header("Content-Type", content_type)
.header("Content-Length", file_size.to_string())
.body(body)
.send()
.await
.map_err(VeracodeError::Http)?;
Ok(response)
}
}
#[cfg(test)]
#[allow(clippy::expect_used)] // Test code: expect is acceptable for test setup
mod tests {
use super::*;
use proptest::prelude::*;
// ============================================================================
// TIER 1: PROPERTY-BASED SECURITY TESTS (Fast, High ROI)
// ============================================================================
/// Helper to create a test config with dummy credentials
fn create_test_config() -> VeracodeConfig {
use crate::{VeracodeCredentials, VeracodeRegion};
VeracodeConfig {
credentials: VeracodeCredentials::new(
"test_api_id".to_string(),
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef".to_string(),
),
base_url: "https://api.veracode.com".to_string(),
rest_base_url: "https://api.veracode.com".to_string(),
xml_base_url: "https://analysiscenter.veracode.com".to_string(),
region: VeracodeRegion::Commercial,
validate_certificates: true,
connect_timeout: 30,
request_timeout: 300,
proxy_url: None,
proxy_username: None,
proxy_password: None,
retry_config: Default::default(),
}
}
// ============================================================================
// SECURITY TEST: URL Construction & Parameter Encoding
// ============================================================================
proptest! {
#![proptest_config(ProptestConfig {
cases: if cfg!(miri) { 5 } else { 1000 },
failure_persistence: None,
.. ProptestConfig::default()
})]
/// Property: URL parameter encoding must prevent injection attacks
/// Tests that special characters are properly encoded and cannot break URL structure
#[test]
fn proptest_url_params_prevent_injection(
key in "[a-zA-Z0-9_]{1,50}",
value in ".*{0,100}",
) {
let config = create_test_config();
let client = VeracodeClient::new(config)
.expect("valid test client configuration");
let params = vec![(key.as_str(), value.as_str())];
let url = client.build_url_with_params("/api/test", ¶ms);
// Property 1: URL must not contain unencoded dangerous characters
prop_assert!(!url.contains("<script>"));
prop_assert!(!url.contains("javascript:"));
// Property 2: URL must contain properly encoded parameters
prop_assert!(url.starts_with("https://api.veracode.com/api/test"));
// Property 3: If params are present, URL must contain '?'
if !params.is_empty() && !key.is_empty() {
prop_assert!(url.contains('?'));
}
}
/// Property: URL construction must handle capacity overflow safely
/// Tests that large numbers of parameters don't cause panics or overflows
#[test]
fn proptest_url_params_capacity_safe(
param_count in 0usize..=100,
) {
let config = create_test_config();
let client = VeracodeClient::new(config)
.expect("valid test client configuration");
// Create param_count parameters
let params: Vec<(&str, &str)> = (0..param_count)
.map(|_| ("key", "value"))
.collect();
// Must not panic on capacity calculations
let url = client.build_url_with_params("/api/test", ¶ms);
// Property: URL should be valid and not panic
prop_assert!(url.starts_with("https://"));
prop_assert!(url.len() < 100000); // Reasonable upper bound
}
/// Property: Empty and whitespace-only parameters are handled safely
#[test]
fn proptest_url_params_empty_safe(
key in "\\s*",
value in "\\s*",
) {
let config = create_test_config();
let client = VeracodeClient::new(config)
.expect("valid test client configuration");
let params = vec![(key.as_str(), value.as_str())];
let url = client.build_url_with_params("/api/test", ¶ms);
// Must not panic and produce valid URL
prop_assert!(url.starts_with("https://"));
}
}
// ============================================================================
// SECURITY TEST: HMAC Signature Generation
// ============================================================================
proptest! {
#![proptest_config(ProptestConfig {
cases: if cfg!(miri) { 5 } else { 1000 },
failure_persistence: None,
.. ProptestConfig::default()
})]
/// Property: HMAC signature generation must handle invalid URLs gracefully
/// Tests that malformed URLs return errors instead of panicking
#[test]
fn proptest_hmac_invalid_urls_return_error(
invalid_url in ".*{0,100}",
) {
let config = create_test_config();
let client = VeracodeClient::new(config)
.expect("valid test client configuration");
// Property: Invalid URLs must return Err, never panic
let result = client.generate_hmac_signature(
"GET",
&invalid_url,
1234567890000,
"0123456789abcdef0123456789abcdef",
);
// Either succeeds (if URL happens to be valid) or returns error
match result {
Ok(_) => {
// If it succeeded, the URL must have been parseable
prop_assert!(Url::parse(&invalid_url).is_ok());
},
Err(e) => {
// Error must be Authentication error
prop_assert!(matches!(e, VeracodeError::Authentication(_)));
}
}
}
/// Property: HMAC signature must be deterministic
/// Same inputs must always produce the same signature
#[test]
fn proptest_hmac_deterministic(
method in "[A-Z]{3,7}",
timestamp in 1000000000000u64..2000000000000u64,
) {
let config = create_test_config();
let client = VeracodeClient::new(config)
.expect("valid test client configuration");
let url = "https://api.veracode.com/api/test";
let nonce = "0123456789abcdef0123456789abcdef";
let sig1 = client.generate_hmac_signature(&method, url, timestamp, nonce);
let sig2 = client.generate_hmac_signature(&method, url, timestamp, nonce);
// Property: Deterministic - same inputs produce same output
match (sig1, sig2) {
(Ok(s1), Ok(s2)) => prop_assert_eq!(s1, s2),
(Err(_), Err(_)) => {}, // Both failed - also deterministic
_ => prop_assert!(false, "Non-deterministic result"),
}
}
/// Property: Invalid hex nonce must return error
/// Tests that non-hex nonces are rejected safely
#[test]
fn proptest_hmac_invalid_nonce_returns_error(
invalid_nonce in "[^0-9a-fA-F]{1,32}",
) {
let config = create_test_config();
let client = VeracodeClient::new(config)
.expect("valid test client configuration");
let result = client.generate_hmac_signature(
"GET",
"https://api.veracode.com/api/test",
1234567890000,
&invalid_nonce,
);
// Property: Non-hex nonce must return Authentication error
prop_assert!(matches!(result, Err(VeracodeError::Authentication(_))));
}
/// Property: Timestamp overflow must be handled safely
/// Tests edge cases in timestamp handling
#[test]
fn proptest_hmac_timestamp_safe(
timestamp in any::<u64>(),
) {
let config = create_test_config();
let client = VeracodeClient::new(config)
.expect("valid test client configuration");
let url = "https://api.veracode.com/api/test";
let nonce = "0123456789abcdef0123456789abcdef";
// Must not panic on any timestamp value
let result = client.generate_hmac_signature("GET", url, timestamp, nonce);
// Property: Either succeeds or returns error, never panics
prop_assert!(result.is_ok() || result.is_err());
}
}
// ============================================================================
// SECURITY TEST: Authentication Header Generation
// ============================================================================
proptest! {
#![proptest_config(ProptestConfig {
cases: if cfg!(miri) { 5 } else { 1000 },
failure_persistence: None,
.. ProptestConfig::default()
})]
/// Property: Auth header must contain all required components
/// Tests that generated headers have proper VERACODE-HMAC-SHA-256 format
#[test]
fn proptest_auth_header_format(
method in "[A-Z]{3,7}",
) {
let config = create_test_config();
let client = VeracodeClient::new(config)
.expect("valid test client configuration");
let url = "https://api.veracode.com/api/test";
let result = client.generate_auth_header(&method, url);
if let Ok(header) = result {
// Property 1: Must start with correct prefix
prop_assert!(header.starts_with("VERACODE-HMAC-SHA-256"));
// Property 2: Must contain all required fields
prop_assert!(header.contains("id="));
prop_assert!(header.contains("ts="));
prop_assert!(header.contains("nonce="));
prop_assert!(header.contains("sig="));
// Property 3: Fields must be comma-separated
let parts: Vec<&str> = header.split(',').collect();
prop_assert_eq!(parts.len(), 4);
}
}
/// Property: Auth header nonce must be unique and valid hex
/// Tests that nonces are properly generated as 32-character hex strings
#[test]
fn proptest_auth_header_nonce_unique(
_seed in any::<u8>(),
) {
let config = create_test_config();
let client = VeracodeClient::new(config)
.expect("valid test client configuration");
let url = "https://api.veracode.com/api/test";
// Generate two headers
let header1 = client.generate_auth_header("GET", url)
.expect("valid auth header generation");
let header2 = client.generate_auth_header("GET", url)
.expect("valid auth header generation");
// Extract nonces using a helper function (avoids lifetime issues)
fn extract_nonce(h: &str) -> Option<String> {
Some(h.split("nonce=")
.nth(1)?
.split(',')
.next()?
.to_string())
}
if let (Some(nonce1), Some(nonce2)) = (extract_nonce(&header1), extract_nonce(&header2)) {
// Property 1: Nonces should be different (probabilistically)
// With 128-bit random, collision is extremely unlikely
prop_assert_ne!(&nonce1, &nonce2);
// Property 2: Nonces must be valid hex (32 chars for 16 bytes)
prop_assert_eq!(nonce1.len(), 32);
prop_assert_eq!(nonce2.len(), 32);
prop_assert!(nonce1.chars().all(|c| c.is_ascii_hexdigit()));
prop_assert!(nonce2.chars().all(|c| c.is_ascii_hexdigit()));
}
}
}
// ============================================================================
// SECURITY TEST: Configuration & Client Creation
// ============================================================================
proptest! {
#![proptest_config(ProptestConfig {
cases: if cfg!(miri) { 5 } else { 100 },
failure_persistence: None,
.. ProptestConfig::default()
})]
/// Property: Client creation with invalid config must fail gracefully
/// Tests that invalid proxy URLs are caught during client creation
#[test]
fn proptest_client_creation_invalid_proxy(
invalid_proxy in ".*{0,100}",
) {
use crate::{VeracodeCredentials, VeracodeRegion};
let config = VeracodeConfig {
credentials: VeracodeCredentials::new(
"test_api_id".to_string(),
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef".to_string(),
),
base_url: "https://api.veracode.com".to_string(),
rest_base_url: "https://api.veracode.com".to_string(),
xml_base_url: "https://analysiscenter.veracode.com".to_string(),
region: VeracodeRegion::Commercial,
validate_certificates: true,
connect_timeout: 30,
request_timeout: 300,
proxy_url: Some(invalid_proxy.clone()),
proxy_username: None,
proxy_password: None,
retry_config: Default::default(),
};
let result = VeracodeClient::new(config);
// Property: Either succeeds (if proxy URL is valid) or returns InvalidConfig error
match result {
Ok(_) => {
// If successful, proxy URL must be valid
prop_assert!(reqwest::Proxy::all(&invalid_proxy).is_ok());
},
Err(e) => {
// Must be InvalidConfig error
prop_assert!(matches!(e, VeracodeError::InvalidConfig(_)));
}
}
}
/// Property: Timeout values must be handled safely
/// Tests that extreme timeout values don't cause panics
#[test]
fn proptest_client_timeouts_safe(
connect_timeout in 1u64..=3600,
request_timeout in 1u64..=7200,
) {
use crate::{VeracodeCredentials, VeracodeRegion};
let config = VeracodeConfig {
credentials: VeracodeCredentials::new(
"test_api_id".to_string(),
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef".to_string(),
),
base_url: "https://api.veracode.com".to_string(),
rest_base_url: "https://api.veracode.com".to_string(),
xml_base_url: "https://analysiscenter.veracode.com".to_string(),
region: VeracodeRegion::Commercial,
validate_certificates: true,
connect_timeout,
request_timeout,
proxy_url: None,
proxy_username: None,
proxy_password: None,
retry_config: Default::default(),
};
// Must not panic on any valid timeout values
let result = VeracodeClient::new(config);
prop_assert!(result.is_ok());
}
}
// ============================================================================
// SECURITY TEST: File Size Limits & Memory Safety
// ============================================================================
proptest! {
#![proptest_config(ProptestConfig {
cases: if cfg!(miri) { 5 } else { 100 },
failure_persistence: None,
.. ProptestConfig::default()
})]
/// Property: File size calculations must not overflow
/// Tests that capacity calculations for file uploads are safe
#[test]
fn proptest_file_upload_capacity_safe(
file_size in 0usize..=1000000,
) {
// Create file data of specified size
let file_data = vec![0u8; file_size];
// Wrap in Arc like the upload functions do
let file_data_arc = Arc::new(file_data);
// Property 1: Length must match original size
prop_assert_eq!(file_data_arc.len(), file_size);
// Property 2: Arc clone must be cheap (same allocation)
let clone1 = Arc::clone(&file_data_arc);
let clone2 = Arc::clone(&file_data_arc);
prop_assert_eq!(clone1.len(), file_size);
prop_assert_eq!(clone2.len(), file_size);
}
/// Property: Content-Type handling must prevent injection
/// Tests that content types are handled safely without code execution
#[test]
fn proptest_content_type_safe(
content_type in ".*{0,200}",
) {
// Test Cow allocation strategy
let content_type_cow: Cow<str> = if content_type.len() < 64 {
Cow::Borrowed(&content_type)
} else {
Cow::Owned(content_type.clone())
};
// Property 1: Must not contain script injection attempts
let ct_lower = content_type_cow.to_lowercase();
if ct_lower.contains("<script>") || ct_lower.contains("javascript:") {
// These should be treated as literal strings, not executed
prop_assert!(content_type_cow.as_ref().contains("<script>") ||
content_type_cow.as_ref().contains("javascript:"));
}
// Property 2: Length must be preserved
prop_assert_eq!(content_type_cow.len(), content_type.len());
}
}
// ============================================================================
// UNIT TESTS: Specific Security Scenarios
// ============================================================================
#[test]
fn test_hmac_signature_with_query_params() {
let config = create_test_config();
let client = VeracodeClient::new(config).expect("valid test client configuration");
// Test URL with query parameters
let url = "https://api.veracode.com/api/test?param1=value1¶m2=value2";
let nonce = "0123456789abcdef0123456789abcdef";
let timestamp = 1234567890000;
let result = client.generate_hmac_signature("GET", url, timestamp, nonce);
assert!(result.is_ok());
let signature = result.expect("valid HMAC signature");
// HMAC signature should be 64 hex characters (32 bytes * 2)
assert_eq!(signature.len(), 64);
assert!(signature.chars().all(|c| c.is_ascii_hexdigit()));
}
#[test]
fn test_hmac_signature_different_methods() {
let config = create_test_config();
let client = VeracodeClient::new(config).expect("valid test client configuration");
let url = "https://api.veracode.com/api/test";
let nonce = "0123456789abcdef0123456789abcdef";
let timestamp = 1234567890000;
let sig_get = client
.generate_hmac_signature("GET", url, timestamp, nonce)
.expect("valid HMAC signature for GET");
let sig_post = client
.generate_hmac_signature("POST", url, timestamp, nonce)
.expect("valid HMAC signature for POST");
// Different methods should produce different signatures
assert_ne!(sig_get, sig_post);
}
#[test]
fn test_url_encoding_special_characters() {
let config = create_test_config();
let client = VeracodeClient::new(config).expect("valid test client configuration");
// Test that special characters are properly encoded
let params = vec![
("key1", "value with spaces"),
("key2", "value&with&ersands"),
("key3", "value=with=equals"),
("key4", "value?with?questions"),
];
let url = client.build_url_with_params("/api/test", ¶ms);
// URL should contain encoded spaces
assert!(url.contains("value%20with%20spaces") || url.contains("value+with+spaces"));
// URL should contain encoded ampersands
assert!(url.contains("%26"));
// URL should start with base URL
assert!(url.starts_with("https://api.veracode.com/api/test?"));
}
#[test]
fn test_url_encoding_unicode() {
let config = create_test_config();
let client = VeracodeClient::new(config).expect("valid test client configuration");
// Test Unicode handling
let params = vec![
("key", "你好世界"), // Chinese characters
("key2", "🔒🛡️"), // Emojis
];
let url = client.build_url_with_params("/api/test", ¶ms);
// Must not panic and should produce valid URL
assert!(url.starts_with("https://api.veracode.com/api/test?"));
// URL should contain percent-encoded Unicode
assert!(url.contains('%'));
}
#[test]
fn test_empty_query_params() {
let config = create_test_config();
let client = VeracodeClient::new(config).expect("valid test client configuration");
let url = client.build_url_with_params("/api/test", &[]);
// Empty params should not add '?'
assert_eq!(url, "https://api.veracode.com/api/test");
}
#[test]
fn test_invalid_api_key_format() {
use crate::{VeracodeCredentials, VeracodeRegion};
// Create config with non-hex API key
let config = VeracodeConfig {
credentials: VeracodeCredentials::new(
"test_api_id".to_string(),
"not_valid_hex_key".to_string(),
),
base_url: "https://api.veracode.com".to_string(),
rest_base_url: "https://api.veracode.com".to_string(),
xml_base_url: "https://analysiscenter.veracode.com".to_string(),
region: VeracodeRegion::Commercial,
validate_certificates: true,
connect_timeout: 30,
request_timeout: 300,
proxy_url: None,
proxy_username: None,
proxy_password: None,
retry_config: Default::default(),
};
let client = VeracodeClient::new(config).expect("valid test client configuration");
let result = client.generate_auth_header("GET", "https://api.veracode.com/api/test");
// Should return Authentication error for invalid hex key
assert!(matches!(result, Err(VeracodeError::Authentication(_))));
}
#[test]
fn test_auth_header_format() {
let config = create_test_config();
let client = VeracodeClient::new(config).expect("valid test client configuration");
let header = client
.generate_auth_header("GET", "https://api.veracode.com/api/test")
.expect("valid auth header generation");
// Verify format
assert!(header.starts_with("VERACODE-HMAC-SHA-256 "));
assert!(header.contains("id=test_api_id"));
assert!(header.contains("ts="));
assert!(header.contains("nonce="));
assert!(header.contains("sig="));
// Verify structure (should have 4 comma-separated parts after prefix)
let parts: Vec<&str> = header.split(',').collect();
assert_eq!(parts.len(), 4);
}
#[cfg(not(miri))] // Skip under Miri - uses SystemTime
#[test]
fn test_auth_header_timestamp_monotonic() {
let config = create_test_config();
let client = VeracodeClient::new(config).expect("valid test client configuration");
let header1 = client
.generate_auth_header("GET", "https://api.veracode.com/api/test")
.expect("valid auth header generation");
std::thread::sleep(std::time::Duration::from_millis(10));
let header2 = client
.generate_auth_header("GET", "https://api.veracode.com/api/test")
.expect("valid auth header generation");
// Extract timestamps
let extract_ts =
|h: &str| -> Option<u64> { h.split("ts=").nth(1)?.split(',').next()?.parse().ok() };
let ts1 = extract_ts(&header1).expect("valid timestamp extraction");
let ts2 = extract_ts(&header2).expect("valid timestamp extraction");
// Second timestamp should be >= first (monotonic)
assert!(ts2 >= ts1);
}
#[test]
fn test_base_url_accessor() {
let config = create_test_config();
let client = VeracodeClient::new(config).expect("valid test client configuration");
assert_eq!(client.base_url(), "https://api.veracode.com");
}
#[test]
fn test_client_clone() {
let config = create_test_config();
let client1 = VeracodeClient::new(config).expect("valid test client configuration");
let client2 = client1.clone();
// Both clients should have same base URL
assert_eq!(client1.base_url(), client2.base_url());
}
#[test]
fn test_url_capacity_estimation() {
let config = create_test_config();
let client = VeracodeClient::new(config).expect("valid test client configuration");
// Test with large number of parameters
let params: Vec<(&str, &str)> = (0..100).map(|_| ("key", "value")).collect();
let url = client.build_url_with_params("/api/test", ¶ms);
// Should handle large param counts without panic
assert!(url.starts_with("https://api.veracode.com/api/test?"));
assert!(url.len() > 100); // Should contain all params
}
#[test]
fn test_saturating_arithmetic() {
let config = create_test_config();
let client = VeracodeClient::new(config).expect("valid test client configuration");
// Test saturating_add in capacity calculation
let params: Vec<(&str, &str)> = vec![("k", "v"); 1000];
// Should not panic even with large numbers
let url = client.build_url_with_params("/api/test", ¶ms);
assert!(url.len() < usize::MAX);
}
}