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

pgpainless / sop-java / #17

15 Nov 2023 01:23PM UTC coverage: 65.932% (-3.9%) from 69.789%
#17

push

other

vanitasvitae
Fix gradle version

1167 of 1770 relevant lines covered (65.93%)

0.66 hits per line

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

95.24
/external-sop/src/main/java/sop/external/operation/DetachedVerifyExternal.java
1
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
2
//
3
// SPDX-License-Identifier: Apache-2.0
4

5
package sop.external.operation;
6

7
import sop.Verification;
8
import sop.exception.SOPGPException;
9
import sop.external.ExternalSOP;
10
import sop.operation.DetachedVerify;
11
import sop.operation.VerifySignatures;
12
import sop.util.UTCUtil;
13

14
import javax.annotation.Nonnull;
15
import java.io.BufferedReader;
16
import java.io.IOException;
17
import java.io.InputStream;
18
import java.io.InputStreamReader;
19
import java.io.OutputStream;
20
import java.util.ArrayList;
21
import java.util.Date;
22
import java.util.HashSet;
23
import java.util.List;
24
import java.util.Properties;
25
import java.util.Set;
26

27
/**
28
 * Implementation of the {@link DetachedVerify} operation using an external SOP binary.
29
 */
30
public class DetachedVerifyExternal implements DetachedVerify {
31

32
    private final List<String> commandList = new ArrayList<>();
1✔
33
    private final List<String> envList;
34

35
    private final Set<InputStream> certs = new HashSet<>();
1✔
36
    private InputStream signatures;
37
    private int certCounter = 0;
1✔
38

39
    public DetachedVerifyExternal(String binary, Properties environment) {
1✔
40
        commandList.add(binary);
1✔
41
        commandList.add("verify");
1✔
42
        envList = ExternalSOP.propertiesToEnv(environment);
1✔
43
    }
1✔
44

45
    @Override
46
    @Nonnull
47
    public DetachedVerify notBefore(@Nonnull Date timestamp) throws SOPGPException.UnsupportedOption {
48
        commandList.add("--not-before=" + UTCUtil.formatUTCDate(timestamp));
1✔
49
        return this;
1✔
50
    }
51

52
    @Override
53
    @Nonnull
54
    public DetachedVerify notAfter(@Nonnull Date timestamp) throws SOPGPException.UnsupportedOption {
55
        commandList.add("--not-after=" + UTCUtil.formatUTCDate(timestamp));
1✔
56
        return this;
1✔
57
    }
58

59
    @Override
60
    @Nonnull
61
    public DetachedVerify cert(@Nonnull InputStream cert) throws SOPGPException.BadData {
62
        this.certs.add(cert);
1✔
63
        return this;
1✔
64
    }
65

66
    @Override
67
    @Nonnull
68
    public VerifySignatures signatures(@Nonnull InputStream signatures) throws SOPGPException.BadData {
69
        this.signatures = signatures;
1✔
70
        return this;
1✔
71
    }
72

73
    @Override
74
    @Nonnull
75
    public List<Verification> data(@Nonnull InputStream data) throws IOException, SOPGPException.NoSignature, SOPGPException.BadData {
76
        commandList.add("@ENV:SIGNATURE");
1✔
77
        envList.add("SIGNATURE=" + ExternalSOP.readString(signatures));
1✔
78

79
        for (InputStream cert : certs) {
1✔
80
            String envVar = "CERT_" + certCounter++;
1✔
81
            commandList.add("@ENV:" + envVar);
1✔
82
            envList.add(envVar + "=" + ExternalSOP.readString(cert));
1✔
83
        }
1✔
84

85
        String[] command = commandList.toArray(new String[0]);
1✔
86
        String[] env = envList.toArray(new String[0]);
1✔
87

88
        try {
89
            Process process = Runtime.getRuntime().exec(command, env);
1✔
90
            OutputStream processOut = process.getOutputStream();
1✔
91
            InputStream processIn = process.getInputStream();
1✔
92

93
                    byte[] buf = new byte[4096];
1✔
94
                    int r;
95
                    while ((r = data.read(buf)) > 0) {
1✔
96
                        processOut.write(buf, 0, r);
1✔
97
                    }
98

99
                    data.close();
1✔
100
                    processOut.close();
1✔
101

102
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(processIn));
1✔
103
                    List<Verification> verifications = new ArrayList<>();
1✔
104

105
                    String line = null;
1✔
106
                    while ((line = bufferedReader.readLine()) != null) {
1✔
107
                        verifications.add(Verification.fromString(line));
1✔
108
                    }
109

110
                    ExternalSOP.finish(process);
1✔
111

112
                    return verifications;
1✔
113
        } catch (IOException e) {
×
114
            throw new RuntimeException(e);
×
115
        }
116
    }
117
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc