config_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2014, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psiphon
  20. import (
  21. "encoding/json"
  22. "io/ioutil"
  23. "strings"
  24. "testing"
  25. "github.com/stretchr/testify/suite"
  26. )
  27. const (
  28. _README = "../README.md"
  29. _README_CONFIG_BEGIN = "<!--BEGIN-SAMPLE-CONFIG-->"
  30. _README_CONFIG_END = "<!--END-SAMPLE-CONFIG-->"
  31. )
  32. type ConfigTestSuite struct {
  33. suite.Suite
  34. confStubBlob []byte
  35. requiredFields []string
  36. nonRequiredFields []string
  37. }
  38. func (suite *ConfigTestSuite) SetupSuite() {
  39. readmeBlob, _ := ioutil.ReadFile(_README)
  40. readmeString := string(readmeBlob)
  41. readmeString = readmeString[strings.Index(readmeString, _README_CONFIG_BEGIN)+len(_README_CONFIG_BEGIN) : strings.Index(readmeString, _README_CONFIG_END)]
  42. readmeString = strings.TrimSpace(readmeString)
  43. readmeString = strings.Trim(readmeString, "`")
  44. suite.confStubBlob = []byte(readmeString)
  45. var obj map[string]interface{}
  46. json.Unmarshal(suite.confStubBlob, &obj)
  47. for k, v := range obj {
  48. if v == "<placeholder>" {
  49. suite.requiredFields = append(suite.requiredFields, k)
  50. } else {
  51. suite.nonRequiredFields = append(suite.nonRequiredFields, k)
  52. }
  53. }
  54. }
  55. func TestConfigTestSuite(t *testing.T) {
  56. suite.Run(t, new(ConfigTestSuite))
  57. }
  58. // Tests good config
  59. func (suite *ConfigTestSuite) Test_LoadConfig_BasicGood() {
  60. _, err := LoadConfig(suite.confStubBlob)
  61. suite.Nil(err, "a basic config should succeed")
  62. }
  63. // Tests non-JSON file contents
  64. func (suite *ConfigTestSuite) Test_LoadConfig_BadFileContents() {
  65. _, err := LoadConfig([]byte(`this is not JSON`))
  66. suite.NotNil(err, "bytes that are not JSON at all should give an error")
  67. }
  68. // Tests config file with JSON contents that don't match our structure
  69. func (suite *ConfigTestSuite) Test_LoadConfig_BadJson() {
  70. var testObj map[string]interface{}
  71. var testObjJSON []byte
  72. // JSON with none of our fields
  73. _, err := LoadConfig([]byte(`{"f1": 11, "f2": "two"}`))
  74. suite.NotNil(err, "JSON with none of our fields should fail")
  75. // Missing a required field
  76. json.Unmarshal(suite.confStubBlob, &testObj)
  77. delete(testObj, suite.requiredFields[0])
  78. testObjJSON, _ = json.Marshal(testObj)
  79. _, err = LoadConfig(testObjJSON)
  80. suite.NotNil(err, "JSON with one of our required fields missing should fail")
  81. // Bad type for required field
  82. json.Unmarshal(suite.confStubBlob, &testObj)
  83. testObj[suite.requiredFields[0]] = false // basically guessing a wrong type
  84. testObjJSON, _ = json.Marshal(testObj)
  85. _, err = LoadConfig(testObjJSON)
  86. suite.NotNil(err, "JSON with one of our required fields with the wrong type should fail")
  87. // One of our required fields is null
  88. json.Unmarshal(suite.confStubBlob, &testObj)
  89. testObj[suite.requiredFields[0]] = nil
  90. testObjJSON, _ = json.Marshal(testObj)
  91. _, err = LoadConfig(testObjJSON)
  92. suite.NotNil(err, "JSON with one of our required fields set to null should fail")
  93. // One of our required fields is an empty string
  94. json.Unmarshal(suite.confStubBlob, &testObj)
  95. testObj[suite.requiredFields[0]] = ""
  96. testObjJSON, _ = json.Marshal(testObj)
  97. _, err = LoadConfig(testObjJSON)
  98. suite.NotNil(err, "JSON with one of our required fields set to an empty string should fail")
  99. // Has incorrect type for optional field
  100. json.Unmarshal(suite.confStubBlob, &testObj)
  101. testObj[suite.nonRequiredFields[0]] = false // basically guessing a wrong type
  102. testObjJSON, _ = json.Marshal(testObj)
  103. _, err = LoadConfig(testObjJSON)
  104. suite.NotNil(err, "JSON with one of our optional fields with the wrong type should fail")
  105. }
  106. // Tests config file with JSON contents that don't match our structure
  107. func (suite *ConfigTestSuite) Test_LoadConfig_GoodJson() {
  108. var testObj map[string]interface{}
  109. var testObjJSON []byte
  110. // TODO: Test that the config actually gets the values we expect?
  111. // Has all of our required fields, but no optional fields
  112. json.Unmarshal(suite.confStubBlob, &testObj)
  113. for i := range suite.nonRequiredFields {
  114. delete(testObj, suite.nonRequiredFields[i])
  115. }
  116. testObjJSON, _ = json.Marshal(testObj)
  117. _, err := LoadConfig(testObjJSON)
  118. suite.Nil(err, "JSON with good values for our required fields but no optional fields should succeed")
  119. // Has all of our required fields, and all optional fields
  120. _, err = LoadConfig(suite.confStubBlob)
  121. suite.Nil(err, "JSON with all good values for required and optional fields should succeed")
  122. // Has null for optional fields
  123. json.Unmarshal(suite.confStubBlob, &testObj)
  124. for i := range suite.nonRequiredFields {
  125. testObj[suite.nonRequiredFields[i]] = nil
  126. }
  127. testObjJSON, _ = json.Marshal(testObj)
  128. _, err = LoadConfig(testObjJSON)
  129. suite.Nil(err, "JSON with null for optional values should succeed")
  130. }