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

Mutations

55

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

56

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

57

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

59

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

60

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

61

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

65

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

66

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

67

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

69

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

70

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

71

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
negated conditional → NO_COVERAGE

81

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

98

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

99

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

102

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

103

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

105

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

106

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

109

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

113

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

129

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

130

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

131

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

136

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

137

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

138

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

142

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

149

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

150

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

153

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

155

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

159

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

163

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

164

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

165

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

171

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

175

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

190

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

198

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

206

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

214

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

222

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

230

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