You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XooRulesDefinitionTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.xoo.rule;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import org.sonar.api.SonarEdition;
  24. import org.sonar.api.SonarQubeSide;
  25. import org.sonar.api.impl.server.RulesDefinitionContext;
  26. import org.sonar.api.internal.SonarRuntimeImpl;
  27. import org.sonar.api.server.debt.DebtRemediationFunction;
  28. import org.sonar.api.server.rule.RulesDefinition;
  29. import org.sonar.api.utils.Version;
  30. import org.sonar.xoo.rule.hotspot.HotspotWithContextsSensor;
  31. import org.sonar.xoo.rule.hotspot.HotspotWithoutContextSensor;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.HOW_TO_FIX_SECTION_KEY;
  34. public class XooRulesDefinitionTest {
  35. private XooRulesDefinition def = new XooRulesDefinition(SonarRuntimeImpl.forSonarQube(Version.create(9, 3), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY));
  36. private RulesDefinition.Context context = new RulesDefinitionContext();
  37. @Before
  38. public void setUp() {
  39. def.define(context);
  40. }
  41. @Test
  42. public void define_xoo_rules() {
  43. RulesDefinition.Repository repo = getRepository();
  44. RulesDefinition.Rule rule = repo.rule(OneIssuePerLineSensor.RULE_KEY);
  45. assertThat(rule.name()).isNotEmpty();
  46. assertThat(rule.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
  47. assertThat(rule.debtRemediationFunction().gapMultiplier()).isEqualTo("1min");
  48. assertThat(rule.debtRemediationFunction().baseEffort()).isNull();
  49. assertThat(rule.gapDescription()).isNotEmpty();
  50. assertThat(rule.ruleDescriptionSections()).isNotEmpty();
  51. assertThat(rule.ruleDescriptionSections().stream().anyMatch(rds -> rds.getContext().isPresent())).isTrue();
  52. }
  53. @Test
  54. public void define_xoo_hotspot_rule() {
  55. RulesDefinition.Repository repo = getRepository();
  56. RulesDefinition.Rule rule = repo.rule(HotspotWithoutContextSensor.RULE_KEY);
  57. assertThat(rule.name()).isNotEmpty();
  58. assertThat(rule.securityStandards())
  59. .isNotEmpty()
  60. .containsExactlyInAnyOrder("cwe:1", "cwe:89", "cwe:123", "cwe:863", "owaspTop10:a1", "owaspTop10:a3",
  61. "owaspTop10-2021:a3", "owaspTop10-2021:a2");
  62. }
  63. @Test
  64. public void define_xoo_hotspot_rule_with_contexts() {
  65. RulesDefinition.Repository repo = getRepository();
  66. RulesDefinition.Rule rule = repo.rule(HotspotWithContextsSensor.RULE_KEY);
  67. assertThat(rule.name()).isNotEmpty();
  68. assertThat(rule.securityStandards()).isEmpty();
  69. assertThat(rule.ruleDescriptionSections()).isNotEmpty();
  70. assertThat(rule.ruleDescriptionSections().stream()
  71. .filter(rds -> rds.getKey().equals(HOW_TO_FIX_SECTION_KEY)))
  72. .allMatch(rds -> rds.getContext().isPresent());
  73. }
  74. @Test
  75. public void define_xoo_vulnerability_rule() {
  76. RulesDefinition.Repository repo = getRepository();
  77. RulesDefinition.Rule rule = repo.rule(OneVulnerabilityIssuePerModuleSensor.RULE_KEY);
  78. assertThat(rule.name()).isNotEmpty();
  79. assertThat(rule.securityStandards())
  80. .isNotEmpty()
  81. .containsExactlyInAnyOrder("cwe:250", "cwe:546", "cwe:564", "cwe:943", "owaspTop10-2021:a6", "owaspTop10-2021:a9",
  82. "owaspTop10:a10", "owaspTop10:a9");
  83. }
  84. @Test
  85. public void define_xooExternal_rules() {
  86. RulesDefinition.Repository repo = context.repository("external_XooEngine");
  87. assertThat(repo).isNotNull();
  88. assertThat(repo.name()).isEqualTo("XooEngine");
  89. assertThat(repo.language()).isEqualTo("xoo");
  90. assertThat(repo.rules()).hasSize(1);
  91. }
  92. @Test
  93. public void define_xoo2_rules() {
  94. RulesDefinition.Repository repo = context.repository("xoo2");
  95. assertThat(repo).isNotNull();
  96. assertThat(repo.name()).isEqualTo("Xoo2");
  97. assertThat(repo.language()).isEqualTo("xoo2");
  98. assertThat(repo.rules()).hasSize(2);
  99. }
  100. private RulesDefinition.Repository getRepository() {
  101. RulesDefinition.Repository repo = context.repository("xoo");
  102. assertThat(repo).isNotNull();
  103. assertThat(repo.name()).isEqualTo("Xoo");
  104. assertThat(repo.language()).isEqualTo("xoo");
  105. assertThat(repo.rules()).hasSize(26);
  106. return repo;
  107. }
  108. }