config_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package config
  2. import (
  3. "crypto/rsa"
  4. "os"
  5. "testing"
  6. "github.com/cloudflare/cfssl/log"
  7. _ "github.com/mattn/go-sqlite3" // import just to initialize SQLite for testing
  8. )
  9. // UnlinkIfExists removes a file if it exists.
  10. func UnlinkIfExists(file string) {
  11. _, err := os.Stat(file)
  12. if err != nil && os.IsNotExist(err) {
  13. panic("failed to remove " + file)
  14. }
  15. os.Remove(file)
  16. }
  17. // stringSlicesEqual compares two string lists, checking that they
  18. // contain the same elements.
  19. func stringSlicesEqual(slice1, slice2 []string) bool {
  20. if len(slice1) != len(slice2) {
  21. return false
  22. }
  23. for i := range slice1 {
  24. if slice1[i] != slice2[i] {
  25. return false
  26. }
  27. }
  28. for i := range slice2 {
  29. if slice1[i] != slice2[i] {
  30. return false
  31. }
  32. }
  33. return true
  34. }
  35. func TestGoodConfig(t *testing.T) {
  36. testFile := "testdata/test.conf"
  37. cmap, err := ParseToRawMap(testFile)
  38. if err != nil {
  39. t.Fatalf("%v", err)
  40. } else if len(cmap) != 2 {
  41. t.Fatal("expected 2 sections, have", len(cmap))
  42. }
  43. }
  44. func TestGoodConfig2(t *testing.T) {
  45. testFile := "testdata/test2.conf"
  46. cmap, err := ParseToRawMap(testFile)
  47. if err != nil {
  48. t.Fatalf("%v", err)
  49. } else if len(cmap) != 1 {
  50. t.Fatal("expected 1 section, have", len(cmap))
  51. } else if len(cmap["default"]) != 3 {
  52. t.Fatal("expected 3 items in default section, have", len(cmap["default"]))
  53. }
  54. }
  55. func TestBadConfig(t *testing.T) {
  56. testFile := "testdata/bad.conf"
  57. _, err := ParseToRawMap(testFile)
  58. if err == nil {
  59. t.Fatal("expected invalid config file to fail")
  60. }
  61. }
  62. func TestQuotedValue(t *testing.T) {
  63. testFile := "testdata/test.conf"
  64. cmap, _ := ParseToRawMap(testFile)
  65. val := cmap["sectionName"]["key4"]
  66. if val != " space at beginning and end " {
  67. t.Fatal("Wrong value in double quotes [", val, "]")
  68. }
  69. if !cmap.SectionInConfig("sectionName") {
  70. t.Fatal("expected SectionInConfig to return true")
  71. }
  72. val = cmap["sectionName"]["key5"]
  73. if val != " is quoted with single quotes " {
  74. t.Fatal("Wrong value in single quotes [", val, "]")
  75. }
  76. }
  77. func TestENoEnt(t *testing.T) {
  78. _, err := ParseToRawMap("testdata/enoent")
  79. if err == nil {
  80. t.Fatal("expected error on non-existent file")
  81. }
  82. }
  83. func TestLoadRoots(t *testing.T) {
  84. roots, err := Parse("testdata/roots.conf")
  85. if err != nil {
  86. t.Fatalf("%v", err)
  87. }
  88. if len(roots) != 2 {
  89. t.Fatal("expected one CA in the roots")
  90. }
  91. if root, ok := roots["primary"]; !ok {
  92. t.Fatal("expected a primary CA section")
  93. } else if _, ok := root.PrivateKey.(*rsa.PrivateKey); !ok {
  94. t.Fatal("expected an RSA private key")
  95. }
  96. }
  97. func TestLoadDERRoots(t *testing.T) {
  98. roots, err := Parse("testdata/roots_der.conf")
  99. if err != nil {
  100. t.Fatalf("%v", err)
  101. }
  102. if len(roots) != 2 {
  103. t.Fatal("expected one CA in the roots")
  104. }
  105. if root, ok := roots["primary"]; !ok {
  106. t.Fatal("expected a primary CA section")
  107. } else if _, ok := root.PrivateKey.(*rsa.PrivateKey); !ok {
  108. t.Fatal("expected an RSA private key")
  109. }
  110. }
  111. func TestLoadKSMRoot(t *testing.T) {
  112. _, err := Parse("testdata/roots_ksm.conf")
  113. if err == nil {
  114. t.Fatal("ksm specs are not supported yet")
  115. }
  116. }
  117. func TestLoadBadRootConfs(t *testing.T) {
  118. confs := []string{
  119. "testdata/roots_bad_db.conf",
  120. "testdata/roots_bad_certificate.conf",
  121. "testdata/roots_bad_private_key.conf",
  122. "testdata/roots_badconfig.conf",
  123. "testdata/roots_badspec.conf",
  124. "testdata/roots_badspec2.conf",
  125. "testdata/roots_badspec3.conf",
  126. "testdata/roots_bad_whitelist.conf",
  127. "testdata/roots_bad_whitelist.conf2",
  128. "testdata/roots_missing_certificate.conf",
  129. "testdata/roots_missing_certificate_entry.conf",
  130. "testdata/roots_missing_private_key.conf",
  131. "testdata/roots_missing_private_key_entry.conf",
  132. }
  133. for _, cf := range confs {
  134. _, err := Parse(cf)
  135. if err == nil {
  136. t.Fatalf("expected config file %s to fail", cf)
  137. }
  138. log.Debugf("%s: %v", cf, err)
  139. }
  140. }
  141. const confWhitelist = "testdata/roots_whitelist.conf"
  142. func TestLoadWhitelist(t *testing.T) {
  143. roots, err := Parse(confWhitelist)
  144. if err != nil {
  145. t.Fatalf("%v", err)
  146. }
  147. if roots["backup"].ACL != nil {
  148. t.Fatal("Expected a nil ACL for the backup root")
  149. }
  150. if roots["primary"].ACL == nil {
  151. t.Fatal("Expected a non-nil ACL for the primary root")
  152. }
  153. validIPs := [][]byte{
  154. {10, 0, 2, 3},
  155. {10, 0, 2, 247},
  156. {172, 16, 3, 9},
  157. {192, 168, 3, 15},
  158. }
  159. badIPs := [][]byte{
  160. {192, 168, 0, 1},
  161. {127, 0, 0, 1},
  162. {192, 168, 3, 14},
  163. {192, 168, 3, 16},
  164. {255, 255, 0, 1},
  165. {0, 0, 0, 0},
  166. }
  167. wl := roots["primary"].ACL
  168. for i := range validIPs {
  169. if !wl.Permitted(validIPs[i]) {
  170. t.Fatalf("ACL should have permitted IP %v", validIPs[i])
  171. }
  172. }
  173. for i := range badIPs {
  174. if wl.Permitted(badIPs[i]) {
  175. t.Fatalf("ACL should not have permitted IP %v", badIPs[i])
  176. }
  177. }
  178. }
  179. const confWhitelistIPv6 = "testdata/roots_whitelist_ipv6.conf"
  180. func TestLoadIPv6Whitelist(t *testing.T) {
  181. roots, err := Parse(confWhitelistIPv6)
  182. if err != nil {
  183. t.Fatalf("%v", err)
  184. }
  185. if roots["backup"].ACL != nil {
  186. t.Fatal("Expected a nil ACL for the backup root")
  187. }
  188. if roots["primary"].ACL == nil {
  189. t.Fatal("Expected a non-nil ACL for the primary root")
  190. }
  191. validIPs := [][]byte{
  192. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  193. {253, 77, 152, 85, 16, 29, 230, 139, 0, 0, 0, 0, 0, 0, 0, 1},
  194. {253, 77, 152, 85, 16, 29, 230, 139, 0, 0, 0, 0, 32, 0, 0, 1},
  195. {10, 0, 4, 18},
  196. }
  197. badIPs := [][]byte{
  198. {192, 168, 0, 1},
  199. {127, 0, 0, 1},
  200. {192, 168, 3, 14},
  201. {192, 168, 3, 16},
  202. {255, 255, 0, 1},
  203. {0, 0, 0, 0},
  204. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
  205. {253, 77, 152, 85, 16, 29, 230, 140, 0, 0, 0, 0, 0, 0, 0, 1},
  206. {254, 77, 152, 85, 16, 29, 230, 139, 0, 0, 0, 0, 0, 0, 0, 1},
  207. }
  208. wl := roots["primary"].ACL
  209. for i := range validIPs {
  210. if !wl.Permitted(validIPs[i]) {
  211. t.Fatalf("ACL should have permitted IP %v", validIPs[i])
  212. }
  213. }
  214. for i := range badIPs {
  215. if wl.Permitted(badIPs[i]) {
  216. t.Fatalf("ACL should not have permitted IP %v", badIPs[i])
  217. }
  218. }
  219. }
  220. const confDBConfig = "testdata/roots_db.conf"
  221. func TestLoadDBConfig(t *testing.T) {
  222. roots, err := Parse(confDBConfig)
  223. if err != nil {
  224. t.Fatalf("%v", err)
  225. }
  226. if roots["backup"].DB != nil {
  227. t.Fatal("Expected a nil DB for the backup root")
  228. }
  229. if roots["primary"].DB == nil {
  230. t.Fatal("Expected a non-nil DB for the primary root")
  231. }
  232. }