|
| 1 | +/*- |
| 2 | + * Copyright 2015 Square Inc. |
| 3 | + * Copyright 2014 CoreOS |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package tests |
| 19 | + |
| 20 | +import ( |
| 21 | + "os" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | +) |
| 25 | + |
| 26 | +// Ensures certificates which aren't a CA can't sign other certificates. |
| 27 | +func TestNotCA(t *testing.T) { |
| 28 | + os.RemoveAll(depotDir) |
| 29 | + defer os.RemoveAll(depotDir) |
| 30 | + |
| 31 | + stdout, stderr, err := run(binPath, "init", "--passphrase", passphrase, "--common-name", "cert1") |
| 32 | + if stderr != "" || err != nil { |
| 33 | + t.Fatalf("Received unexpected error: %v, %v", stderr, err) |
| 34 | + } |
| 35 | + if strings.Count(stdout, "Created") != 3 { |
| 36 | + t.Fatalf("Received incorrect create: %v", stdout) |
| 37 | + } |
| 38 | + |
| 39 | + stdout, stderr, err = run(binPath, "request-cert", "--passphrase", passphrase, "--common-name", "cert2") |
| 40 | + if stderr != "" || err != nil { |
| 41 | + t.Fatalf("Received unexpected error: %v, %v", stderr, err) |
| 42 | + } |
| 43 | + if strings.Count(stdout, "Created") != 2 { |
| 44 | + t.Fatalf("Received incorrect create: %v", stdout) |
| 45 | + } |
| 46 | + |
| 47 | + stdout, stderr, err = run(binPath, "request-cert", "--passphrase", passphrase, "--common-name", "cert3") |
| 48 | + if stderr != "" || err != nil { |
| 49 | + t.Fatalf("Received unexpected error: %v, %v", stderr, err) |
| 50 | + } |
| 51 | + if strings.Count(stdout, "Created") != 2 { |
| 52 | + t.Fatalf("Received incorrect create: %v", stdout) |
| 53 | + } |
| 54 | + |
| 55 | + stdout, stderr, err = run(binPath, "sign", "--passphrase", passphrase, "--CA", "cert1", "cert2") |
| 56 | + if stderr != "" || err != nil { |
| 57 | + t.Fatalf("Received unexpected error: %v, %v", stderr, err) |
| 58 | + } |
| 59 | + if strings.Count(stdout, "Created") != 1 { |
| 60 | + t.Fatalf("Received incorrect create: %v", stdout) |
| 61 | + } |
| 62 | + |
| 63 | + stdout, stderr, err = run(binPath, "sign", "--passphrase", passphrase, "--CA", "cert2", "cert3") |
| 64 | + if stderr != "CA certificate is not allowed to sign certificates.\n" { |
| 65 | + t.Fatalf("Failed to receive expected error.") |
| 66 | + } |
| 67 | +} |
0 commit comments