• 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

77.27
/external-sop/src/main/java/sop/external/operation/InlineVerifyExternal.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.ReadyWithResult;
8
import sop.Verification;
9
import sop.exception.SOPGPException;
10
import sop.external.ExternalSOP;
11
import sop.operation.InlineVerify;
12
import sop.util.UTCUtil;
13

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

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

32
    private final ExternalSOP.TempDirProvider tempDirProvider;
33
    private final List<String> commandList = new ArrayList<>();
1✔
34
    private final List<String> envList;
35

36
    private int certCounter = 0;
1✔
37

38
    public InlineVerifyExternal(String binary, Properties environment, ExternalSOP.TempDirProvider tempDirProvider) {
1✔
39
        this.tempDirProvider = tempDirProvider;
1✔
40
        commandList.add(binary);
1✔
41
        commandList.add("inline-verify");
1✔
42
        envList = ExternalSOP.propertiesToEnv(environment);
1✔
43
    }
1✔
44

45
    @Override
46
    @Nonnull
47
    public InlineVerify 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 InlineVerify 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 InlineVerify cert(@Nonnull InputStream cert) throws SOPGPException.BadData, IOException {
62
        String envVar = "CERT_" + certCounter++;
1✔
63
        commandList.add("@ENV:" + envVar);
1✔
64
        envList.add(envVar + "=" + ExternalSOP.readString(cert));
1✔
65
        return this;
1✔
66
    }
67

68
    @Override
69
    @Nonnull
70
    public ReadyWithResult<List<Verification>> data(@Nonnull InputStream data) throws IOException, SOPGPException.NoSignature, SOPGPException.BadData {
71
        File tempDir = tempDirProvider.provideTempDirectory();
1✔
72

73
        File verificationsOut = new File(tempDir, "verifications-out");
1✔
74
        verificationsOut.delete();
1✔
75
        commandList.add("--verifications-out=" + verificationsOut.getAbsolutePath());
1✔
76

77
        String[] command = commandList.toArray(new String[0]);
1✔
78
        String[] env = envList.toArray(new String[0]);
1✔
79

80
        try {
81
            Process process = Runtime.getRuntime().exec(command, env);
1✔
82
            OutputStream processOut = process.getOutputStream();
1✔
83
            InputStream processIn = process.getInputStream();
1✔
84

85
            return new ReadyWithResult<List<Verification>>() {
1✔
86
                @Override
87
                public List<Verification> writeTo(@Nonnull OutputStream outputStream) throws IOException, SOPGPException.NoSignature {
88
                    byte[] buf = new byte[4096];
1✔
89
                    int r;
90
                    while ((r = data.read(buf)) > 0) {
1✔
91
                        processOut.write(buf, 0, r);
1✔
92
                    }
93

94
                    data.close();
1✔
95
                    processOut.close();
1✔
96

97

98
                    while ((r = processIn.read(buf)) > 0) {
1✔
99
                        outputStream.write(buf, 0 , r);
×
100
                    }
101

102
                    processIn.close();
1✔
103
                    outputStream.close();
1✔
104

105
                    ExternalSOP.finish(process);
×
106

107
                    FileInputStream verificationsOutIn = new FileInputStream(verificationsOut);
×
108
                    BufferedReader reader = new BufferedReader(new InputStreamReader(verificationsOutIn));
×
109
                    List<Verification> verificationList = new ArrayList<>();
×
110
                    String line;
111
                    while ((line = reader.readLine()) != null) {
×
112
                        verificationList.add(Verification.fromString(line.trim()));
×
113
                    }
114

115
                    return verificationList;
×
116
                }
117
            };
118
        } catch (IOException e) {
×
119
            throw new RuntimeException(e);
×
120
        }
121
    }
122
}
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