| 1 | package model.utente; | |
| 2 | ||
| 3 | import model.DAOInterface; | |
| 4 | import model.DBConnection; | |
| 5 | import model.security.CryptoKeyProvider; | |
| 6 | import model.security.CryptoUtils; | |
| 7 | import org.mindrot.jbcrypt.BCrypt; | |
| 8 | ||
| 9 | import javax.crypto.SecretKey; | |
| 10 | import javax.sql.DataSource; | |
| 11 | import java.security.GeneralSecurityException; | |
| 12 | import java.sql.*; | |
| 13 | import java.time.LocalDate; | |
| 14 | import java.util.ArrayList; | |
| 15 | import java.util.Collection; | |
| 16 | ||
| 17 | public class UtenteDAO implements DAOInterface<UtenteBean, String> { | |
| 18 | ||
| 19 | private static final String TABLE_NAME = "Utente"; | |
| 20 | private static SecretKey key; | |
| 21 | private static DataSource ds; | |
| 22 | ||
| 23 | public UtenteDAO() { | |
| 24 | ds = DBConnection.getDataSource(); | |
| 25 | key = CryptoKeyProvider.getKey(); | |
| 26 | } | |
| 27 | ||
| 28 | public UtenteDAO(DataSource ds, SecretKey keyProvider) { | |
| 29 | UtenteDAO.ds = ds; | |
| 30 | UtenteDAO.key = keyProvider; | |
| 31 | } | |
| 32 | ||
| 33 | @Override | |
| 34 | public UtenteBean doRetrieveByKey(String code) throws SQLException { | |
| 35 | UtenteBean user = new UtenteBean(); | |
| 36 | String query = "SELECT * FROM " + TABLE_NAME + " WHERE username = ?"; | |
| 37 |
1
1. doRetrieveByKey : replaced return value with null for model/utente/UtenteDAO::doRetrieveByKey → KILLED |
return getUtenteBean(code, user, query); |
| 38 | } | |
| 39 | ||
| 40 | @Override | |
| 41 | public Collection<UtenteBean> doRetriveAll(String order) throws SQLException { | |
| 42 | Collection<UtenteBean> users = new ArrayList<>(); | |
| 43 | String query = "SELECT * FROM " + TABLE_NAME; | |
| 44 | ||
| 45 | try (Connection connection = ds.getConnection(); | |
| 46 | PreparedStatement preparedStatement = connection.prepareStatement(query)) { | |
| 47 | ||
| 48 | ResultSet resultSet = preparedStatement.executeQuery(); | |
| 49 |
1
1. doRetriveAll : negated conditional → TIMED_OUT |
while (resultSet.next()) { |
| 50 | UtenteBean user = new UtenteBean(); | |
| 51 |
1
1. doRetriveAll : removed call to model/utente/UtenteDAO::setUtente → KILLED |
setUtente(resultSet, user); |
| 52 | users.add(user); | |
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 |
1
1. doRetriveAll : replaced return value with Collections.emptyList for model/utente/UtenteDAO::doRetriveAll → KILLED |
return users; |
| 57 | } | |
| 58 | ||
| 59 | public UtenteBean doRetrieveByEmail(String email) throws SQLException { | |
| 60 | UtenteBean user = new UtenteBean(); | |
| 61 | String query = "SELECT * FROM " + TABLE_NAME + " WHERE email = ?"; | |
| 62 |
1
1. doRetrieveByEmail : replaced return value with null for model/utente/UtenteDAO::doRetrieveByEmail → KILLED |
return getUtenteBean(email, user, query); |
| 63 | } | |
| 64 | ||
| 65 | @Override | |
| 66 | public synchronized void doSave(UtenteBean u) throws SQLException { | |
| 67 | String query = "INSERT INTO " + TABLE_NAME + | |
| 68 | " (username, pwd, nome, cognome, email, dataNascita, nomeCarta, cognomeCarta, numCarta, dataScadenza, CVV, cap, via, citta, tipo) " + | |
| 69 | "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; | |
| 70 | ||
| 71 | try (Connection c = ds.getConnection(); | |
| 72 | PreparedStatement ps = c.prepareStatement(query)) { | |
| 73 | ||
| 74 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(1, u.getUsername()); |
| 75 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(2, BCrypt.hashpw(u.getPwd(), BCrypt.gensalt())); |
| 76 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(3, u.getNome()); |
| 77 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(4, u.getCognome()); |
| 78 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(5, u.getEmail()); |
| 79 |
1
1. doSave : removed call to java/sql/PreparedStatement::setDate → KILLED |
ps.setDate(6, Date.valueOf(u.getDataNascita())); |
| 80 | ||
| 81 | try { | |
| 82 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(7, encryptOrNull(key, u.getNomeCarta())); |
| 83 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(8, encryptOrNull(key, u.getCognomeCarta())); |
| 84 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(9, encryptOrNull(key, u.getNumCarta())); |
| 85 |
2
1. doSave : negated conditional → KILLED 2. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(10, encryptOrNull(key, u.getDataScadenza() == null ? null : u.getDataScadenza().toString())); |
| 86 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(11, encryptOrNull(key, u.getCVV())); |
| 87 | } catch (Exception e) { | |
| 88 | throw new SQLException("Encryption error", e); | |
| 89 | } | |
| 90 | ||
| 91 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(12, u.getCap()); |
| 92 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(13, u.getVia()); |
| 93 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(14, u.getCitta()); |
| 94 |
1
1. doSave : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(15, u.getTipo()); |
| 95 | ||
| 96 | ps.executeUpdate(); | |
| 97 | } | |
| 98 | } | |
| 99 | ||
| 100 | @Override | |
| 101 | public synchronized void doUpdate(UtenteBean u) throws SQLException { | |
| 102 | String query = "UPDATE " + TABLE_NAME + | |
| 103 | " SET pwd = ?, nome = ?, cognome = ?, email = ?, dataNascita = ?, " + | |
| 104 | "numCarta = ?, dataScadenza = ?, CVV = ?, nomeCarta = ?, cognomeCarta = ?, " + | |
| 105 | "cap = ?, via = ?, citta = ?, tipo = ? WHERE username = ?"; | |
| 106 | ||
| 107 | try (Connection c = ds.getConnection(); | |
| 108 | PreparedStatement ps = c.prepareStatement(query)) { | |
| 109 | ||
| 110 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(1, BCrypt.hashpw(u.getPwd(), BCrypt.gensalt())); |
| 111 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(2, u.getNome()); |
| 112 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(3, u.getCognome()); |
| 113 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(4, u.getEmail()); |
| 114 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setDate → KILLED |
ps.setDate(5, Date.valueOf(u.getDataNascita())); |
| 115 | ||
| 116 | try { | |
| 117 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(6, encryptOrNull(key, u.getNumCarta())); |
| 118 |
2
1. doUpdate : negated conditional → KILLED 2. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(7, encryptOrNull(key, u.getDataScadenza() == null ? null : u.getDataScadenza().toString())); |
| 119 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(8, encryptOrNull(key, u.getCVV())); |
| 120 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(9, encryptOrNull(key, u.getNomeCarta())); |
| 121 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(10, encryptOrNull(key, u.getCognomeCarta())); |
| 122 | } catch (Exception e) { | |
| 123 | throw new SQLException("Encryption error", e); | |
| 124 | } | |
| 125 | ||
| 126 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(11, u.getCap()); |
| 127 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(12, u.getVia()); |
| 128 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(13, u.getCitta()); |
| 129 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(14, u.getTipo()); |
| 130 |
1
1. doUpdate : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(15, u.getUsername()); |
| 131 | ||
| 132 | ps.executeUpdate(); | |
| 133 | } | |
| 134 | } | |
| 135 | ||
| 136 | @Override | |
| 137 | public boolean doDelete(String code) throws SQLException { | |
| 138 | try (Connection c = ds.getConnection(); | |
| 139 | PreparedStatement ps = c.prepareStatement("DELETE FROM " + TABLE_NAME + " WHERE username = ?")) { | |
| 140 | ||
| 141 |
1
1. doDelete : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(1, code); |
| 142 |
3
1. doDelete : replaced boolean return with false for model/utente/UtenteDAO::doDelete → KILLED 2. doDelete : replaced boolean return with true for model/utente/UtenteDAO::doDelete → KILLED 3. doDelete : negated conditional → KILLED |
return ps.executeUpdate() != 0; |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | private UtenteBean getUtenteBean(String code, UtenteBean user, String query) throws SQLException { | |
| 147 | boolean found; | |
| 148 | ||
| 149 | try (Connection c = ds.getConnection(); | |
| 150 | PreparedStatement ps = c.prepareStatement(query)) { | |
| 151 | ||
| 152 |
1
1. getUtenteBean : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(1, code); |
| 153 | ResultSet rs = ps.executeQuery(); | |
| 154 | ||
| 155 | found = rs.isBeforeFirst(); | |
| 156 |
1
1. getUtenteBean : negated conditional → KILLED |
if (found) { |
| 157 | rs.next(); | |
| 158 |
1
1. getUtenteBean : removed call to model/utente/UtenteDAO::setUtente → KILLED |
setUtente(rs, user); |
| 159 | } | |
| 160 | } | |
| 161 |
1
1. getUtenteBean : negated conditional → KILLED |
return found ? user : null; |
| 162 | } | |
| 163 | ||
| 164 | private void setUtente(ResultSet rs, UtenteBean u) throws SQLException { | |
| 165 |
1
1. setUtente : removed call to model/utente/UtenteBean::setUsername → KILLED |
u.setUsername(rs.getString("username")); |
| 166 |
1
1. setUtente : removed call to model/utente/UtenteBean::setPwd → KILLED |
u.setPwd(rs.getString("pwd")); |
| 167 |
1
1. setUtente : removed call to model/utente/UtenteBean::setNome → KILLED |
u.setNome(rs.getString("nome")); |
| 168 |
1
1. setUtente : removed call to model/utente/UtenteBean::setCognome → KILLED |
u.setCognome(rs.getString("cognome")); |
| 169 |
1
1. setUtente : removed call to model/utente/UtenteBean::setEmail → KILLED |
u.setEmail(rs.getString("email")); |
| 170 | ||
| 171 | Date birth = rs.getDate("dataNascita"); | |
| 172 |
2
1. setUtente : removed call to model/utente/UtenteBean::setDataNascita → KILLED 2. setUtente : negated conditional → KILLED |
if (birth != null) u.setDataNascita(birth.toLocalDate()); |
| 173 | ||
| 174 | try { | |
| 175 |
1
1. setUtente : removed call to model/utente/UtenteBean::setNomeCarta → KILLED |
u.setNomeCarta(decryptOrNull(key, rs.getString("nomeCarta"))); |
| 176 |
1
1. setUtente : removed call to model/utente/UtenteBean::setCognomeCarta → KILLED |
u.setCognomeCarta(decryptOrNull(key, rs.getString("cognomeCarta"))); |
| 177 |
1
1. setUtente : removed call to model/utente/UtenteBean::setNumCarta → KILLED |
u.setNumCarta(decryptOrNull(key, rs.getString("numCarta"))); |
| 178 |
1
1. setUtente : removed call to model/utente/UtenteBean::setCVV → KILLED |
u.setCVV(decryptOrNull(key, rs.getString("CVV"))); |
| 179 | ||
| 180 | String exp = rs.getString("dataScadenza"); | |
| 181 |
2
1. setUtente : negated conditional → KILLED 2. setUtente : removed call to model/utente/UtenteBean::setDataScadenza → KILLED |
u.setDataScadenza(exp == null ? null : LocalDate.parse(decryptOrNull(key, exp))); |
| 182 | ||
| 183 | } catch (Exception e) { | |
| 184 | throw new SQLException("Decryption error", e); | |
| 185 | } | |
| 186 | ||
| 187 |
1
1. setUtente : removed call to model/utente/UtenteBean::setCap → KILLED |
u.setCap(rs.getString("cap")); |
| 188 |
1
1. setUtente : removed call to model/utente/UtenteBean::setVia → KILLED |
u.setVia(rs.getString("via")); |
| 189 |
1
1. setUtente : removed call to model/utente/UtenteBean::setCitta → KILLED |
u.setCitta(rs.getString("citta")); |
| 190 |
1
1. setUtente : removed call to model/utente/UtenteBean::setTipo → KILLED |
u.setTipo(rs.getString("tipo")); |
| 191 | } | |
| 192 | ||
| 193 | private String encryptOrNull(SecretKey key, String v) throws GeneralSecurityException { | |
| 194 |
3
1. encryptOrNull : negated conditional → KILLED 2. encryptOrNull : negated conditional → KILLED 3. encryptOrNull : replaced return value with "" for model/utente/UtenteDAO::encryptOrNull → KILLED |
return (v == null || v.isEmpty()) ? null : CryptoUtils.encrypt(key, v); |
| 195 | } | |
| 196 | ||
| 197 | private String decryptOrNull(SecretKey key, String v) throws GeneralSecurityException { | |
| 198 |
2
1. decryptOrNull : negated conditional → KILLED 2. decryptOrNull : replaced return value with "" for model/utente/UtenteDAO::decryptOrNull → KILLED |
return (v == null) ? null : CryptoUtils.decrypt(key, v); |
| 199 | } | |
| 200 | } | |
| 201 | ||
Mutations | ||
| 37 |
1.1 |
|
| 49 |
1.1 |
|
| 51 |
1.1 |
|
| 56 |
1.1 |
|
| 62 |
1.1 |
|
| 74 |
1.1 |
|
| 75 |
1.1 |
|
| 76 |
1.1 |
|
| 77 |
1.1 |
|
| 78 |
1.1 |
|
| 79 |
1.1 |
|
| 82 |
1.1 |
|
| 83 |
1.1 |
|
| 84 |
1.1 |
|
| 85 |
1.1 2.2 |
|
| 86 |
1.1 |
|
| 91 |
1.1 |
|
| 92 |
1.1 |
|
| 93 |
1.1 |
|
| 94 |
1.1 |
|
| 110 |
1.1 |
|
| 111 |
1.1 |
|
| 112 |
1.1 |
|
| 113 |
1.1 |
|
| 114 |
1.1 |
|
| 117 |
1.1 |
|
| 118 |
1.1 2.2 |
|
| 119 |
1.1 |
|
| 120 |
1.1 |
|
| 121 |
1.1 |
|
| 126 |
1.1 |
|
| 127 |
1.1 |
|
| 128 |
1.1 |
|
| 129 |
1.1 |
|
| 130 |
1.1 |
|
| 141 |
1.1 |
|
| 142 |
1.1 2.2 3.3 |
|
| 152 |
1.1 |
|
| 156 |
1.1 |
|
| 158 |
1.1 |
|
| 161 |
1.1 |
|
| 165 |
1.1 |
|
| 166 |
1.1 |
|
| 167 |
1.1 |
|
| 168 |
1.1 |
|
| 169 |
1.1 |
|
| 172 |
1.1 2.2 |
|
| 175 |
1.1 |
|
| 176 |
1.1 |
|
| 177 |
1.1 |
|
| 178 |
1.1 |
|
| 181 |
1.1 2.2 |
|
| 187 |
1.1 |
|
| 188 |
1.1 |
|
| 189 |
1.1 |
|
| 190 |
1.1 |
|
| 194 |
1.1 2.2 3.3 |
|
| 198 |
1.1 2.2 |