Skip to content

Commit

Permalink
feat(ec513): integrated rule html and json
Browse files Browse the repository at this point in the history
  • Loading branch information
rgoussu-exalt committed May 29, 2024
1 parent a1ee6e5 commit cde52fa
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.ecocode.ios.swift.checks.geolocalisation;
package io.ecocode.ios.swift.checks.sobriety;

import io.ecocode.ios.swift.SwiftRuleCheck;
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
Expand All @@ -29,17 +29,15 @@ public class LocationLeakCheck extends SwiftRuleCheck {
private static final String DEFAULT_ISSUE_MESSAGE = "calls must be carefully paired: CLLocationManager.startUpdatingLocation() and CLLocationManager.stopUpdatingLocation()";
protected boolean firstCallExist = false;
protected boolean secondCallExist = false;
protected Swift5Parser.Postfix_expressionContext id;
protected Swift5Parser.ExpressionContext id;

@Override
public void apply(ParseTree tree) {

System.out.println(tree.getClass().getName());


if (tree instanceof Swift5Parser.ExpressionContext && (tree.getText().contains(".startUpdatingLocation()"))) {
firstCallExist = true;
id = (Swift5Parser.Postfix_expressionContext) tree;
id = (Swift5Parser.ExpressionContext) tree;
}

if (tree instanceof Swift5Parser.ExpressionContext && (tree.getText().contains(".stopUpdatingLocation()"))) {
Expand All @@ -48,7 +46,6 @@ public void apply(ParseTree tree) {

if (tree instanceof TerminalNodeImpl && tree.getText().equals("<EOF>")) {
if (firstCallExist && !secondCallExist) {
System.out.println("ok");
this.recordIssue(id.getStart().getStartIndex(), DEFAULT_ISSUE_MESSAGE);
}
firstCallExist = false;
Expand Down
3 changes: 2 additions & 1 deletion swift-lang/src/main/resources/ecocode_swift_profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"EC547",
"EC523",
"EC503",
"EC603"
"EC603",
"EC513"
]
}
49 changes: 49 additions & 0 deletions swift-lang/src/main/resources/io/ecocode/rules/swift/EC513.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<p>Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions.
In addition to these are the image sensor (a.k.a Camera) and the geopositioning sensor (a.k.a GPS).</p>
<br>The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor
unnecessarily process data when the app enters an idle state, typically when paused or stopped.</br>
Consequently, calls must be carefully pairwised: <code>CLLocationManager.startUpdatingLocation() and CLLocationManager.stopUpdatingLocation()</code>.</br>
Failing to do so can drain the battery in just a few hours.</p>
<h2>Noncompliant Code Example</h2>
<pre>
import CoreLocation

class LocationTracker: NSObject, CLLocationManagerDelegate {
var locationManager: CLLocationManager?

override init() {
super.init()
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestAlwaysAuthorization() // Request appropriate authorization
locationManager?.startUpdatingLocation() // Start location updates
}

// LocationManager Delegate Methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Process new locations
}
}
</pre>
<br>
<h2>Compliant Code Example</h2>
<pre>
class LocationTracker: NSObject, CLLocationManagerDelegate {
var locationManager: CLLocationManager?

override init() {
super.init()
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestAlwaysAuthorization() // Request appropriate authorization
locationManager?.startUpdatingLocation() // Start location updates only when needed
}

// LocationManager Delegate Methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Process new locations
// Possibly stop updates if they are no longer needed
locationManager?.stopUpdatingLocation()
}
}
</pre>
19 changes: 19 additions & 0 deletions swift-lang/src/main/resources/io/ecocode/rules/swift/EC513.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"key": "EC513",
"title": "Leakage: Location Leak",
"defaultSeverity": "Major",
"description": "Most iOS devices come equipped with a variety of sensors that measure motion, orientation, and various environmental conditions. In addition to these, the devices include advanced sensors such as the image sensor (commonly referred to as the Camera) and the geo-positioning sensor (commonly referred to as GPS).\n\nThe common point of all these sensors is that they are power-intensive while in use. A typical issue arises when these sensors continue to process data unnecessarily after the application enters an idle state, typically when paused or when the user stops interacting with it.\n\nConsequently, calls must be carefully paired: CLLocationManager.startUpdatingLocation() and CLLocationManager.stopUpdatingLocation(). Failing to do so can drain the battery in just a few hours.",
"status": "ready",
"remediation": {
"func": "Constant/Issue",
"constantCost": "20min"
},
"tags": [
"sobriety",
"environment",
"ecocode",
"eco-design"
],
"type": "CODE_SMELL"
}

Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public void testMetadata() {
}

@Test
public void testRegisteredRules() {
assertThat(repository.rules()).hasSize(10);
public void testRegistredRules() {
assertThat(repository.rules()).hasSize(11);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void locationLeakCheck_missing_release_trigger(){
SensorContextTester context = CheckTestHelper.analyzeTestFile(TEST_CASE_MISSING_RELEASE_CALL);
ObjectAssert<Issue> issue = assertThat(context.allIssues()).hasSize(1)
.first();
issue.extracting(Issue::ruleKey).isEqualTo("EC513");
issue.extracting(Issue::ruleKey).extracting(RuleKey::rule).isEqualTo("EC513");
issue.extracting(Issue::ruleKey).extracting(RuleKey::repository)
.isEqualTo("ecoCode-swift");
issue.extracting(Issue::primaryLocation)
Expand Down

0 comments on commit cde52fa

Please sign in to comment.