ProxyUtil.java

1
package com.jsql.util;
2
3
import com.jsql.model.InjectionModel;
4
import com.jsql.util.GitUtil.ShowOnConsole;
5
import org.apache.commons.lang3.StringUtils;
6
import org.apache.logging.log4j.LogManager;
7
import org.apache.logging.log4j.Logger;
8
9
import java.net.Socket;
10
import java.util.Optional;
11
import java.util.prefs.Preferences;
12
13
/**
14
 * Utility class managing proxy settings.
15
 * The proxy configuration is saved as preferences and applied to the JVM.
16
 */
17
public class ProxyUtil {
18
    
19
    /**
20
     * Log4j logger sent to view.
21
     */
22
    private static final Logger LOGGER = LogManager.getRootLogger();
23
    
24
    /**
25
     * Proxy IP address or name.
26
     */
27
    private String proxyAddressHttp;
28
    private String proxyAddressHttps;
29
30
    /**
31
     * Proxy port number.
32
     */
33
    private String proxyPortHttp;
34
    private String proxyPortHttps;
35
    
36
    /**
37
     * True if connection is proxified.
38
     */
39
    private boolean isUsingProxyHttp = false;
40
    private boolean isUsingProxyHttps = false;
41
    
42
    private static final String PROPERTIES_HTTP_PROXY_HOST = "http.proxyHost";
43
    private static final String PROPERTIES_HTTP_PROXY_PORT = "http.proxyPort";
44
    private static final String PROPERTIES_HTTPS_PROXY_HOST = "https.proxyHost";
45
    private static final String PROPERTIES_HTTPS_PROXY_PORT = "https.proxyPort";
46
    
47
    /**
48
     * Save proxy configuration into the JVM preferences.
49
     * @param isUsingProxyHttp whether the connection is using a proxy
50
     * @param proxyAddressHttp IP address or name of the proxy
51
     * @param proxyPortHttp port number of proxy
52
     */
53
    public void setPreferences(
54
        boolean isUsingProxyHttp, String proxyAddressHttp, String proxyPortHttp,
55
        boolean isUsingProxyHttps, String proxyAddressHttps, String proxyPortHttps
56
    ) {
57
        // Set the application proxy settings
58 1 1. setPreferences : removed call to com/jsql/util/ProxyUtil::setUsingProxyHttp → NO_COVERAGE
        this.setUsingProxyHttp(isUsingProxyHttp);
59 1 1. setPreferences : removed call to com/jsql/util/ProxyUtil::setProxyAddressHttp → NO_COVERAGE
        this.setProxyAddressHttp(proxyAddressHttp);
60 1 1. setPreferences : removed call to com/jsql/util/ProxyUtil::setProxyPortHttp → NO_COVERAGE
        this.setProxyPortHttp(proxyPortHttp);
61
        
62 1 1. setPreferences : removed call to com/jsql/util/ProxyUtil::setUsingProxyHttps → NO_COVERAGE
        this.setUsingProxyHttps(isUsingProxyHttps);
63 1 1. setPreferences : removed call to com/jsql/util/ProxyUtil::setProxyAddressHttps → NO_COVERAGE
        this.setProxyAddressHttps(proxyAddressHttps);
64 1 1. setPreferences : removed call to com/jsql/util/ProxyUtil::setProxyPortHttps → NO_COVERAGE
        this.setProxyPortHttps(proxyPortHttps);
65
66
        // Save the settings in the JVM
67
        Preferences prefs = Preferences.userRoot().node(InjectionModel.class.getName());
68 1 1. setPreferences : removed call to java/util/prefs/Preferences::putBoolean → NO_COVERAGE
        prefs.putBoolean("isUsingProxy", this.isUsingProxyHttp());
69 1 1. setPreferences : removed call to java/util/prefs/Preferences::put → NO_COVERAGE
        prefs.put("proxyAddress", this.getProxyAddressHttp());
70 1 1. setPreferences : removed call to java/util/prefs/Preferences::put → NO_COVERAGE
        prefs.put("proxyPort", this.getProxyPortHttp());
71
        
72 1 1. setPreferences : removed call to java/util/prefs/Preferences::putBoolean → NO_COVERAGE
        prefs.putBoolean("isUsingProxyHttps", this.isUsingProxyHttps());
73 1 1. setPreferences : removed call to java/util/prefs/Preferences::put → NO_COVERAGE
        prefs.put("proxyAddressHttps", this.getProxyAddressHttps());
74 1 1. setPreferences : removed call to java/util/prefs/Preferences::put → NO_COVERAGE
        prefs.put("proxyPortHttps", this.getProxyPortHttps());
75
76
        // Change the JVM configuration
77 1 1. setPreferences : negated conditional → NO_COVERAGE
        if (this.isUsingProxyHttp()) {
78
            System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_HOST, this.getProxyAddressHttp());
79
            System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_PORT, this.getProxyPortHttp());
80
        } else {
81
            System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_HOST, StringUtils.EMPTY);
82
            System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_PORT, StringUtils.EMPTY);
83
        }
84 1 1. setPreferences : negated conditional → NO_COVERAGE
        if (this.isUsingProxyHttps()) {
85
            System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_HOST, this.getProxyAddressHttps());
86
            System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_PORT, this.getProxyPortHttps());
87
        } else {
88
            System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_HOST, StringUtils.EMPTY);
89
            System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_PORT, StringUtils.EMPTY);
90
        }
91
    }
92
    
93
    /**
94
     * Initialize proxy information from JVM already saved preferences.
95
     */
96
    public void initProxy() {
97
        // Use Preferences API to persist proxy configuration
98
        var preferences = Preferences.userRoot().node(InjectionModel.class.getName());
99
100
        // Default proxy disabled
101 1 1. initProxy : removed call to com/jsql/util/ProxyUtil::setUsingProxyHttp → NO_COVERAGE
        this.setUsingProxyHttp(preferences.getBoolean("isUsingProxy", false));
102 1 1. initProxy : removed call to com/jsql/util/ProxyUtil::setUsingProxyHttps → NO_COVERAGE
        this.setUsingProxyHttps(preferences.getBoolean("isUsingProxyHttps", false));
103
104
        // Default proxy config
105 1 1. initProxy : removed call to com/jsql/util/ProxyUtil::setProxyAddressHttp → NO_COVERAGE
        this.setProxyAddressHttp(preferences.get("proxyAddress", "127.0.0.1"));
106 1 1. initProxy : removed call to com/jsql/util/ProxyUtil::setProxyPortHttp → NO_COVERAGE
        this.setProxyPortHttp(preferences.get("proxyPort", "8118"));
107
        
108 1 1. initProxy : removed call to com/jsql/util/ProxyUtil::setProxyAddressHttps → NO_COVERAGE
        this.setProxyAddressHttps(preferences.get("proxyAddressHttps", "127.0.0.1"));
109 1 1. initProxy : removed call to com/jsql/util/ProxyUtil::setProxyPortHttps → NO_COVERAGE
        this.setProxyPortHttps(preferences.get("proxyPortHttps", "8118"));
110
        
111
        // Change the JVM configuration
112 1 1. initProxy : negated conditional → NO_COVERAGE
        if (this.isUsingProxyHttp()) {
113
            System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_HOST, this.getProxyAddressHttp());
114
            System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_PORT, this.getProxyPortHttp());
115
        }
116 1 1. initProxy : negated conditional → NO_COVERAGE
        if (this.isUsingProxyHttps()) {
117
            System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_HOST, this.getProxyAddressHttps());
118
            System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_PORT, this.getProxyPortHttps());
119
        }
120
    }
121
    
122
    /**
123
     * Check if enabled proxies are up when application starts,
124
     * injection begins,- checking IP, sending reports.
125
     * Display logs except when sending unhandled exception.
126
     * @param showOnConsole whether the message should be presented to the user
127
     * @return true if enabled proxies are up
128
     */
129
    public boolean isNotLive(ShowOnConsole showOnConsole) {
130
        var isLive = true;
131
        if (
132 1 1. isNotLive : negated conditional → NO_COVERAGE
            this.isUsingProxyHttp()
133 1 1. isNotLive : negated conditional → NO_COVERAGE
            && StringUtils.isNotEmpty(this.getProxyAddressHttp())
134 1 1. isNotLive : negated conditional → NO_COVERAGE
            && StringUtils.isNotEmpty(this.getProxyPortHttp())
135
        ) {
136
            isLive = this.isSocketOn(showOnConsole, this.getProxyAddressHttp(), this.getProxyPortHttp(), "HTTP");
137
        }
138
        if (
139 1 1. isNotLive : negated conditional → NO_COVERAGE
            this.isUsingProxyHttps()
140 1 1. isNotLive : negated conditional → NO_COVERAGE
            && StringUtils.isNotEmpty(this.getProxyAddressHttps())
141 1 1. isNotLive : negated conditional → NO_COVERAGE
            && StringUtils.isNotEmpty(this.getProxyPortHttps())
142
        ) {
143
            isLive = this.isSocketOn(showOnConsole, this.getProxyAddressHttps(), this.getProxyPortHttps(), "HTTPS");
144
        }
145 2 1. isNotLive : negated conditional → NO_COVERAGE
2. isNotLive : replaced boolean return with true for com/jsql/util/ProxyUtil::isNotLive → NO_COVERAGE
        return !isLive;
146
    }
147
    
148
    private boolean isSocketOn(ShowOnConsole showOnConsole, String address, String port, String protocol) {
149
        var isSocketOn = true;
150
        try {
151
            var socket = new Socket(address, Integer.parseInt(port));
152 1 1. isSocketOn : removed call to java/net/Socket::close → NO_COVERAGE
            socket.close();
153 1 1. isSocketOn : removed call to com/jsql/util/ProxyUtil::logStatus → NO_COVERAGE
            this.logStatus(showOnConsole, address, port, protocol);
154
        } catch (Exception e) {
155
            isSocketOn = false;
156 1 1. isSocketOn : removed call to com/jsql/util/ProxyUtil::logStatus → NO_COVERAGE
            this.logStatus(showOnConsole, address, port, protocol, e);
157
        }
158 2 1. isSocketOn : replaced boolean return with false for com/jsql/util/ProxyUtil::isSocketOn → NO_COVERAGE
2. isSocketOn : replaced boolean return with true for com/jsql/util/ProxyUtil::isSocketOn → NO_COVERAGE
        return isSocketOn;
159
    }
160
    
161
    private void logStatus(ShowOnConsole showOnConsole, String address, String port, String protocol) {
162 1 1. logStatus : negated conditional → NO_COVERAGE
        if (showOnConsole == ShowOnConsole.YES) {
163
            LOGGER.log(
164
                LogLevelUtil.CONSOLE_SUCCESS,
165
                "Connection successful to {} proxy {}:{}",
166 1 1. lambda$logStatus$0 : replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$0 → NO_COVERAGE
                () -> protocol,
167 1 1. lambda$logStatus$1 : replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$1 → NO_COVERAGE
                () -> address,
168 1 1. lambda$logStatus$2 : replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$2 → NO_COVERAGE
                () ->port
169
            );
170
        }
171
    }
172
    
173
    private void logStatus(ShowOnConsole showOnConsole, String address, String port, String protocol, Exception e) {
174 1 1. logStatus : negated conditional → NO_COVERAGE
        if (showOnConsole == ShowOnConsole.YES) {
175
            String message = Optional.ofNullable(e.getMessage()).orElse(StringUtils.EMPTY);
176
            LOGGER.log(
177
                LogLevelUtil.CONSOLE_ERROR,
178 1 1. lambda$logStatus$3 : replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$3 → NO_COVERAGE
                () -> String.format(
179
                    "Connection to %s proxy %s:%s failed, verify your proxy settings: %s",
180
                    protocol,
181
                    address,
182
                    port,
183
                    message.replace(e.getClass().getName() +": ", StringUtils.EMPTY)
184
                )
185
            );
186
        }
187
    }
188
    
189
    
190
    // Getters and setters
191
    
192
    public String getProxyAddressHttp() {
193 1 1. getProxyAddressHttp : replaced return value with "" for com/jsql/util/ProxyUtil::getProxyAddressHttp → NO_COVERAGE
        return this.proxyAddressHttp;
194
    }
195
196
    public void setProxyAddressHttp(String proxyAddressHttp) {
197
        this.proxyAddressHttp = proxyAddressHttp;
198
    }
199
200
    public String getProxyPortHttp() {
201 1 1. getProxyPortHttp : replaced return value with "" for com/jsql/util/ProxyUtil::getProxyPortHttp → NO_COVERAGE
        return this.proxyPortHttp;
202
    }
203
204
    public void setProxyPortHttp(String proxyPortHttp) {
205
        this.proxyPortHttp = proxyPortHttp;
206
    }
207
208
    public boolean isUsingProxyHttp() {
209 2 1. isUsingProxyHttp : replaced boolean return with false for com/jsql/util/ProxyUtil::isUsingProxyHttp → NO_COVERAGE
2. isUsingProxyHttp : replaced boolean return with true for com/jsql/util/ProxyUtil::isUsingProxyHttp → NO_COVERAGE
        return this.isUsingProxyHttp;
210
    }
211
212
    public void setUsingProxyHttp(boolean isUsingProxyHttp) {
213
        this.isUsingProxyHttp = isUsingProxyHttp;
214
    }
215
216
    public String getProxyAddressHttps() {
217 1 1. getProxyAddressHttps : replaced return value with "" for com/jsql/util/ProxyUtil::getProxyAddressHttps → NO_COVERAGE
        return this.proxyAddressHttps;
218
    }
219
220
    public void setProxyAddressHttps(String proxyAddressHttps) {
221
        this.proxyAddressHttps = proxyAddressHttps;
222
    }
223
224
    public String getProxyPortHttps() {
225 1 1. getProxyPortHttps : replaced return value with "" for com/jsql/util/ProxyUtil::getProxyPortHttps → NO_COVERAGE
        return this.proxyPortHttps;
226
    }
227
228
    public void setProxyPortHttps(String proxyPortHttps) {
229
        this.proxyPortHttps = proxyPortHttps;
230
    }
231
232
    public boolean isUsingProxyHttps() {
233 2 1. isUsingProxyHttps : replaced boolean return with true for com/jsql/util/ProxyUtil::isUsingProxyHttps → NO_COVERAGE
2. isUsingProxyHttps : replaced boolean return with false for com/jsql/util/ProxyUtil::isUsingProxyHttps → NO_COVERAGE
        return this.isUsingProxyHttps;
234
    }
235
236
    public void setUsingProxyHttps(boolean isUsingProxyHttps) {
237
        this.isUsingProxyHttps = isUsingProxyHttps;
238
    }
239
}

Mutations

58

1.1
Location : setPreferences
Killed by : none
removed call to com/jsql/util/ProxyUtil::setUsingProxyHttp → NO_COVERAGE

59

1.1
Location : setPreferences
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyAddressHttp → NO_COVERAGE

60

1.1
Location : setPreferences
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyPortHttp → NO_COVERAGE

62

1.1
Location : setPreferences
Killed by : none
removed call to com/jsql/util/ProxyUtil::setUsingProxyHttps → NO_COVERAGE

63

1.1
Location : setPreferences
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyAddressHttps → NO_COVERAGE

64

1.1
Location : setPreferences
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyPortHttps → NO_COVERAGE

68

1.1
Location : setPreferences
Killed by : none
removed call to java/util/prefs/Preferences::putBoolean → NO_COVERAGE

69

1.1
Location : setPreferences
Killed by : none
removed call to java/util/prefs/Preferences::put → NO_COVERAGE

70

1.1
Location : setPreferences
Killed by : none
removed call to java/util/prefs/Preferences::put → NO_COVERAGE

72

1.1
Location : setPreferences
Killed by : none
removed call to java/util/prefs/Preferences::putBoolean → NO_COVERAGE

73

1.1
Location : setPreferences
Killed by : none
removed call to java/util/prefs/Preferences::put → NO_COVERAGE

74

1.1
Location : setPreferences
Killed by : none
removed call to java/util/prefs/Preferences::put → NO_COVERAGE

77

1.1
Location : setPreferences
Killed by : none
negated conditional → NO_COVERAGE

84

1.1
Location : setPreferences
Killed by : none
negated conditional → NO_COVERAGE

101

1.1
Location : initProxy
Killed by : none
removed call to com/jsql/util/ProxyUtil::setUsingProxyHttp → NO_COVERAGE

102

1.1
Location : initProxy
Killed by : none
removed call to com/jsql/util/ProxyUtil::setUsingProxyHttps → NO_COVERAGE

105

1.1
Location : initProxy
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyAddressHttp → NO_COVERAGE

106

1.1
Location : initProxy
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyPortHttp → NO_COVERAGE

108

1.1
Location : initProxy
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyAddressHttps → NO_COVERAGE

109

1.1
Location : initProxy
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyPortHttps → NO_COVERAGE

112

1.1
Location : initProxy
Killed by : none
negated conditional → NO_COVERAGE

116

1.1
Location : initProxy
Killed by : none
negated conditional → NO_COVERAGE

132

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

133

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

134

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

139

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

140

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

141

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

145

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : isNotLive
Killed by : none
replaced boolean return with true for com/jsql/util/ProxyUtil::isNotLive → NO_COVERAGE

152

1.1
Location : isSocketOn
Killed by : none
removed call to java/net/Socket::close → NO_COVERAGE

153

1.1
Location : isSocketOn
Killed by : none
removed call to com/jsql/util/ProxyUtil::logStatus → NO_COVERAGE

156

1.1
Location : isSocketOn
Killed by : none
removed call to com/jsql/util/ProxyUtil::logStatus → NO_COVERAGE

158

1.1
Location : isSocketOn
Killed by : none
replaced boolean return with false for com/jsql/util/ProxyUtil::isSocketOn → NO_COVERAGE

2.2
Location : isSocketOn
Killed by : none
replaced boolean return with true for com/jsql/util/ProxyUtil::isSocketOn → NO_COVERAGE

162

1.1
Location : logStatus
Killed by : none
negated conditional → NO_COVERAGE

166

1.1
Location : lambda$logStatus$0
Killed by : none
replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$0 → NO_COVERAGE

167

1.1
Location : lambda$logStatus$1
Killed by : none
replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$1 → NO_COVERAGE

168

1.1
Location : lambda$logStatus$2
Killed by : none
replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$2 → NO_COVERAGE

174

1.1
Location : logStatus
Killed by : none
negated conditional → NO_COVERAGE

178

1.1
Location : lambda$logStatus$3
Killed by : none
replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$3 → NO_COVERAGE

193

1.1
Location : getProxyAddressHttp
Killed by : none
replaced return value with "" for com/jsql/util/ProxyUtil::getProxyAddressHttp → NO_COVERAGE

201

1.1
Location : getProxyPortHttp
Killed by : none
replaced return value with "" for com/jsql/util/ProxyUtil::getProxyPortHttp → NO_COVERAGE

209

1.1
Location : isUsingProxyHttp
Killed by : none
replaced boolean return with false for com/jsql/util/ProxyUtil::isUsingProxyHttp → NO_COVERAGE

2.2
Location : isUsingProxyHttp
Killed by : none
replaced boolean return with true for com/jsql/util/ProxyUtil::isUsingProxyHttp → NO_COVERAGE

217

1.1
Location : getProxyAddressHttps
Killed by : none
replaced return value with "" for com/jsql/util/ProxyUtil::getProxyAddressHttps → NO_COVERAGE

225

1.1
Location : getProxyPortHttps
Killed by : none
replaced return value with "" for com/jsql/util/ProxyUtil::getProxyPortHttps → NO_COVERAGE

233

1.1
Location : isUsingProxyHttps
Killed by : none
replaced boolean return with true for com/jsql/util/ProxyUtil::isUsingProxyHttps → NO_COVERAGE

2.2
Location : isUsingProxyHttps
Killed by : none
replaced boolean return with false for com/jsql/util/ProxyUtil::isUsingProxyHttps → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.19.1