Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVA] CRJVM207 - 10 MILLIARDS - Customer data must have end-of-life information #170

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package fr.greencodeinitiative.java.checks;
import java.util.*;


import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.java.model.declaration.VariableTreeImpl;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.*;
saidmohamedali marked this conversation as resolved.
Show resolved Hide resolved
import org.sonar.plugins.java.api.tree.Tree.Kind;


@Rule(
key = "CRJVM207-JAVA",
name = "Developpement",
description = CookieWithoutExpirationRule.MESSAGERULE,
priority = Priority.MINOR,
tags = {"smell"})

public class CookieWithoutExpirationRule extends IssuableSubscriptionVisitor {

protected static final String MESSAGERULE = "Customer data must have end-of-life information, so cookies must have a maxAge";


@Override
public List<Tree.Kind> nodesToVisit() {
return Collections.singletonList(Tree.Kind.CLASS);
}
private final CookieWithoutExpirationRuleCheckVisitor visitorInFile = new CookieWithoutExpirationRuleCheckVisitor();


@Override
public void visitNode(Tree tree) {

tree.accept(visitorInFile);
//Class visitor
if (visitorInFile.hasANewCookieWithoutMaxDate())
{
//if we found a cookie that maxDate is not initialized, we report issue
reportIssue(tree, "MaxAge must have a value");
}
}

private class CookieWithoutExpirationRuleCheckVisitor extends BaseTreeVisitor {


// storage of variable name for setMaxAge
private ArrayList<String> hasSetMaxAgeForCookiesVariableName = new ArrayList<>();
// storage of variable name for New Cookies
private ArrayList<String> newCookieVariableName = new ArrayList<>();
@Override
public void visitReturnStatement(ReturnStatementTree tree) {
this.scan(tree.expression());
}

@Override
public void visitVariable(VariableTree tree) {
//when we visit variable affectation
for (Tree children : ((VariableTreeImpl) tree).children())
{
//if we found an affectation
if (children.is(Tree.Kind.NEW_CLASS)
&& ((IdentifierTree)((NewClassTree)children).identifier()).toString().equals("Cookie"))
{
//if this is a New Cookie affectation, we store the name of the variable
this.newCookieVariableName.add(tree.simpleName().toString());
}
else
super.visitVariable(tree);
}
}


public boolean hasANewCookieWithoutMaxDate()
saidmohamedali marked this conversation as resolved.
Show resolved Hide resolved
{
//loop on variables on which a new Cookie was done
for (String variableName : newCookieVariableName )
{
if (!hasSetMaxAgeForCookiesVariableName.contains(variableName))
//no setMaxAge has been done
return true;
}
return false;
}
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
//Method visiting
if (tree.methodSelect().is(Kind.MEMBER_SELECT) &&
(((MemberSelectExpressionTree)tree.methodSelect()).identifier().name().equals("setMaxAge"))
)
{
//if visited methode is setMaxAge, then save the assigned variable
hasSetMaxAgeForCookiesVariableName.add(((MemberSelectExpressionTree)tree.methodSelect()).expression().toString());
}

}


}
}
34 changes: 34 additions & 0 deletions java-plugin/src/test/files/CookieWithoutExpirationCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

class TestClass {// Noncompliant {{MaxAge must have a value}}

public void NOK_CookieCreation() {
// create objet cookie
Cookie A = new Cookie("id","674684641");
}

public Cookie NOK_ReturnCookieCreation() {
// create objet cookie
Cookie A = new Cookie("id","674684641");
return A;
}
public void OK_CookieCreation() {
// create objet cookie
Cookie B = new Cookie("id","674684641");
// set the validity
B.setMaxAge(24*3600);

}

public void OK_CookieCreation2() {
// create objet cookie
Cookie C;
C = new Cookie("id","674684641");
// set the validity
C.setMaxAge(24*3600);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fr.greencodeinitiative.java.checks;
saidmohamedali marked this conversation as resolved.
Show resolved Hide resolved

import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
import org.sonar.plugins.java.api.JavaFileScanner;

class CookieWithExpirationCheckTest {

/**
* @formatter:off
*/
@Test
void test() {
CheckVerifier.newVerifier()
.onFile("src/test/files/CookieWithoutExpirationCheck.java")
.withCheck(new CookieWithoutExpirationRule())
.verifyIssues();
}

}
Loading