|
| 1 | +/** |
| 2 | + * OWASP Benchmark Project |
| 3 | + * |
| 4 | + * <p>This file is part of the Open Web Application Security Project (OWASP) Benchmark Project For |
| 5 | + * details, please see <a |
| 6 | + * href="https://owasp.org/www-project-benchmark/">https://owasp.org/www-project-benchmark/</a>. |
| 7 | + * |
| 8 | + * <p>The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms |
| 9 | + * of the GNU General Public License as published by the Free Software Foundation, version 2. |
| 10 | + * |
| 11 | + * <p>The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 13 | + * PURPOSE. See the GNU General Public License for more details. |
| 14 | + */ |
| 15 | +package org.owasp.benchmark.report.sonarqube; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.CsvSource; |
| 23 | + |
| 24 | +public class SonarReportTest { |
| 25 | + |
| 26 | + private static final int PAGE_SIZE = 500; |
| 27 | + |
| 28 | + /** |
| 29 | + * Replicates the fixed paging formula from SonarReport.forAllPagesAt(). The old formula was |
| 30 | + * {@code (resultCount / PAGE_SIZE) + 1}, which over-counts by 1 when resultCount is an exact |
| 31 | + * multiple of PAGE_SIZE. |
| 32 | + */ |
| 33 | + private static int calculatePages(int resultCount) { |
| 34 | + return (resultCount / PAGE_SIZE) + (resultCount % PAGE_SIZE == 0 ? 0 : 1); |
| 35 | + } |
| 36 | + |
| 37 | + @ParameterizedTest(name = "resultCount={0} => pages={1}") |
| 38 | + @CsvSource({ |
| 39 | + "0, 0", |
| 40 | + "1, 1", |
| 41 | + "499, 1", |
| 42 | + "500, 1", |
| 43 | + "501, 2", |
| 44 | + "999, 2", |
| 45 | + "1000, 2", |
| 46 | + "1001, 3", |
| 47 | + "10000, 20", |
| 48 | + "10001, 21" |
| 49 | + }) |
| 50 | + void pageCalculationProducesCorrectCeiling(int resultCount, int expectedPages) { |
| 51 | + assertEquals( |
| 52 | + expectedPages, |
| 53 | + calculatePages(resultCount), |
| 54 | + "Paging for " + resultCount + " results at page size " + PAGE_SIZE); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void oldFormulaOvercountsOnExactMultiple() { |
| 59 | + int resultCount = 1000; |
| 60 | + int oldFormula = (resultCount / PAGE_SIZE) + 1; |
| 61 | + int fixedFormula = calculatePages(resultCount); |
| 62 | + |
| 63 | + assertEquals(3, oldFormula, "Old formula produces 3 pages for 1000 results (wrong)"); |
| 64 | + assertEquals(2, fixedFormula, "Fixed formula produces 2 pages for 1000 results (correct)"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void issueSearchUrlHasNoDoubleAmpersand() { |
| 69 | + String sonarProject = "benchmark"; |
| 70 | + String rule = "java:S1234"; |
| 71 | + |
| 72 | + String fixedUrl = |
| 73 | + "issues/search?componentKeys=" |
| 74 | + + sonarProject |
| 75 | + + "&types=VULNERABILITY&rules=" |
| 76 | + + rule; |
| 77 | + |
| 78 | + assertFalse(fixedUrl.contains("&&"), "URL must not contain double ampersand"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void oldIssueSearchUrlHadDoubleAmpersand() { |
| 83 | + String sonarProject = "benchmark"; |
| 84 | + String allJavaRules = "java:S1234,java:S5678"; |
| 85 | + |
| 86 | + String oldUrl = |
| 87 | + "issues/search?componentKeys=" |
| 88 | + + sonarProject |
| 89 | + + "&types=VULNERABILITY&&rules=" |
| 90 | + + allJavaRules; |
| 91 | + |
| 92 | + assertEquals( |
| 93 | + true, oldUrl.contains("&&"), "Old URL construction had double ampersand (the bug)"); |
| 94 | + } |
| 95 | +} |
0 commit comments