github.com/Venafi/vcert/v5@v5.10.2/config_test.go (about)

     1  /*
     2   * Copyright 2018 Venafi, Inc.
     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  
    17  package vcert
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"testing"
    23  )
    24  
    25  const validTestModeConfig = `
    26  test_mode = true`
    27  
    28  const invalidTestModeConfig = `
    29  test_mode = false`
    30  
    31  const validTPPConfigDeprecated = `# all fine here
    32  tpp_url = https://ha-tpp1.example.com:5008/vedsdk
    33  tpp_user = admin
    34  tpp_password = xxx
    35  tpp_zone = devops\vcert`
    36  
    37  const validTPPConfig = `# all fine here
    38  url = https://ha-tpp1.example.com:5008/vedsdk
    39  access_token = ns1dofUPmsdxTLQS2hM1gQ==
    40  tpp_zone = devops\vcert`
    41  
    42  const emptyConfig = ``
    43  
    44  const invalidTPPConfig = `# cloud zone cannot be used in TPP section
    45  url = https://ha-tpp1.example.com:5008/vedsdk
    46  access_token = ns1dofUPmsdxTLQS2hM1gQ==
    47  tpp_zone = devops\vcert
    48  cloud_zone = Default`
    49  
    50  const invalidTPPConfig2 = `# missing password
    51  url = https://ha-tpp1.example.com:5008/vedsdk
    52  tpp_user = admin
    53  #tpp_password = xxx
    54  tpp_zone = devops\vcert`
    55  
    56  const invalidTPPConfig3 = `# trust bundle cannot be loaded
    57  url = https://ha-tpp1.example.com:5008/vedsdk
    58  access_token = ns1dofUPmsdxTLQS2hM1gQ==
    59  tpp_zone = devops\vcert
    60  trust_bundle = ~/.vcert/file.does-not-exist`
    61  
    62  const validCloudConfig = `
    63  url = https://api.dev12.qa.venafi.io/v1
    64  cloud_apikey = xxxxxxxx-b256-4c43-a4d4-15372ce2d548
    65  cloud_zone = Default`
    66  
    67  const validCloudConfig2 = `
    68  cloud_apikey = xxxxxxxx-b256-4c43-a4d4-15372ce2d548`
    69  
    70  const invalidCloudConfig = `# tpp user is illegal
    71  url = https://api.dev12.qa.venafi.io/v1
    72  cloud_apikey = xxxxxxxx-b256-4c43-a4d4-15372ce2d548
    73  tpp_user = admin
    74  cloud_zone = Default`
    75  
    76  func TestLoadFromFile(t *testing.T) {
    77  	var cases = []struct {
    78  		valid   bool
    79  		content string
    80  	}{
    81  		{true, validTestModeConfig},
    82  		{true, validTPPConfig},
    83  		{true, validTPPConfigDeprecated},
    84  		{true, validCloudConfig},
    85  		{true, validCloudConfig},
    86  		{true, validCloudConfig2},
    87  		{false, emptyConfig},
    88  		{false, invalidTestModeConfig},
    89  		{false, invalidTPPConfig},
    90  		{false, invalidTPPConfig2},
    91  		{false, invalidTPPConfig3},
    92  		{false, invalidCloudConfig},
    93  	}
    94  	for _, test_case := range cases {
    95  		tmpfile, err := ioutil.TempFile("", "")
    96  		if err != nil {
    97  			t.Fatal(err)
    98  		}
    99  		defer os.Remove(tmpfile.Name())
   100  
   101  		err = ioutil.WriteFile(tmpfile.Name(), []byte(test_case.content), 0644)
   102  		if err != nil {
   103  			t.Fatal(err)
   104  		}
   105  
   106  		_, err = LoadConfigFromFile(tmpfile.Name(), "")
   107  		if test_case.valid {
   108  			if err != nil {
   109  				t.Logf("config: %s", test_case.content)
   110  				t.Fatal(err)
   111  			}
   112  		} else {
   113  			if err == nil {
   114  				t.Fatalf("it should fail to load config: \n%s", test_case.content)
   115  			}
   116  		}
   117  	}
   118  }