|
| 1 | +/* |
| 2 | + * Copyright 2024 The Sigstore Authors. |
| 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 | +package dev.sigstore.rekor.client; |
| 17 | + |
| 18 | +import com.google.common.io.Resources; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Base64; |
| 23 | +import org.junit.jupiter.api.Assertions; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +public class CheckpointsTest { |
| 27 | + |
| 28 | + public static final String REKOR_PUB_KEYID = "wNI9atQGlz+VWfO6LRygH4QUfY/8W4RFwiT5i5WRgB0="; |
| 29 | + |
| 30 | + public String getResource(String filename) throws IOException { |
| 31 | + return Resources.toString( |
| 32 | + Resources.getResource("dev/sigstore/samples/checkpoints/" + filename), |
| 33 | + StandardCharsets.UTF_8); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void from_valid() throws Exception { |
| 38 | + var checkpoint = Checkpoints.from(getResource("valid.txt")); |
| 39 | + Assertions.assertEquals("rekor.sigstore.dev - 2605736670972794746", checkpoint.getOrigin()); |
| 40 | + Assertions.assertEquals(37795272, checkpoint.getSize()); |
| 41 | + Assertions.assertEquals( |
| 42 | + "60ll7idWI1jYRZzxc+jKflYoW+4jWxgZaGR15ASsWt4=", checkpoint.getBase64Hash()); |
| 43 | + |
| 44 | + var keyBytesHintExpected = |
| 45 | + Arrays.copyOfRange(Base64.getDecoder().decode(REKOR_PUB_KEYID), 0, 4); |
| 46 | + var sig = checkpoint.getSignatures().get(0); |
| 47 | + Assertions.assertEquals(1, checkpoint.getSignatures().size()); |
| 48 | + Assertions.assertEquals("rekor.sigstore.dev", sig.getIdentity()); |
| 49 | + Assertions.assertArrayEquals(keyBytesHintExpected, sig.getKeyHint()); |
| 50 | + Assertions.assertEquals( |
| 51 | + "MEYCIQCVZQfYdI9rogwhEGAVwhemHcyP3EzvRZHRVUAO8YiX+gIhAKB+9RSNH9fmN7CWqkBYjw24kiJwqlMbri+jpQzl+lKB", |
| 52 | + Base64.getEncoder().encodeToString(sig.getSignature())); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void from_validMultiSig() throws Exception { |
| 57 | + var checkpoint = Checkpoints.from(getResource("valid_multi_sig.txt")); |
| 58 | + Assertions.assertEquals("rekor.sigstore.dev - 2605736670972794746", checkpoint.getOrigin()); |
| 59 | + Assertions.assertEquals(37795272, checkpoint.getSize()); |
| 60 | + Assertions.assertEquals( |
| 61 | + "60ll7idWI1jYRZzxc+jKflYoW+4jWxgZaGR15ASsWt4=", checkpoint.getBase64Hash()); |
| 62 | + |
| 63 | + Assertions.assertEquals(2, checkpoint.getSignatures().size()); |
| 64 | + var keyBytesHintExpected = |
| 65 | + Arrays.copyOfRange(Base64.getDecoder().decode(REKOR_PUB_KEYID), 0, 4); |
| 66 | + |
| 67 | + var sig1 = checkpoint.getSignatures().get(0); |
| 68 | + Assertions.assertEquals("rekor.sigstore.dev", sig1.getIdentity()); |
| 69 | + Assertions.assertArrayEquals(keyBytesHintExpected, sig1.getKeyHint()); |
| 70 | + Assertions.assertEquals( |
| 71 | + "MEYCIQCVZQfYdI9rogwhEGAVwhemHcyP3EzvRZHRVUAO8YiX+gIhAKB+9RSNH9fmN7CWqkBYjw24kiJwqlMbri+jpQzl+lKB", |
| 72 | + Base64.getEncoder().encodeToString(sig1.getSignature())); |
| 73 | + |
| 74 | + var sig2 = checkpoint.getSignatures().get(1); |
| 75 | + Assertions.assertEquals("bob.loblaw.dev", sig2.getIdentity()); |
| 76 | + Assertions.assertArrayEquals(keyBytesHintExpected, sig2.getKeyHint()); |
| 77 | + Assertions.assertEquals( |
| 78 | + "MEYCIQCVZQfYdI9rogwhEGAVwhGmHcyP3EzvRZHRVUAO8YiX+gIhAKB+9RSNH9fmN7CWqkBYjw24kiJwqlMbri+jpQzl+lKB", |
| 79 | + Base64.getEncoder().encodeToString(sig2.getSignature())); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void from_noSeparator() throws Exception { |
| 84 | + var ex = |
| 85 | + Assertions.assertThrows( |
| 86 | + RekorParseException.class, |
| 87 | + () -> Checkpoints.from(getResource("error_header_body_separator.txt"))); |
| 88 | + Assertions.assertEquals( |
| 89 | + "Checkpoint must contain one blank line, delineating the header from the signature block", |
| 90 | + ex.getMessage()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void from_notEnoughHeaders() throws Exception { |
| 95 | + var ex = |
| 96 | + Assertions.assertThrows( |
| 97 | + RekorParseException.class, |
| 98 | + () -> Checkpoints.from(getResource("error_header_count.txt"))); |
| 99 | + Assertions.assertEquals("Checkpoint header must contain at least 3 lines", ex.getMessage()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void from_notANumber() throws Exception { |
| 104 | + var ex = |
| 105 | + Assertions.assertThrows( |
| 106 | + RekorParseException.class, |
| 107 | + () -> Checkpoints.from(getResource("error_not_a_number.txt"))); |
| 108 | + Assertions.assertEquals( |
| 109 | + "Checkpoint header attribute size must be a number, but was: abcdefg", ex.getMessage()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void from_noSignatures() throws Exception { |
| 114 | + var ex = |
| 115 | + Assertions.assertThrows( |
| 116 | + RekorParseException.class, |
| 117 | + () -> Checkpoints.from(getResource("error_no_signatures.txt"))); |
| 118 | + Assertions.assertEquals("Checkpoint body must contain at least one signature", ex.getMessage()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void from_noNewlineAfterSignatures() throws Exception { |
| 123 | + var ex = |
| 124 | + Assertions.assertThrows( |
| 125 | + RekorParseException.class, |
| 126 | + () -> Checkpoints.from(getResource("error_no_newline_after_signature.txt"))); |
| 127 | + Assertions.assertEquals("Checkpoint signature section must end with newline", ex.getMessage()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void from_signatureFormatInvalid() throws Exception { |
| 132 | + var ex = |
| 133 | + Assertions.assertThrows( |
| 134 | + RekorParseException.class, |
| 135 | + () -> Checkpoints.from(getResource("error_signature_format_invalid.txt"))); |
| 136 | + Assertions.assertEquals( |
| 137 | + "Checkpoint signature 'rekor.sigstore.dev wNI9ajBGAiEAlWUH2HSPa6IMIRBgFcIXph3Mj9xM70WR0VVADvGIl/oCIQCgfvUUjR/X5jewlqpAWI8NuJIicKpTG64vo6UM5fpSgQ==' was not in the format '— <id> <base64 keyhint+signature>'", |
| 138 | + ex.getMessage()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void from_signatureLengthInsufficient() throws Exception { |
| 143 | + var ex = |
| 144 | + Assertions.assertThrows( |
| 145 | + RekorParseException.class, |
| 146 | + () -> Checkpoints.from(getResource("error_signature_length_insufficient.txt"))); |
| 147 | + Assertions.assertEquals( |
| 148 | + "Checkpoint signature <keyhint + signature> was 4 bytes long, but must be at least 5 bytes long", |
| 149 | + ex.getMessage()); |
| 150 | + } |
| 151 | +} |
0 commit comments