您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SecurityStandardsTest.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.security;
  21. import java.util.Arrays;
  22. import java.util.EnumSet;
  23. import java.util.List;
  24. import java.util.Set;
  25. import java.util.stream.Collectors;
  26. import org.junit.Test;
  27. import org.sonar.api.server.rule.RulesDefinition.OwaspAsvsVersion;
  28. import org.sonar.server.security.SecurityStandards.OwaspAsvs;
  29. import org.sonar.server.security.SecurityStandards.PciDss;
  30. import org.sonar.server.security.SecurityStandards.SQCategory;
  31. import static java.util.Collections.emptySet;
  32. import static java.util.Collections.singleton;
  33. import static java.util.stream.Collectors.toSet;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.junit.Assert.assertEquals;
  36. import static org.junit.Assert.assertTrue;
  37. import static org.sonar.server.security.SecurityStandards.CWES_BY_SQ_CATEGORY;
  38. import static org.sonar.server.security.SecurityStandards.OWASP_ASVS_REQUIREMENTS_BY_LEVEL;
  39. import static org.sonar.server.security.SecurityStandards.SQ_CATEGORY_KEYS_ORDERING;
  40. import static org.sonar.server.security.SecurityStandards.fromSecurityStandards;
  41. import static org.sonar.server.security.SecurityStandards.getRequirementsForCategoryAndLevel;
  42. public class SecurityStandardsTest {
  43. @Test
  44. public void fromSecurityStandards_from_empty_set_has_SQCategory_OTHERS() {
  45. SecurityStandards securityStandards = fromSecurityStandards(emptySet());
  46. assertThat(securityStandards.getStandards()).isEmpty();
  47. assertThat(securityStandards.getSqCategory()).isEqualTo(SQCategory.OTHERS);
  48. assertThat(securityStandards.getIgnoredSQCategories()).isEmpty();
  49. }
  50. @Test
  51. public void fromSecurityStandards_from_empty_set_has_unkwown_cwe_standard() {
  52. SecurityStandards securityStandards = fromSecurityStandards(emptySet());
  53. assertThat(securityStandards.getStandards()).isEmpty();
  54. assertThat(securityStandards.getCwe()).containsOnly("unknown");
  55. }
  56. @Test
  57. public void fromSecurityStandards_from_empty_set_has_no_OwaspTop10_standard() {
  58. SecurityStandards securityStandards = fromSecurityStandards(emptySet());
  59. assertThat(securityStandards.getStandards()).isEmpty();
  60. assertThat(securityStandards.getOwaspTop10()).isEmpty();
  61. }
  62. @Test
  63. public void fromSecurityStandards_from_empty_set_has_no_SansTop25_standard() {
  64. SecurityStandards securityStandards = fromSecurityStandards(emptySet());
  65. assertThat(securityStandards.getStandards()).isEmpty();
  66. assertThat(securityStandards.getSansTop25()).isEmpty();
  67. }
  68. @Test
  69. public void fromSecurityStandards_from_empty_set_has_no_CweTop25_standard() {
  70. SecurityStandards securityStandards = fromSecurityStandards(emptySet());
  71. assertThat(securityStandards.getStandards()).isEmpty();
  72. assertThat(securityStandards.getCweTop25()).isEmpty();
  73. }
  74. @Test
  75. public void fromSecurityStandards_finds_SQCategory_from_any_if_the_mapped_CWE_standard() {
  76. CWES_BY_SQ_CATEGORY.forEach((sqCategory, cwes) -> {
  77. cwes.forEach(cwe -> {
  78. SecurityStandards securityStandards = fromSecurityStandards(singleton("cwe:" + cwe));
  79. assertThat(securityStandards.getSqCategory()).isEqualTo(sqCategory);
  80. });
  81. });
  82. }
  83. @Test
  84. public void fromSecurityStandards_finds_SQCategory_from_multiple_of_the_mapped_CWE_standard() {
  85. CWES_BY_SQ_CATEGORY.forEach((sqCategory, cwes) -> {
  86. SecurityStandards securityStandards = fromSecurityStandards(cwes.stream().map(t -> "cwe:" + t).collect(toSet()));
  87. assertThat(securityStandards.getSqCategory()).isEqualTo(sqCategory);
  88. });
  89. }
  90. @Test
  91. public void fromSecurityStandards_finds_SQCategory_first_in_order_when_CWEs_map_to_multiple_SQCategories() {
  92. EnumSet<SQCategory> sqCategories = EnumSet.allOf(SQCategory.class);
  93. sqCategories.remove(SQCategory.OTHERS);
  94. while (!sqCategories.isEmpty()) {
  95. SQCategory expected = sqCategories.stream().min(SQ_CATEGORY_KEYS_ORDERING.onResultOf(SQCategory::getKey)).get();
  96. SQCategory[] expectedIgnored = sqCategories.stream().filter(t -> t != expected).toArray(SQCategory[]::new);
  97. Set<String> cwes = sqCategories.stream()
  98. .flatMap(t -> CWES_BY_SQ_CATEGORY.get(t).stream().map(e -> "cwe:" + e))
  99. .collect(Collectors.toSet());
  100. SecurityStandards securityStandards = fromSecurityStandards(cwes);
  101. assertThat(securityStandards.getSqCategory()).isEqualTo(expected);
  102. assertThat(securityStandards.getIgnoredSQCategories()).containsOnly(expectedIgnored);
  103. sqCategories.remove(expected);
  104. }
  105. }
  106. @Test
  107. public void pciDss_categories_check() {
  108. List<String> pciDssCategories = Arrays.stream(PciDss.values()).map(PciDss::category).toList();
  109. assertThat(pciDssCategories).hasSize(12).containsExactly("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");
  110. }
  111. @Test
  112. public void owaspAsvs_categories_check() {
  113. List<String> owaspAsvsCategories = Arrays.stream(OwaspAsvs.values()).map(OwaspAsvs::category).toList();
  114. assertThat(owaspAsvsCategories).hasSize(14).containsExactly("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14");
  115. }
  116. @Test
  117. public void owaspAsvs40_requirements_distribution_by_level_check() {
  118. assertTrue(OWASP_ASVS_REQUIREMENTS_BY_LEVEL.containsKey(OwaspAsvsVersion.V4_0));
  119. assertTrue(OWASP_ASVS_REQUIREMENTS_BY_LEVEL.get(OwaspAsvsVersion.V4_0).containsKey(1));
  120. assertTrue(OWASP_ASVS_REQUIREMENTS_BY_LEVEL.get(OwaspAsvsVersion.V4_0).containsKey(2));
  121. assertTrue(OWASP_ASVS_REQUIREMENTS_BY_LEVEL.get(OwaspAsvsVersion.V4_0).containsKey(3));
  122. assertEquals(135, OWASP_ASVS_REQUIREMENTS_BY_LEVEL.get(OwaspAsvsVersion.V4_0).get(1).size());
  123. assertEquals(266, OWASP_ASVS_REQUIREMENTS_BY_LEVEL.get(OwaspAsvsVersion.V4_0).get(2).size());
  124. assertEquals(286, OWASP_ASVS_REQUIREMENTS_BY_LEVEL.get(OwaspAsvsVersion.V4_0).get(3).size());
  125. }
  126. @Test
  127. public void owaspAsvs40_requirements_by_category_and_level_check() {
  128. assertEquals(0, getRequirementsForCategoryAndLevel(OwaspAsvs.C1, 1).size());
  129. assertEquals(31, getRequirementsForCategoryAndLevel(OwaspAsvs.C2, 1).size());
  130. assertEquals(12, getRequirementsForCategoryAndLevel(OwaspAsvs.C3, 1).size());
  131. assertEquals(9, getRequirementsForCategoryAndLevel(OwaspAsvs.C4, 1).size());
  132. assertEquals(27, getRequirementsForCategoryAndLevel(OwaspAsvs.C5, 1).size());
  133. assertEquals(1, getRequirementsForCategoryAndLevel(OwaspAsvs.C6, 1).size());
  134. assertEquals(3, getRequirementsForCategoryAndLevel(OwaspAsvs.C7, 1).size());
  135. assertEquals(7, getRequirementsForCategoryAndLevel(OwaspAsvs.C8, 1).size());
  136. assertEquals(3, getRequirementsForCategoryAndLevel(OwaspAsvs.C9, 1).size());
  137. assertEquals(3, getRequirementsForCategoryAndLevel(OwaspAsvs.C10, 1).size());
  138. assertEquals(5, getRequirementsForCategoryAndLevel(OwaspAsvs.C11, 1).size());
  139. assertEquals(11, getRequirementsForCategoryAndLevel(OwaspAsvs.C12, 1).size());
  140. assertEquals(7, getRequirementsForCategoryAndLevel(OwaspAsvs.C13, 1).size());
  141. assertEquals(16, getRequirementsForCategoryAndLevel(OwaspAsvs.C14, 1).size());
  142. }
  143. }