• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ebourg / jsign / #409

08 Jul 2026 03:29PM UTC coverage: 80.912% (+0.1%) from 80.783%
#409

push

ebourg
Improved the test coverage of NAVX file parsing

1 of 1 new or added line in 1 file covered. (100.0%)

49 existing lines in 3 files now uncovered.

5129 of 6339 relevant lines covered (80.91%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.51
/jsign-ant/src/main/java/net/jsign/JsignTask.java
1
/*
2
 * Copyright 2012 Emmanuel Bourg
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package net.jsign;
18

19
import java.io.File;
20
import java.lang.reflect.Field;
21
import java.lang.reflect.Method;
22
import java.util.logging.Level;
23
import java.util.logging.Logger;
24
import java.util.stream.Stream;
25

26
import org.apache.tools.ant.BuildException;
27
import org.apache.tools.ant.BuildLogger;
28
import org.apache.tools.ant.DefaultLogger;
29
import org.apache.tools.ant.Task;
30
import org.apache.tools.ant.types.FileSet;
31

32
import static org.apache.tools.ant.Project.*;
33

34
/**
35
 * Ant task for signing files with Authenticode.
36
 *
37
 * @author Emmanuel Bourg
38
 * @since 1.0
39
 */
40
public class JsignTask extends Task {
1✔
41

42
    /** The file to be signed. */
43
    private File file;
44

45
    /** The set of files to be signed. */
46
    private FileSet fileset;
47

48
    /** The operation to execute */
49
    private String command = "sign";
1✔
50

51
    /** The program name embedded in the signature. */
52
    private String name;
53

54
    /** The program URL embedded in the signature. */
55
    private String url;
56

57
    /** The digest algorithm to use for the signature. */
58
    private String algorithm;
59

60
    /** The keystore file, the SunPKCS11 configuration file or the cloud keystore name. */
61
    private String keystore;
62

63
    /** The password for the keystore. */
64
    private String storepass;
65

66
    /** The type of the keystore. */
67
    private String storetype;
68

69
    /** The alias of the certificate in the keystore. */
70
    private String alias;
71

72
    /** The file containing the certificate chain (PKCS#7 format). */
73
    private File certfile;
74

75
    /** The file containing the private key (PEM or PVK format) */
76
    private File keyfile;
77

78
    /** The password for the key in the store (if different from the keystore password) or in the keyfile. */
79
    private String keypass;
80

81
    /** The URL of the timestamping authority. */
82
    private String tsaurl;
83

84
    /** The protocol used for  the timestamping */
85
    private String tsmode;
86

87
    /** The number of retries for timestamping */
88
    private int tsretries = -1;
1✔
89

90
    /** The number of seconds to wait between timestamping retries */
91
    private int tsretrywait = -1;
1✔
92

93
    /** Tells if previous signatures should be replaced */
94
    private boolean replace;
95

96
    /** Skip files that are already signed. */
97
    private boolean lazy;
98

99
    /** The encoding of the script to be signed (UTF-8 by default). */
100
    private String encoding = "UTF-8";
1✔
101

102
    /** Tells if a detached signature should be generated or reused. */
103
    private boolean detached;
104

105
    /** The output format of the signature (DER or PEM). */
106
    private String format;
107

108
    /** The value of the unsigned attribute */
109
    private String value;
110

111
    public void setCommand(String command) {
112
        this.command = command;
1✔
113
    }
1✔
114

115
    public void setFile(File file) {
116
        this.file = file;
1✔
117
    }
1✔
118

119
    public void addFileset(FileSet fileset) {
120
        this.fileset = fileset;
1✔
121
    }
1✔
122

123
    public void setName(String name) {
124
        this.name = name;
1✔
125
    }
1✔
126

127
    public void setUrl(String url) {
128
        this.url = url;
1✔
129
    }
1✔
130

131
    public void setAlg(String alg) {
132
        this.algorithm = alg;
1✔
133
    }
1✔
134

135
    public void setTsmode(String tsmode) {
136
        this.tsmode = tsmode;
1✔
137
    }
1✔
138

139
    public void setKeystore(String keystore) {
140
        this.keystore = keystore;
1✔
141
    }
1✔
142

143
    public void setStorepass(String storepass) {
144
        this.storepass = storepass;
1✔
145
    }
1✔
146

147
    public void setStoretype(String storetype) {
148
        this.storetype = storetype;
1✔
149
    }
1✔
150

151
    public void setAlias(String alias) {
152
        this.alias = alias;
1✔
153
    }
1✔
154

155
    public void setCertfile(File certfile) {
156
        this.certfile = certfile;
1✔
157
    }
1✔
158

159
    public void setKeyfile(File keyfile) {
160
        this.keyfile = keyfile;
1✔
161
    }
1✔
162

163
    public void setKeypass(String keypass) {
164
        this.keypass = keypass;
1✔
165
    }
1✔
166

167
    public void setTsaurl(String tsaurl) {
168
        this.tsaurl = tsaurl;
1✔
169
    }
1✔
170

171
    public void setTsretries(int tsretries) {
172
        this.tsretries = tsretries;
1✔
173
    }
1✔
174

175
    public void setTsretrywait(int tsretrywait) {
176
        this.tsretrywait = tsretrywait;
1✔
177
    }
1✔
178

179
    public void setReplace(boolean replace) {
180
        this.replace = replace;
1✔
181
    }
1✔
182

183
    public void setLazy(boolean lazy) {
184
        this.lazy = lazy;
1✔
185
    }
1✔
186

187
    public void setEncoding(String encoding) {
188
        this.encoding = encoding;
1✔
189
    }
1✔
190

191
    public void setDetached(boolean detached) {
192
        this.detached = detached;
1✔
193
    }
1✔
194

195
    public void setFormat(String format) {
196
        this.format = format;
1✔
197
    }
1✔
198

199
    public void setValue(String value) {
200
        this.value = value;
1✔
201
    }
1✔
202

203
    @Override
204
    public void execute() throws BuildException {
205
        try {
206
            // configure the logging
207
            Logger log = Logger.getLogger("net.jsign");
1✔
208
            log.setLevel(getLevel());
1✔
209
            log.setUseParentHandlers(false);
1✔
210
            Stream.of(log.getHandlers()).forEach(log::removeHandler);
1✔
211
            log.addHandler(new AntLogHandler(this));
1✔
212

213
            SignerHelper helper = new SignerHelper("attribute");
1✔
214
            helper.setBaseDir(getProject().getBaseDir());
1✔
215
            
216
            helper.command(command);
1✔
217
            helper.name(name);
1✔
218
            helper.url(url);
1✔
219
            helper.alg(algorithm);
1✔
220
            helper.keystore(keystore);
1✔
221
            helper.storepass(storepass);
1✔
222
            helper.storetype(storetype);
1✔
223
            helper.alias(alias);
1✔
224
            helper.certfile(certfile);
1✔
225
            helper.keyfile(keyfile);
1✔
226
            helper.keypass(keypass);
1✔
227
            helper.tsaurl(tsaurl);
1✔
228
            helper.tsmode(tsmode);
1✔
229
            helper.tsretries(tsretries);
1✔
230
            helper.tsretrywait(tsretrywait);
1✔
231
            helper.replace(replace);
1✔
232
            helper.lazy(lazy);
1✔
233
            helper.encoding(encoding);
1✔
234
            helper.detached(detached);
1✔
235
            helper.format(format);
1✔
236
            helper.value(value);
1✔
237

238
            if (fileset != null) {
1✔
239
                for(String fileElement : fileset.getDirectoryScanner().getIncludedFiles()) {
1✔
240
                    helper.execute(new File(fileset.getDir(), fileElement));
1✔
241
                }
242
            } else {
243
                helper.execute(file);
1✔
244
            }
245
        } catch (Exception e) {
1✔
246
            throw new BuildException(e.getMessage(), e, getLocation());
1✔
247
        }
1✔
248
    }
1✔
249

250
    /**
251
     * Returns the logging level equivalent to the Ant message output level.
252
     */
253
    private Level getLevel() {
254
        int messageOutputLevel = getMessageOutputLevel();
1✔
255
        switch (messageOutputLevel) {
1✔
256
            case MSG_ERR:
UNCOV
257
                return Level.SEVERE;
×
258
            case MSG_WARN:
UNCOV
259
                return Level.WARNING;
×
260
            case MSG_INFO:
261
                return Level.INFO;
1✔
262
            case MSG_VERBOSE:
263
                return Level.FINE;
×
264
            case MSG_DEBUG:
UNCOV
265
                return Level.FINEST;
×
266
            default:
UNCOV
267
                return Level.INFO;
×
268
        }
269
    }
270

271
    /**
272
     * Returns the Ant message output level.
273
     */
274
    private int getMessageOutputLevel() {
275
        for (Object listener : getProject().getBuildListeners()) {
1✔
276
            if (listener instanceof BuildLogger) {
1✔
277
                try {
UNCOV
278
                    Method method = BuildLogger.class.getMethod("getMessageOutputLevel"); // requires Ant 1.10.13
×
UNCOV
279
                    return (Integer) method.invoke(listener);
×
280
                } catch (Exception e) {
1✔
281
                }
282

283
                try {
284
                    Field field = DefaultLogger.class.getDeclaredField("msgOutputLevel");
1✔
285
                    field.setAccessible(true);
1✔
286
                    return (Integer) field.get(listener);
1✔
UNCOV
287
                } catch (Exception e) {
×
288
                }
289
            }
290
        }
1✔
291

UNCOV
292
        return MSG_INFO;
×
293
    }
294
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc