config_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // Test all required fields
  76. for _, field := range suite.requiredFields {
  77. // Missing a required field
  78. json.Unmarshal(suite.confStubBlob, &testObj)
  79. delete(testObj, field)
  80. testObjJSON, _ = json.Marshal(testObj)
  81. _, err = LoadConfig(testObjJSON)
  82. suite.NotNil(err, "JSON with one of our required fields missing should fail: %s", field)
  83. // Bad type for required field
  84. json.Unmarshal(suite.confStubBlob, &testObj)
  85. testObj[field] = false // basically guessing a wrong type
  86. testObjJSON, _ = json.Marshal(testObj)
  87. _, err = LoadConfig(testObjJSON)
  88. suite.NotNil(err, "JSON with one of our required fields with the wrong type should fail: %s", field)
  89. // One of our required fields is null
  90. json.Unmarshal(suite.confStubBlob, &testObj)
  91. testObj[field] = nil
  92. testObjJSON, _ = json.Marshal(testObj)
  93. _, err = LoadConfig(testObjJSON)
  94. suite.NotNil(err, "JSON with one of our required fields set to null should fail: %s", field)
  95. // One of our required fields is an empty string
  96. json.Unmarshal(suite.confStubBlob, &testObj)
  97. testObj[field] = ""
  98. testObjJSON, _ = json.Marshal(testObj)
  99. _, err = LoadConfig(testObjJSON)
  100. suite.NotNil(err, "JSON with one of our required fields set to an empty string should fail: %s", field)
  101. }
  102. // Test optional fields
  103. for _, field := range suite.nonRequiredFields {
  104. // Has incorrect type for optional field
  105. json.Unmarshal(suite.confStubBlob, &testObj)
  106. testObj[field] = false // basically guessing a wrong type
  107. testObjJSON, _ = json.Marshal(testObj)
  108. _, err = LoadConfig(testObjJSON)
  109. suite.NotNil(err, "JSON with one of our optional fields with the wrong type should fail: %s", field)
  110. }
  111. }
  112. // Tests config file with JSON contents that don't match our structure
  113. func (suite *ConfigTestSuite) Test_LoadConfig_GoodJson() {
  114. var testObj map[string]interface{}
  115. var testObjJSON []byte
  116. // TODO: Test that the config actually gets the values we expect?
  117. // Has all of our required fields, but no optional fields
  118. json.Unmarshal(suite.confStubBlob, &testObj)
  119. for i := range suite.nonRequiredFields {
  120. delete(testObj, suite.nonRequiredFields[i])
  121. }
  122. testObjJSON, _ = json.Marshal(testObj)
  123. _, err := LoadConfig(testObjJSON)
  124. suite.Nil(err, "JSON with good values for our required fields but no optional fields should succeed")
  125. // Has all of our required fields, and all optional fields
  126. _, err = LoadConfig(suite.confStubBlob)
  127. suite.Nil(err, "JSON with all good values for required and optional fields should succeed")
  128. // Has null for optional fields
  129. json.Unmarshal(suite.confStubBlob, &testObj)
  130. for i := range suite.nonRequiredFields {
  131. testObj[suite.nonRequiredFields[i]] = nil
  132. }
  133. testObjJSON, _ = json.Marshal(testObj)
  134. _, err = LoadConfig(testObjJSON)
  135. suite.Nil(err, "JSON with null for optional values should succeed")
  136. }