|
| 1 | +// Copyright 2022 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.devtools.build.lib.authandtls; |
| 16 | + |
| 17 | +import static com.google.common.truth.Truth.assertThat; |
| 18 | +import static com.google.common.truth.Truth8.assertThat; |
| 19 | +import static org.junit.Assert.assertThrows; |
| 20 | + |
| 21 | +import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions.UnresolvedScopedCredentialHelper; |
| 22 | +import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions.UnresolvedScopedCredentialHelperConverter; |
| 23 | +import com.google.devtools.common.options.OptionsParsingException; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.junit.runners.JUnit4; |
| 27 | + |
| 28 | +/** Test for {@link UnresolvedScopedCredentialHelperConverter}. */ |
| 29 | +@RunWith(JUnit4.class) |
| 30 | +public class UnresolvedScopedCredentialHelperConverterTest { |
| 31 | + @Test |
| 32 | + public void convertAbsolutePath() throws Exception { |
| 33 | + UnresolvedScopedCredentialHelper helper1 = |
| 34 | + UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("/absolute/path"); |
| 35 | + assertThat(helper1.getScope()).isEmpty(); |
| 36 | + assertThat(helper1.getPath()).isEqualTo("/absolute/path"); |
| 37 | + |
| 38 | + UnresolvedScopedCredentialHelper helper2 = |
| 39 | + UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("example.com=/absolute/path"); |
| 40 | + assertThat(helper2.getScope()).hasValue("example.com"); |
| 41 | + assertThat(helper2.getPath()).isEqualTo("/absolute/path"); |
| 42 | + |
| 43 | + UnresolvedScopedCredentialHelper helper3 = |
| 44 | + UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("*.example.com=/absolute/path"); |
| 45 | + assertThat(helper3.getScope()).hasValue("*.example.com"); |
| 46 | + assertThat(helper3.getPath()).isEqualTo("/absolute/path"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void convertRootRelativePath() throws Exception { |
| 51 | + UnresolvedScopedCredentialHelper helper1 = |
| 52 | + UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("%workspace%/path"); |
| 53 | + assertThat(helper1.getScope()).isEmpty(); |
| 54 | + assertThat(helper1.getPath()).isEqualTo("%workspace%/path"); |
| 55 | + |
| 56 | + UnresolvedScopedCredentialHelper helper2 = |
| 57 | + UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("example.com=%workspace%/path"); |
| 58 | + assertThat(helper2.getScope()).hasValue("example.com"); |
| 59 | + assertThat(helper2.getPath()).isEqualTo("%workspace%/path"); |
| 60 | + |
| 61 | + UnresolvedScopedCredentialHelper helper3 = |
| 62 | + UnresolvedScopedCredentialHelperConverter.INSTANCE.convert( |
| 63 | + "*.example.com=%workspace%/path"); |
| 64 | + assertThat(helper3.getScope()).hasValue("*.example.com"); |
| 65 | + assertThat(helper3.getPath()).isEqualTo("%workspace%/path"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void convertPathLookup() throws Exception { |
| 70 | + UnresolvedScopedCredentialHelper helper1 = |
| 71 | + UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("foo"); |
| 72 | + assertThat(helper1.getScope()).isEmpty(); |
| 73 | + assertThat(helper1.getPath()).isEqualTo("foo"); |
| 74 | + |
| 75 | + UnresolvedScopedCredentialHelper helper2 = |
| 76 | + UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("example.com=foo"); |
| 77 | + assertThat(helper2.getScope()).hasValue("example.com"); |
| 78 | + assertThat(helper2.getPath()).isEqualTo("foo"); |
| 79 | + |
| 80 | + UnresolvedScopedCredentialHelper helper3 = |
| 81 | + UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("*.example.com=foo"); |
| 82 | + assertThat(helper3.getScope()).hasValue("*.example.com"); |
| 83 | + assertThat(helper3.getPath()).isEqualTo("foo"); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void emptyPath() { |
| 88 | + assertThrows( |
| 89 | + OptionsParsingException.class, |
| 90 | + () -> UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("")); |
| 91 | + assertThrows( |
| 92 | + OptionsParsingException.class, |
| 93 | + () -> UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("foo=")); |
| 94 | + assertThrows( |
| 95 | + OptionsParsingException.class, |
| 96 | + () -> UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("=")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void emptyScope() { |
| 101 | + assertThrows( |
| 102 | + OptionsParsingException.class, |
| 103 | + () -> UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("=/foo")); |
| 104 | + assertThrows( |
| 105 | + OptionsParsingException.class, |
| 106 | + () -> UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("=")); |
| 107 | + } |
| 108 | +} |
0 commit comments