LaboratoryTokenService.java

1
package com.edtech.service;
2
3
import com.edtech.model.User;
4
import com.edtech.model.UserRole;
5
import com.edtech.repository.UserRepository;
6
import java.nio.charset.StandardCharsets;
7
import java.security.MessageDigest;
8
import java.security.NoSuchAlgorithmException;
9
import java.util.List;
10
import java.util.Optional;
11
import java.util.UUID;
12
import org.springframework.beans.factory.annotation.Value;
13
import org.springframework.stereotype.Service;
14
15
/** Javadoc. */
16
@Service
17
public class LaboratoryTokenService {
18
19
  private final UserRepository userRepository;
20
  private final String serverSecret;
21
22
  public LaboratoryTokenService(
23
      UserRepository userRepository,
24
      @Value("${jwt.secret:default-secret-key-for-lab-token-12345}") String serverSecret) {
25
    this.userRepository = userRepository;
26
    this.serverSecret = serverSecret;
27
  }
28
29
  /** Javadoc. */
30
  public String generateToken(UUID advisorId) {
31 1 1. generateToken : Replaced long division with multiplication → KILLED
    long currentWeek = System.currentTimeMillis() / (7L * 24 * 60 * 60 * 1000);
32
    String rawData = advisorId.toString() + ":" + currentWeek + ":" + serverSecret;
33
    try {
34
      MessageDigest digest = MessageDigest.getInstance("SHA-256");
35
      byte[] hash = digest.digest(rawData.getBytes(StandardCharsets.UTF_8));
36
37
      // Extrai os 6 ultimos bytes para formar o TOTP
38 2 1. generateToken : Replaced bitwise AND with OR → KILLED
2. generateToken : Replaced integer subtraction with addition → KILLED
      int offset = hash[hash.length - 1] & 0xf;
39 13 1. generateToken : Replaced integer addition with subtraction → KILLED
2. generateToken : Replaced Shift Left with Shift Right → KILLED
3. generateToken : Replaced bitwise OR with AND → KILLED
4. generateToken : Replaced bitwise AND with OR → KILLED
5. generateToken : Replaced integer addition with subtraction → KILLED
6. generateToken : Replaced Shift Left with Shift Right → KILLED
7. generateToken : Replaced bitwise AND with OR → KILLED
8. generateToken : Replaced bitwise AND with OR → KILLED
9. generateToken : Replaced bitwise OR with AND → KILLED
10. generateToken : Replaced integer addition with subtraction → KILLED
11. generateToken : Replaced Shift Left with Shift Right → KILLED
12. generateToken : Replaced bitwise OR with AND → KILLED
13. generateToken : Replaced bitwise AND with OR → KILLED
      int binary =
40
          ((hash[offset] & 0x7f) << 24)
41
              | ((hash[offset + 1] & 0xff) << 16)
42
              | ((hash[offset + 2] & 0xff) << 8)
43
              | (hash[offset + 3] & 0xff);
44
45 1 1. generateToken : Replaced integer modulus with multiplication → KILLED
      int otp = binary % 1000000;
46 1 1. generateToken : replaced return value with "" for com/edtech/service/LaboratoryTokenService::generateToken → KILLED
      return String.format("%06d", otp);
47
    } catch (NoSuchAlgorithmException e) {
48
      throw new RuntimeException("SHA-256 not found", e);
49
    }
50
  }
51
52
  /** Javadoc. */
53
  public Optional<User> findAdvisorByToken(String token) {
54
    List<User> advisors = userRepository.findByRole(UserRole.ADVISOR);
55
    for (User advisor : advisors) {
56 1 1. findAdvisorByToken : negated conditional → KILLED
      if (generateToken(advisor.getId()).equals(token)) {
57 1 1. findAdvisorByToken : replaced return value with Optional.empty for com/edtech/service/LaboratoryTokenService::findAdvisorByToken → KILLED
        return Optional.of(advisor);
58
      }
59
    }
60
    return Optional.empty();
61
  }
62
}

Mutations

31

1.1
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced long division with multiplication → KILLED

38

1.1
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testFindAdvisorByToken_Found()]
Replaced bitwise AND with OR → KILLED

2.2
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken()]
Replaced integer subtraction with addition → KILLED

39

1.1
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced integer addition with subtraction → KILLED

2.2
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced Shift Left with Shift Right → KILLED

3.3
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced bitwise OR with AND → KILLED

4.4
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced bitwise AND with OR → KILLED

5.5
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced integer addition with subtraction → KILLED

6.6
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced Shift Left with Shift Right → KILLED

7.7
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced bitwise AND with OR → KILLED

8.8
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced bitwise AND with OR → KILLED

9.9
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced bitwise OR with AND → KILLED

10.10
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced integer addition with subtraction → KILLED

11.11
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced Shift Left with Shift Right → KILLED

12.12
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced bitwise OR with AND → KILLED

13.13
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken_Deterministic()]
Replaced bitwise AND with OR → KILLED

45

1.1
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken()]
Replaced integer modulus with multiplication → KILLED

46

1.1
Location : generateToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testGenerateToken()]
replaced return value with "" for com/edtech/service/LaboratoryTokenService::generateToken → KILLED

56

1.1
Location : findAdvisorByToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testFindAdvisorByToken_Found()]
negated conditional → KILLED

57

1.1
Location : findAdvisorByToken
Killed by : com.edtech.service.LaboratoryTokenServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.LaboratoryTokenServiceTest]/[method:testFindAdvisorByToken_Found()]
replaced return value with Optional.empty for com/edtech/service/LaboratoryTokenService::findAdvisorByToken → KILLED

Active mutators

Tests examined


Report generated by PIT 1.16.1