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, UserRole targetRole) {
31 1 1. generateToken : Replaced long division with multiplication → KILLED
    long currentWeek = System.currentTimeMillis() / (7L * 24 * 60 * 60 * 1000);
32
    String rawData =
33
        advisorId.toString() + ":" + currentWeek + ":" + serverSecret + ":" + targetRole.name();
34
    try {
35
      MessageDigest digest = MessageDigest.getInstance("SHA-256");
36
      byte[] hash = digest.digest(rawData.getBytes(StandardCharsets.UTF_8));
37
38
      // Extrai os 6 ultimos bytes para formar o TOTP
39 2 1. generateToken : Replaced bitwise AND with OR → KILLED
2. generateToken : Replaced integer subtraction with addition → KILLED
      int offset = hash[hash.length - 1] & 0xf;
40 13 1. generateToken : Replaced integer addition with subtraction → KILLED
2. generateToken : Replaced bitwise OR with AND → KILLED
3. generateToken : Replaced Shift Left with Shift Right → 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 OR with AND → KILLED
9. generateToken : Replaced bitwise AND with OR → KILLED
10. generateToken : Replaced integer addition with subtraction → KILLED
11. generateToken : Replaced bitwise OR with AND → KILLED
12. generateToken : Replaced Shift Left with Shift Right → KILLED
13. generateToken : Replaced bitwise AND with OR → KILLED
      int binary =
41
          ((hash[offset] & 0x7f) << 24)
42
              | ((hash[offset + 1] & 0xff) << 16)
43
              | ((hash[offset + 2] & 0xff) << 8)
44
              | (hash[offset + 3] & 0xff);
45
46 1 1. generateToken : Replaced integer modulus with multiplication → KILLED
      int otp = binary % 1000000;
47 1 1. generateToken : replaced return value with "" for com/edtech/service/LaboratoryTokenService::generateToken → KILLED
      return String.format("%06d", otp);
48
    } catch (NoSuchAlgorithmException e) {
49
      throw new RuntimeException("SHA-256 not found", e);
50
    }
51
  }
52
53
  /** Javadoc. */
54
  public Optional<User> findAdvisorByToken(String token, UserRole targetRole) {
55
    List<User> advisors = userRepository.findByRole(UserRole.ADVISOR);
56
    for (User advisor : advisors) {
57 1 1. findAdvisorByToken : negated conditional → KILLED
      if (generateToken(advisor.getId(), targetRole).equals(token)) {
58 1 1. findAdvisorByToken : replaced return value with Optional.empty for com/edtech/service/LaboratoryTokenService::findAdvisorByToken → KILLED
        return Optional.of(advisor);
59
      }
60
    }
61
    return Optional.empty();
62
  }
63
}

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

39

1.1
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

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

40

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 bitwise OR with AND → KILLED

3.3
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

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 OR with AND → KILLED

9.9
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

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 bitwise OR with AND → KILLED

12.12
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

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

46

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

47

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

57

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

58

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.25.9 support