config_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "testing"
  6. "time"
  7. )
  8. var expiry = 1 * time.Minute
  9. var invalidProfileConfig = &Config{
  10. Signing: &Signing{
  11. Profiles: map[string]*SigningProfile{
  12. "invalid": {
  13. Usage: []string{"wiretapping"},
  14. Expiry: expiry,
  15. },
  16. "empty": {},
  17. },
  18. Default: &SigningProfile{
  19. Usage: []string{"digital signature"},
  20. Expiry: expiry,
  21. },
  22. },
  23. }
  24. var invalidDefaultConfig = &Config{
  25. Signing: &Signing{
  26. Profiles: map[string]*SigningProfile{
  27. "key usage": {
  28. Usage: []string{"digital signature"},
  29. },
  30. },
  31. Default: &SigningProfile{
  32. Usage: []string{"s/mime"},
  33. },
  34. },
  35. }
  36. var validConfig = &Config{
  37. Signing: &Signing{
  38. Profiles: map[string]*SigningProfile{
  39. "valid": {
  40. Usage: []string{"digital signature"},
  41. Expiry: expiry,
  42. },
  43. },
  44. Default: &SigningProfile{
  45. Usage: []string{"digital signature"},
  46. Expiry: expiry,
  47. },
  48. },
  49. }
  50. var validMixedConfig = `
  51. {
  52. "signing": {
  53. "profiles": {
  54. "CA": {
  55. "auth_key": "sample",
  56. "remote": "localhost"
  57. },
  58. "email": {
  59. "usages": ["s/mime"],
  60. "expiry": "720h"
  61. }
  62. },
  63. "default": {
  64. "usages": ["digital signature", "email protection"],
  65. "expiry": "8000h"
  66. }
  67. },
  68. "auth_keys": {
  69. "sample": {
  70. "type":"standard",
  71. "key":"0123456789ABCDEF0123456789ABCDEF"
  72. }
  73. },
  74. "remotes": {
  75. "localhost": "127.0.0.1:8888"
  76. }
  77. }`
  78. var validMinimalRemoteConfig = `
  79. {
  80. "signing": {
  81. "default": {
  82. "auth_key": "sample",
  83. "remote": "localhost"
  84. }
  85. },
  86. "auth_keys": {
  87. "sample": {
  88. "type":"standard",
  89. "key":"0123456789ABCDEF0123456789ABCDEF"
  90. }
  91. },
  92. "remotes": {
  93. "localhost": "127.0.0.1:8888"
  94. }
  95. }`
  96. var validMinimalRemoteConfig2 = `
  97. {
  98. "signing": {
  99. "default": {
  100. "auth_remote":{
  101. "auth_key": "sample",
  102. "remote": "localhost"
  103. }
  104. }
  105. },
  106. "auth_keys": {
  107. "sample": {
  108. "type":"standard",
  109. "key":"0123456789ABCDEF0123456789ABCDEF"
  110. }
  111. },
  112. "remotes": {
  113. "localhost": "127.0.0.1:8888"
  114. }
  115. }`
  116. var invalidConflictRemoteConfig = `
  117. {
  118. "signing": {
  119. "default": {
  120. "auth_remote":{
  121. "auth_key": "sample",
  122. "remote": "localhost"
  123. },
  124. "remote": "localhost"
  125. }
  126. },
  127. "auth_keys": {
  128. "sample": {
  129. "type":"standard",
  130. "key":"0123456789ABCDEF0123456789ABCDEF"
  131. }
  132. },
  133. "remotes": {
  134. "localhost": "127.0.0.1:8888"
  135. }
  136. }`
  137. var invalidRemoteConfig = `
  138. {
  139. "signing": {
  140. "default": {
  141. "auth_remotes_typos":{
  142. "auth_key": "sample",
  143. "remote": "localhost"
  144. }
  145. }
  146. },
  147. "auth_keys": {
  148. "sample": {
  149. "type":"standard",
  150. "key":"0123456789ABCDEF0123456789ABCDEF"
  151. }
  152. },
  153. "remotes": {
  154. "localhost": "127.0.0.1:8888"
  155. }
  156. }`
  157. var invalidAuthRemoteConfigMissingRemote = `
  158. {
  159. "signing": {
  160. "default": {
  161. "auth_remote":{
  162. "auth_key": "sample"
  163. }
  164. }
  165. },
  166. "auth_keys": {
  167. "sample": {
  168. "type":"standard",
  169. "key":"0123456789ABCDEF0123456789ABCDEF"
  170. }
  171. },
  172. "remotes": {
  173. "localhost": "127.0.0.1:8888"
  174. }
  175. }`
  176. var invalidAuthRemoteConfigMissingKey = `
  177. {
  178. "signing": {
  179. "default": {
  180. "auth_remote":{
  181. "remote": "localhost"
  182. }
  183. }
  184. },
  185. "auth_keys": {
  186. "sample": {
  187. "type":"standard",
  188. "key":"0123456789ABCDEF0123456789ABCDEF"
  189. }
  190. },
  191. "remotes": {
  192. "localhost": "127.0.0.1:8888"
  193. }
  194. }`
  195. var validMinimalLocalConfig = `
  196. {
  197. "signing": {
  198. "default": {
  199. "usages": ["digital signature", "email protection"],
  200. "expiry": "8000h"
  201. }
  202. }
  203. }`
  204. var validLocalConfigsWithCAConstraint = []string{
  205. `{
  206. "signing": {
  207. "default": {
  208. "usages": ["digital signature", "email protection"],
  209. "ca_constraint": { "is_ca": true },
  210. "expiry": "8000h"
  211. }
  212. }
  213. }`,
  214. `{
  215. "signing": {
  216. "default": {
  217. "usages": ["digital signature", "email protection"],
  218. "ca_constraint": { "is_ca": true, "max_path_len": 1 },
  219. "expiry": "8000h"
  220. }
  221. }
  222. }`,
  223. `{
  224. "signing": {
  225. "default": {
  226. "usages": ["digital signature", "email protection"],
  227. "ca_constraint": { "is_ca": true, "max_path_len_zero": true },
  228. "expiry": "8000h"
  229. }
  230. }
  231. }`,
  232. }
  233. func TestInvalidProfile(t *testing.T) {
  234. if invalidProfileConfig.Signing.Profiles["invalid"].validProfile(false) {
  235. t.Fatal("invalid profile accepted as valid")
  236. }
  237. if invalidProfileConfig.Signing.Profiles["empty"].validProfile(false) {
  238. t.Fatal("invalid profile accepted as valid")
  239. }
  240. if invalidProfileConfig.Valid() {
  241. t.Fatal("invalid config accepted as valid")
  242. }
  243. if !invalidProfileConfig.Signing.Profiles["invalid"].validProfile(true) {
  244. t.Fatal("invalid profile should be a valid default profile")
  245. }
  246. }
  247. func TestRemoteProfiles(t *testing.T) {
  248. var validRemoteProfile = &SigningProfile{
  249. RemoteName: "localhost",
  250. RemoteServer: "localhost:8080",
  251. }
  252. var invalidRemoteProfile = &SigningProfile{
  253. RemoteName: "localhost",
  254. }
  255. var invalidRemoteAuthProfile = &SigningProfile{
  256. RemoteName: "localhost",
  257. RemoteServer: "localhost:8080",
  258. AuthKeyName: "blahblah",
  259. }
  260. if !validRemoteProfile.validProfile(true) ||
  261. !validRemoteProfile.validProfile(false) {
  262. t.Fatal("valid remote profile is rejected.")
  263. }
  264. if invalidRemoteProfile.validProfile(true) ||
  265. invalidRemoteProfile.validProfile(false) {
  266. t.Fatal("invalid remote profile is accepted.")
  267. }
  268. if invalidRemoteAuthProfile.validProfile(true) ||
  269. invalidRemoteAuthProfile.validProfile(false) {
  270. t.Fatal("invalid remote profile is accepted.")
  271. }
  272. }
  273. func TestInvalidDefault(t *testing.T) {
  274. if invalidDefaultConfig.Signing.Default.validProfile(true) {
  275. t.Fatal("invalid default accepted as valid")
  276. }
  277. if invalidDefaultConfig.Valid() {
  278. t.Fatal("invalid config accepted as valid")
  279. }
  280. if !invalidDefaultConfig.Signing.Default.validProfile(false) {
  281. t.Fatal("invalid default profile should be a valid profile")
  282. }
  283. }
  284. func TestValidConfig(t *testing.T) {
  285. if !validConfig.Valid() {
  286. t.Fatal("Valid config is not valid")
  287. }
  288. bytes, _ := json.Marshal(validConfig)
  289. fmt.Printf("%v", string(bytes))
  290. }
  291. func TestDefaultConfig(t *testing.T) {
  292. if !DefaultConfig().validProfile(false) {
  293. t.Fatal("global default signing profile should be a valid profile.")
  294. }
  295. if !DefaultConfig().validProfile(true) {
  296. t.Fatal("global default signing profile should be a valid default profile")
  297. }
  298. }
  299. func TestParse(t *testing.T) {
  300. var validProfiles = []*SigningProfile{
  301. {
  302. ExpiryString: "8760h",
  303. },
  304. {
  305. ExpiryString: "168h",
  306. },
  307. {
  308. ExpiryString: "300s",
  309. },
  310. }
  311. var invalidProfiles = []*SigningProfile{
  312. nil,
  313. {},
  314. {
  315. ExpiryString: "",
  316. },
  317. {
  318. ExpiryString: "365d",
  319. },
  320. {
  321. ExpiryString: "1y",
  322. },
  323. {
  324. ExpiryString: "one year",
  325. },
  326. }
  327. for _, p := range validProfiles {
  328. if p.populate(nil) != nil {
  329. t.Fatalf("Failed to parse ExpiryString=%s", p.ExpiryString)
  330. }
  331. }
  332. for _, p := range invalidProfiles {
  333. if p.populate(nil) == nil {
  334. if p != nil {
  335. t.Fatalf("ExpiryString=%s should not be parseable", p.ExpiryString)
  336. }
  337. t.Fatalf("Nil profile should not be parseable")
  338. }
  339. }
  340. }
  341. func TestLoadFile(t *testing.T) {
  342. validConfigFiles := []string{
  343. "testdata/valid_config.json",
  344. "testdata/valid_config_auth.json",
  345. "testdata/valid_config_no_default.json",
  346. "testdata/valid_config_auth_no_default.json",
  347. }
  348. for _, configFile := range validConfigFiles {
  349. _, err := LoadFile(configFile)
  350. if err != nil {
  351. t.Fatal("Load valid config file failed.", configFile, "error is ", err)
  352. }
  353. }
  354. }
  355. func TestLoadInvalidConfigFile(t *testing.T) {
  356. invalidConfigFiles := []string{"", "testdata/no_such_file",
  357. "testdata/invalid_default.json",
  358. "testdata/invalid_profiles.json",
  359. "testdata/invalid_usage.json",
  360. "testdata/invalid_config.json",
  361. "testdata/invalid_auth.json",
  362. "testdata/invalid_auth_bad_key.json",
  363. "testdata/invalid_no_auth_keys.json",
  364. "testdata/invalid_remote.json",
  365. "testdata/invalid_no_remotes.json",
  366. }
  367. for _, configFile := range invalidConfigFiles {
  368. _, err := LoadFile(configFile)
  369. if err == nil {
  370. t.Fatal("Invalid config is loaded.", configFile)
  371. }
  372. }
  373. }
  374. func TestNeedLocalSigner(t *testing.T) {
  375. c, err := LoadConfig([]byte(validMixedConfig))
  376. if err != nil {
  377. t.Fatal("load valid config failed:", err)
  378. }
  379. // This signing config needs both local signer and remote signer.
  380. if c.Signing.NeedsLocalSigner() != true {
  381. t.Fatal("incorrect NeedsLocalSigner().")
  382. }
  383. if c.Signing.NeedsRemoteSigner() != true {
  384. t.Fatal("incorrect NeedsRemoteSigner()")
  385. }
  386. remoteConfig, err := LoadConfig([]byte(validMinimalRemoteConfig))
  387. if err != nil {
  388. t.Fatal("Load valid config failed:", err)
  389. }
  390. if remoteConfig.Signing.NeedsLocalSigner() != false {
  391. t.Fatal("incorrect NeedsLocalSigner().")
  392. }
  393. if remoteConfig.Signing.NeedsRemoteSigner() != true {
  394. t.Fatal("incorrect NeedsRemoteSigner().")
  395. }
  396. localConfig, err := LoadConfig([]byte(validMinimalLocalConfig))
  397. if localConfig.Signing.NeedsLocalSigner() != true {
  398. t.Fatal("incorrect NeedsLocalSigner().")
  399. }
  400. if localConfig.Signing.NeedsRemoteSigner() != false {
  401. t.Fatal("incorrect NeedsRemoteSigner().")
  402. }
  403. if err != nil {
  404. t.Fatal(err)
  405. }
  406. }
  407. func TestOverrideRemotes(t *testing.T) {
  408. c, err := LoadConfig([]byte(validMixedConfig))
  409. if err != nil {
  410. t.Fatal("load valid config failed:", err)
  411. }
  412. host := "localhost:8888"
  413. c.Signing.OverrideRemotes(host)
  414. if c.Signing.Default.RemoteServer != host {
  415. t.Fatal("should override default profile's RemoteServer")
  416. }
  417. for _, p := range c.Signing.Profiles {
  418. if p.RemoteServer != host {
  419. t.Fatal("failed to override profile's RemoteServer")
  420. }
  421. }
  422. }
  423. func TestAuthRemoteConfig(t *testing.T) {
  424. c, err := LoadConfig([]byte(validMinimalRemoteConfig2))
  425. if err != nil {
  426. t.Fatal("load valid config failed:", err)
  427. }
  428. if c.Signing.Default.RemoteServer != "127.0.0.1:8888" {
  429. t.Fatal("load valid config failed: incorrect remote server")
  430. }
  431. host := "localhost:8888"
  432. c.Signing.OverrideRemotes(host)
  433. if c.Signing.Default.RemoteServer != host {
  434. t.Fatal("should override default profile's RemoteServer")
  435. }
  436. for _, p := range c.Signing.Profiles {
  437. if p.RemoteServer != host {
  438. t.Fatal("failed to override profile's RemoteServer")
  439. }
  440. }
  441. }
  442. func TestDuplicateRemoteConfig(t *testing.T) {
  443. _, err := LoadConfig([]byte(invalidConflictRemoteConfig))
  444. if err == nil {
  445. t.Fatal("fail to reject invalid config")
  446. }
  447. }
  448. func TestBadAuthRemoteConfig(t *testing.T) {
  449. _, err := LoadConfig([]byte(invalidRemoteConfig))
  450. if err == nil {
  451. t.Fatal("load invalid config should failed")
  452. }
  453. _, err = LoadConfig([]byte(invalidAuthRemoteConfigMissingRemote))
  454. if err == nil {
  455. t.Fatal("load invalid config should failed")
  456. }
  457. _, err = LoadConfig([]byte(invalidAuthRemoteConfigMissingKey))
  458. if err == nil {
  459. t.Fatal("load invalid config should failed")
  460. }
  461. var p *Signing
  462. if p.Valid() {
  463. t.Fatal("nil Signing config should be invalid")
  464. }
  465. }
  466. func TestValidCAConstraint(t *testing.T) {
  467. for _, config := range validLocalConfigsWithCAConstraint {
  468. _, err := LoadConfig([]byte(config))
  469. if err != nil {
  470. t.Fatal("can't parse valid ca constraint")
  471. }
  472. }
  473. }