acmeClient_test.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. package ca
  2. import (
  3. "crypto/x509"
  4. "encoding/base64"
  5. "encoding/json"
  6. "encoding/pem"
  7. "io/ioutil"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. "time"
  12. "github.com/pkg/errors"
  13. "github.com/smallstep/assert"
  14. "github.com/smallstep/certificates/acme"
  15. acmeAPI "github.com/smallstep/certificates/acme/api"
  16. "github.com/smallstep/certificates/api"
  17. "go.step.sm/crypto/jose"
  18. "go.step.sm/crypto/pemutil"
  19. )
  20. func TestNewACMEClient(t *testing.T) {
  21. type test struct {
  22. ops []ClientOption
  23. r1, r2 interface{}
  24. rc1, rc2 int
  25. err error
  26. }
  27. srv := httptest.NewServer(nil)
  28. defer srv.Close()
  29. dir := acmeAPI.Directory{
  30. NewNonce: srv.URL + "/foo",
  31. NewAccount: srv.URL + "/bar",
  32. NewOrder: srv.URL + "/baz",
  33. NewAuthz: srv.URL + "/zap",
  34. RevokeCert: srv.URL + "/zip",
  35. KeyChange: srv.URL + "/blorp",
  36. }
  37. acc := acme.Account{
  38. Contact: []string{"max", "mariano"},
  39. Status: "valid",
  40. OrdersURL: "orders-url",
  41. }
  42. tests := map[string]func(t *testing.T) test{
  43. "fail/client-option-error": func(t *testing.T) test {
  44. return test{
  45. ops: []ClientOption{
  46. func(o *clientOptions) error {
  47. return errors.New("force")
  48. },
  49. },
  50. err: errors.New("force"),
  51. }
  52. },
  53. "fail/get-directory": func(t *testing.T) test {
  54. return test{
  55. ops: []ClientOption{WithTransport(http.DefaultTransport)},
  56. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  57. rc1: 400,
  58. err: errors.New("The request message was malformed"),
  59. }
  60. },
  61. "fail/bad-directory": func(t *testing.T) test {
  62. return test{
  63. ops: []ClientOption{WithTransport(http.DefaultTransport)},
  64. r1: "foo",
  65. rc1: 200,
  66. err: errors.New("error reading http://127.0.0.1"),
  67. }
  68. },
  69. "fail/error-post-newAccount": func(t *testing.T) test {
  70. return test{
  71. ops: []ClientOption{WithTransport(http.DefaultTransport)},
  72. r1: dir,
  73. rc1: 200,
  74. r2: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  75. rc2: 400,
  76. err: errors.New("Account does not exist"),
  77. }
  78. },
  79. "fail/error-bad-account": func(t *testing.T) test {
  80. return test{
  81. ops: []ClientOption{WithTransport(http.DefaultTransport)},
  82. r1: dir,
  83. rc1: 200,
  84. r2: "foo",
  85. rc2: 200,
  86. err: errors.New("error reading http://127.0.0.1"),
  87. }
  88. },
  89. "ok": func(t *testing.T) test {
  90. return test{
  91. ops: []ClientOption{WithTransport(http.DefaultTransport)},
  92. r1: dir,
  93. rc1: 200,
  94. r2: acc,
  95. rc2: 200,
  96. }
  97. },
  98. }
  99. accLocation := "linkitylink"
  100. for name, run := range tests {
  101. t.Run(name, func(t *testing.T) {
  102. tc := run(t)
  103. i := 0
  104. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  105. switch {
  106. case i == 0:
  107. api.JSONStatus(w, tc.r1, tc.rc1)
  108. i++
  109. case i == 1:
  110. w.Header().Set("Replay-Nonce", "abc123")
  111. api.JSONStatus(w, []byte{}, 200)
  112. i++
  113. default:
  114. w.Header().Set("Location", accLocation)
  115. api.JSONStatus(w, tc.r2, tc.rc2)
  116. }
  117. })
  118. if client, err := NewACMEClient(srv.URL, []string{"max", "mariano"}, tc.ops...); err != nil {
  119. if assert.NotNil(t, tc.err) {
  120. assert.HasPrefix(t, err.Error(), tc.err.Error())
  121. }
  122. } else {
  123. if assert.Nil(t, tc.err) {
  124. assert.Equals(t, *client.dir, dir)
  125. assert.NotNil(t, client.Key)
  126. assert.NotNil(t, client.acc)
  127. assert.Equals(t, client.kid, accLocation)
  128. }
  129. }
  130. })
  131. }
  132. }
  133. func TestACMEClient_GetDirectory(t *testing.T) {
  134. c := &ACMEClient{
  135. dir: &acmeAPI.Directory{
  136. NewNonce: "/foo",
  137. NewAccount: "/bar",
  138. NewOrder: "/baz",
  139. NewAuthz: "/zap",
  140. RevokeCert: "/zip",
  141. KeyChange: "/blorp",
  142. },
  143. }
  144. dir, err := c.GetDirectory()
  145. assert.FatalError(t, err)
  146. assert.Equals(t, c.dir, dir)
  147. }
  148. func TestACMEClient_GetNonce(t *testing.T) {
  149. type test struct {
  150. r1 interface{}
  151. rc1 int
  152. err error
  153. }
  154. srv := httptest.NewServer(nil)
  155. defer srv.Close()
  156. dir := acmeAPI.Directory{
  157. NewNonce: srv.URL + "/foo",
  158. }
  159. // Retrieve transport from options.
  160. o := new(clientOptions)
  161. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  162. tr, err := o.getTransport(srv.URL)
  163. assert.FatalError(t, err)
  164. ac := &ACMEClient{
  165. client: &http.Client{
  166. Transport: tr,
  167. },
  168. dirLoc: srv.URL,
  169. dir: &dir,
  170. }
  171. tests := map[string]func(t *testing.T) test{
  172. "fail/GET-nonce": func(t *testing.T) test {
  173. return test{
  174. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  175. rc1: 400,
  176. err: errors.New("The request message was malformed"),
  177. }
  178. },
  179. "ok": func(t *testing.T) test {
  180. return test{
  181. r1: []byte{},
  182. rc1: 200,
  183. }
  184. },
  185. }
  186. expectedNonce := "abc123"
  187. for name, run := range tests {
  188. t.Run(name, func(t *testing.T) {
  189. tc := run(t)
  190. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  191. w.Header().Set("Replay-Nonce", expectedNonce)
  192. api.JSONStatus(w, tc.r1, tc.rc1)
  193. })
  194. if nonce, err := ac.GetNonce(); err != nil {
  195. if assert.NotNil(t, tc.err) {
  196. assert.HasPrefix(t, err.Error(), tc.err.Error())
  197. }
  198. } else {
  199. if assert.Nil(t, tc.err) {
  200. assert.Equals(t, expectedNonce, nonce)
  201. }
  202. }
  203. })
  204. }
  205. }
  206. func TestACMEClient_post(t *testing.T) {
  207. type test struct {
  208. payload []byte
  209. Key *jose.JSONWebKey
  210. ops []withHeaderOption
  211. r1, r2 interface{}
  212. rc1, rc2 int
  213. jwkInJWS bool
  214. client *ACMEClient
  215. err error
  216. }
  217. srv := httptest.NewServer(nil)
  218. defer srv.Close()
  219. dir := acmeAPI.Directory{
  220. NewNonce: srv.URL + "/foo",
  221. }
  222. // Retrieve transport from options.
  223. o := new(clientOptions)
  224. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  225. tr, err := o.getTransport(srv.URL)
  226. assert.FatalError(t, err)
  227. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  228. assert.FatalError(t, err)
  229. acc := acme.Account{
  230. Contact: []string{"max", "mariano"},
  231. Status: "valid",
  232. OrdersURL: "orders-url",
  233. }
  234. ac := &ACMEClient{
  235. client: &http.Client{
  236. Transport: tr,
  237. },
  238. dirLoc: srv.URL,
  239. dir: &dir,
  240. Key: jwk,
  241. kid: "foobar",
  242. }
  243. tests := map[string]func(t *testing.T) test{
  244. "fail/account-not-configured": func(t *testing.T) test {
  245. return test{
  246. client: &ACMEClient{},
  247. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  248. rc1: 400,
  249. err: errors.New("acme client not configured with account"),
  250. }
  251. },
  252. "fail/GET-nonce": func(t *testing.T) test {
  253. return test{
  254. client: ac,
  255. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  256. rc1: 400,
  257. err: errors.New("The request message was malformed"),
  258. }
  259. },
  260. "ok/jwk": func(t *testing.T) test {
  261. return test{
  262. client: ac,
  263. r1: []byte{},
  264. rc1: 200,
  265. r2: acc,
  266. rc2: 200,
  267. ops: []withHeaderOption{withJWK(ac)},
  268. jwkInJWS: true,
  269. }
  270. },
  271. "ok/kid": func(t *testing.T) test {
  272. return test{
  273. client: ac,
  274. r1: []byte{},
  275. rc1: 200,
  276. r2: acc,
  277. rc2: 200,
  278. ops: []withHeaderOption{withKid(ac)},
  279. }
  280. },
  281. }
  282. expectedNonce := "abc123"
  283. url := srv.URL + "/foo"
  284. for name, run := range tests {
  285. t.Run(name, func(t *testing.T) {
  286. tc := run(t)
  287. i := 0
  288. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  289. w.Header().Set("Replay-Nonce", expectedNonce)
  290. if i == 0 {
  291. api.JSONStatus(w, tc.r1, tc.rc1)
  292. i++
  293. return
  294. }
  295. // validate jws request protected headers and body
  296. body, err := ioutil.ReadAll(req.Body)
  297. assert.FatalError(t, err)
  298. jws, err := jose.ParseJWS(string(body))
  299. assert.FatalError(t, err)
  300. hdr := jws.Signatures[0].Protected
  301. assert.Equals(t, hdr.Nonce, expectedNonce)
  302. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  303. assert.Fatal(t, ok)
  304. assert.Equals(t, jwsURL, url)
  305. if tc.jwkInJWS {
  306. assert.Equals(t, hdr.JSONWebKey.KeyID, ac.Key.KeyID)
  307. } else {
  308. assert.Equals(t, hdr.KeyID, ac.kid)
  309. }
  310. api.JSONStatus(w, tc.r2, tc.rc2)
  311. })
  312. if resp, err := tc.client.post(tc.payload, url, tc.ops...); err != nil {
  313. if assert.NotNil(t, tc.err) {
  314. assert.HasPrefix(t, err.Error(), tc.err.Error())
  315. }
  316. } else {
  317. if assert.Nil(t, tc.err) {
  318. var res acme.Account
  319. assert.FatalError(t, readJSON(resp.Body, &res))
  320. assert.Equals(t, res, acc)
  321. }
  322. }
  323. })
  324. }
  325. }
  326. func TestACMEClient_NewOrder(t *testing.T) {
  327. type test struct {
  328. ops []withHeaderOption
  329. r1, r2 interface{}
  330. rc1, rc2 int
  331. err error
  332. }
  333. srv := httptest.NewServer(nil)
  334. defer srv.Close()
  335. dir := acmeAPI.Directory{
  336. NewNonce: srv.URL + "/foo",
  337. NewOrder: srv.URL + "/bar",
  338. }
  339. // Retrieve transport from options.
  340. o := new(clientOptions)
  341. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  342. tr, err := o.getTransport(srv.URL)
  343. assert.FatalError(t, err)
  344. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  345. assert.FatalError(t, err)
  346. now := time.Now().UTC().Round(time.Second)
  347. nor := acmeAPI.NewOrderRequest{
  348. Identifiers: []acme.Identifier{
  349. {Type: "dns", Value: "example.com"},
  350. {Type: "dns", Value: "acme.example.com"},
  351. },
  352. NotBefore: now,
  353. NotAfter: now.Add(time.Minute),
  354. }
  355. norb, err := json.Marshal(nor)
  356. assert.FatalError(t, err)
  357. ord := acme.Order{
  358. Status: "valid",
  359. ExpiresAt: now, // "soon"
  360. FinalizeURL: "finalize-url",
  361. }
  362. ac := &ACMEClient{
  363. client: &http.Client{
  364. Transport: tr,
  365. },
  366. dirLoc: srv.URL,
  367. dir: &dir,
  368. Key: jwk,
  369. kid: "foobar",
  370. }
  371. tests := map[string]func(t *testing.T) test{
  372. "fail/client-post": func(t *testing.T) test {
  373. return test{
  374. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  375. rc1: 400,
  376. err: errors.New("The request message was malformed"),
  377. }
  378. },
  379. "fail/newOrder-error": func(t *testing.T) test {
  380. return test{
  381. r1: []byte{},
  382. rc1: 200,
  383. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  384. rc2: 400,
  385. ops: []withHeaderOption{withKid(ac)},
  386. err: errors.New("The request message was malformed"),
  387. }
  388. },
  389. "fail/bad-order": func(t *testing.T) test {
  390. return test{
  391. r1: []byte{},
  392. rc1: 200,
  393. r2: "foo",
  394. rc2: 200,
  395. ops: []withHeaderOption{withKid(ac)},
  396. err: errors.New("error reading http://127.0.0.1"),
  397. }
  398. },
  399. "ok": func(t *testing.T) test {
  400. return test{
  401. r1: []byte{},
  402. rc1: 200,
  403. r2: ord,
  404. rc2: 200,
  405. ops: []withHeaderOption{withKid(ac)},
  406. }
  407. },
  408. }
  409. expectedNonce := "abc123"
  410. for name, run := range tests {
  411. t.Run(name, func(t *testing.T) {
  412. tc := run(t)
  413. i := 0
  414. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  415. w.Header().Set("Replay-Nonce", expectedNonce)
  416. if i == 0 {
  417. api.JSONStatus(w, tc.r1, tc.rc1)
  418. i++
  419. return
  420. }
  421. // validate jws request protected headers and body
  422. body, err := ioutil.ReadAll(req.Body)
  423. assert.FatalError(t, err)
  424. jws, err := jose.ParseJWS(string(body))
  425. assert.FatalError(t, err)
  426. hdr := jws.Signatures[0].Protected
  427. assert.Equals(t, hdr.Nonce, expectedNonce)
  428. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  429. assert.Fatal(t, ok)
  430. assert.Equals(t, jwsURL, dir.NewOrder)
  431. assert.Equals(t, hdr.KeyID, ac.kid)
  432. payload, err := jws.Verify(ac.Key.Public())
  433. assert.FatalError(t, err)
  434. assert.Equals(t, payload, norb)
  435. api.JSONStatus(w, tc.r2, tc.rc2)
  436. })
  437. if res, err := ac.NewOrder(norb); err != nil {
  438. if assert.NotNil(t, tc.err) {
  439. assert.HasPrefix(t, err.Error(), tc.err.Error())
  440. }
  441. } else {
  442. if assert.Nil(t, tc.err) {
  443. assert.Equals(t, *res, ord)
  444. }
  445. }
  446. })
  447. }
  448. }
  449. func TestACMEClient_GetOrder(t *testing.T) {
  450. type test struct {
  451. r1, r2 interface{}
  452. rc1, rc2 int
  453. err error
  454. }
  455. srv := httptest.NewServer(nil)
  456. defer srv.Close()
  457. dir := acmeAPI.Directory{
  458. NewNonce: srv.URL + "/foo",
  459. }
  460. // Retrieve transport from options.
  461. o := new(clientOptions)
  462. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  463. tr, err := o.getTransport(srv.URL)
  464. assert.FatalError(t, err)
  465. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  466. assert.FatalError(t, err)
  467. ord := acme.Order{
  468. Status: "valid",
  469. ExpiresAt: time.Now().UTC().Round(time.Second), // "soon"
  470. FinalizeURL: "finalize-url",
  471. }
  472. ac := &ACMEClient{
  473. client: &http.Client{
  474. Transport: tr,
  475. },
  476. dirLoc: srv.URL,
  477. dir: &dir,
  478. Key: jwk,
  479. kid: "foobar",
  480. }
  481. tests := map[string]func(t *testing.T) test{
  482. "fail/client-post": func(t *testing.T) test {
  483. return test{
  484. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  485. rc1: 400,
  486. err: errors.New("The request message was malformed"),
  487. }
  488. },
  489. "fail/getOrder-error": func(t *testing.T) test {
  490. return test{
  491. r1: []byte{},
  492. rc1: 200,
  493. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  494. rc2: 400,
  495. err: errors.New("The request message was malformed"),
  496. }
  497. },
  498. "fail/bad-order": func(t *testing.T) test {
  499. return test{
  500. r1: []byte{},
  501. rc1: 200,
  502. r2: "foo",
  503. rc2: 200,
  504. err: errors.New("error reading http://127.0.0.1"),
  505. }
  506. },
  507. "ok": func(t *testing.T) test {
  508. return test{
  509. r1: []byte{},
  510. rc1: 200,
  511. r2: ord,
  512. rc2: 200,
  513. }
  514. },
  515. }
  516. expectedNonce := "abc123"
  517. url := srv.URL + "/hullaballoo"
  518. for name, run := range tests {
  519. t.Run(name, func(t *testing.T) {
  520. tc := run(t)
  521. i := 0
  522. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  523. w.Header().Set("Replay-Nonce", expectedNonce)
  524. if i == 0 {
  525. api.JSONStatus(w, tc.r1, tc.rc1)
  526. i++
  527. return
  528. }
  529. // validate jws request protected headers and body
  530. body, err := ioutil.ReadAll(req.Body)
  531. assert.FatalError(t, err)
  532. jws, err := jose.ParseJWS(string(body))
  533. assert.FatalError(t, err)
  534. hdr := jws.Signatures[0].Protected
  535. assert.Equals(t, hdr.Nonce, expectedNonce)
  536. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  537. assert.Fatal(t, ok)
  538. assert.Equals(t, jwsURL, url)
  539. assert.Equals(t, hdr.KeyID, ac.kid)
  540. payload, err := jws.Verify(ac.Key.Public())
  541. assert.FatalError(t, err)
  542. assert.Equals(t, len(payload), 0)
  543. api.JSONStatus(w, tc.r2, tc.rc2)
  544. })
  545. if res, err := ac.GetOrder(url); err != nil {
  546. if assert.NotNil(t, tc.err) {
  547. assert.HasPrefix(t, err.Error(), tc.err.Error())
  548. }
  549. } else {
  550. if assert.Nil(t, tc.err) {
  551. assert.Equals(t, *res, ord)
  552. }
  553. }
  554. })
  555. }
  556. }
  557. func TestACMEClient_GetAuthz(t *testing.T) {
  558. type test struct {
  559. r1, r2 interface{}
  560. rc1, rc2 int
  561. err error
  562. }
  563. srv := httptest.NewServer(nil)
  564. defer srv.Close()
  565. dir := acmeAPI.Directory{
  566. NewNonce: srv.URL + "/foo",
  567. }
  568. // Retrieve transport from options.
  569. o := new(clientOptions)
  570. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  571. tr, err := o.getTransport(srv.URL)
  572. assert.FatalError(t, err)
  573. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  574. assert.FatalError(t, err)
  575. az := acme.Authorization{
  576. Status: "valid",
  577. ExpiresAt: time.Now().UTC().Round(time.Second),
  578. Identifier: acme.Identifier{Type: "dns", Value: "example.com"},
  579. }
  580. ac := &ACMEClient{
  581. client: &http.Client{
  582. Transport: tr,
  583. },
  584. dirLoc: srv.URL,
  585. dir: &dir,
  586. Key: jwk,
  587. kid: "foobar",
  588. }
  589. tests := map[string]func(t *testing.T) test{
  590. "fail/client-post": func(t *testing.T) test {
  591. return test{
  592. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  593. rc1: 400,
  594. err: errors.New("The request message was malformed"),
  595. }
  596. },
  597. "fail/getChallenge-error": func(t *testing.T) test {
  598. return test{
  599. r1: []byte{},
  600. rc1: 200,
  601. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  602. rc2: 400,
  603. err: errors.New("The request message was malformed"),
  604. }
  605. },
  606. "fail/bad-challenge": func(t *testing.T) test {
  607. return test{
  608. r1: []byte{},
  609. rc1: 200,
  610. r2: "foo",
  611. rc2: 200,
  612. err: errors.New("error reading http://127.0.0.1"),
  613. }
  614. },
  615. "ok": func(t *testing.T) test {
  616. return test{
  617. r1: []byte{},
  618. rc1: 200,
  619. r2: az,
  620. rc2: 200,
  621. }
  622. },
  623. }
  624. expectedNonce := "abc123"
  625. url := srv.URL + "/hullaballoo"
  626. for name, run := range tests {
  627. t.Run(name, func(t *testing.T) {
  628. tc := run(t)
  629. i := 0
  630. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  631. w.Header().Set("Replay-Nonce", expectedNonce)
  632. if i == 0 {
  633. api.JSONStatus(w, tc.r1, tc.rc1)
  634. i++
  635. return
  636. }
  637. // validate jws request protected headers and body
  638. body, err := ioutil.ReadAll(req.Body)
  639. assert.FatalError(t, err)
  640. jws, err := jose.ParseJWS(string(body))
  641. assert.FatalError(t, err)
  642. hdr := jws.Signatures[0].Protected
  643. assert.Equals(t, hdr.Nonce, expectedNonce)
  644. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  645. assert.Fatal(t, ok)
  646. assert.Equals(t, jwsURL, url)
  647. assert.Equals(t, hdr.KeyID, ac.kid)
  648. payload, err := jws.Verify(ac.Key.Public())
  649. assert.FatalError(t, err)
  650. assert.Equals(t, len(payload), 0)
  651. api.JSONStatus(w, tc.r2, tc.rc2)
  652. })
  653. if res, err := ac.GetAuthz(url); err != nil {
  654. if assert.NotNil(t, tc.err) {
  655. assert.HasPrefix(t, err.Error(), tc.err.Error())
  656. }
  657. } else {
  658. if assert.Nil(t, tc.err) {
  659. assert.Equals(t, *res, az)
  660. }
  661. }
  662. })
  663. }
  664. }
  665. func TestACMEClient_GetChallenge(t *testing.T) {
  666. type test struct {
  667. r1, r2 interface{}
  668. rc1, rc2 int
  669. err error
  670. }
  671. srv := httptest.NewServer(nil)
  672. defer srv.Close()
  673. dir := acmeAPI.Directory{
  674. NewNonce: srv.URL + "/foo",
  675. }
  676. // Retrieve transport from options.
  677. o := new(clientOptions)
  678. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  679. tr, err := o.getTransport(srv.URL)
  680. assert.FatalError(t, err)
  681. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  682. assert.FatalError(t, err)
  683. ch := acme.Challenge{
  684. Type: "http-01",
  685. Status: "valid",
  686. Token: "foo",
  687. }
  688. ac := &ACMEClient{
  689. client: &http.Client{
  690. Transport: tr,
  691. },
  692. dirLoc: srv.URL,
  693. dir: &dir,
  694. Key: jwk,
  695. kid: "foobar",
  696. }
  697. tests := map[string]func(t *testing.T) test{
  698. "fail/client-post": func(t *testing.T) test {
  699. return test{
  700. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  701. rc1: 400,
  702. err: errors.New("The request message was malformed"),
  703. }
  704. },
  705. "fail/getChallenge-error": func(t *testing.T) test {
  706. return test{
  707. r1: []byte{},
  708. rc1: 200,
  709. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  710. rc2: 400,
  711. err: errors.New("The request message was malformed"),
  712. }
  713. },
  714. "fail/bad-challenge": func(t *testing.T) test {
  715. return test{
  716. r1: []byte{},
  717. rc1: 200,
  718. r2: "foo",
  719. rc2: 200,
  720. err: errors.New("error reading http://127.0.0.1"),
  721. }
  722. },
  723. "ok": func(t *testing.T) test {
  724. return test{
  725. r1: []byte{},
  726. rc1: 200,
  727. r2: ch,
  728. rc2: 200,
  729. }
  730. },
  731. }
  732. expectedNonce := "abc123"
  733. url := srv.URL + "/hullaballoo"
  734. for name, run := range tests {
  735. t.Run(name, func(t *testing.T) {
  736. tc := run(t)
  737. i := 0
  738. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  739. w.Header().Set("Replay-Nonce", expectedNonce)
  740. if i == 0 {
  741. api.JSONStatus(w, tc.r1, tc.rc1)
  742. i++
  743. return
  744. }
  745. // validate jws request protected headers and body
  746. body, err := ioutil.ReadAll(req.Body)
  747. assert.FatalError(t, err)
  748. jws, err := jose.ParseJWS(string(body))
  749. assert.FatalError(t, err)
  750. hdr := jws.Signatures[0].Protected
  751. assert.Equals(t, hdr.Nonce, expectedNonce)
  752. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  753. assert.Fatal(t, ok)
  754. assert.Equals(t, jwsURL, url)
  755. assert.Equals(t, hdr.KeyID, ac.kid)
  756. payload, err := jws.Verify(ac.Key.Public())
  757. assert.FatalError(t, err)
  758. assert.Equals(t, len(payload), 0)
  759. api.JSONStatus(w, tc.r2, tc.rc2)
  760. })
  761. if res, err := ac.GetChallenge(url); err != nil {
  762. if assert.NotNil(t, tc.err) {
  763. assert.HasPrefix(t, err.Error(), tc.err.Error())
  764. }
  765. } else {
  766. if assert.Nil(t, tc.err) {
  767. assert.Equals(t, *res, ch)
  768. }
  769. }
  770. })
  771. }
  772. }
  773. func TestACMEClient_ValidateChallenge(t *testing.T) {
  774. type test struct {
  775. r1, r2 interface{}
  776. rc1, rc2 int
  777. err error
  778. }
  779. srv := httptest.NewServer(nil)
  780. defer srv.Close()
  781. dir := acmeAPI.Directory{
  782. NewNonce: srv.URL + "/foo",
  783. }
  784. // Retrieve transport from options.
  785. o := new(clientOptions)
  786. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  787. tr, err := o.getTransport(srv.URL)
  788. assert.FatalError(t, err)
  789. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  790. assert.FatalError(t, err)
  791. ch := acme.Challenge{
  792. Type: "http-01",
  793. Status: "valid",
  794. Token: "foo",
  795. }
  796. ac := &ACMEClient{
  797. client: &http.Client{
  798. Transport: tr,
  799. },
  800. dirLoc: srv.URL,
  801. dir: &dir,
  802. Key: jwk,
  803. kid: "foobar",
  804. }
  805. tests := map[string]func(t *testing.T) test{
  806. "fail/client-post": func(t *testing.T) test {
  807. return test{
  808. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  809. rc1: 400,
  810. err: errors.New("The request message was malformed"),
  811. }
  812. },
  813. "fail/getChallenge-error": func(t *testing.T) test {
  814. return test{
  815. r1: []byte{},
  816. rc1: 200,
  817. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  818. rc2: 400,
  819. err: errors.New("The request message was malformed"),
  820. }
  821. },
  822. "fail/bad-challenge": func(t *testing.T) test {
  823. return test{
  824. r1: []byte{},
  825. rc1: 200,
  826. r2: "foo",
  827. rc2: 200,
  828. err: errors.New("error reading http://127.0.0.1"),
  829. }
  830. },
  831. "ok": func(t *testing.T) test {
  832. return test{
  833. r1: []byte{},
  834. rc1: 200,
  835. r2: ch,
  836. rc2: 200,
  837. }
  838. },
  839. }
  840. expectedNonce := "abc123"
  841. url := srv.URL + "/hullaballoo"
  842. for name, run := range tests {
  843. t.Run(name, func(t *testing.T) {
  844. tc := run(t)
  845. i := 0
  846. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  847. w.Header().Set("Replay-Nonce", expectedNonce)
  848. if i == 0 {
  849. api.JSONStatus(w, tc.r1, tc.rc1)
  850. i++
  851. return
  852. }
  853. // validate jws request protected headers and body
  854. body, err := ioutil.ReadAll(req.Body)
  855. assert.FatalError(t, err)
  856. jws, err := jose.ParseJWS(string(body))
  857. assert.FatalError(t, err)
  858. hdr := jws.Signatures[0].Protected
  859. assert.Equals(t, hdr.Nonce, expectedNonce)
  860. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  861. assert.Fatal(t, ok)
  862. assert.Equals(t, jwsURL, url)
  863. assert.Equals(t, hdr.KeyID, ac.kid)
  864. payload, err := jws.Verify(ac.Key.Public())
  865. assert.FatalError(t, err)
  866. assert.Equals(t, payload, []byte("{}"))
  867. api.JSONStatus(w, tc.r2, tc.rc2)
  868. })
  869. if err := ac.ValidateChallenge(url); err != nil {
  870. if assert.NotNil(t, tc.err) {
  871. assert.HasPrefix(t, err.Error(), tc.err.Error())
  872. }
  873. }
  874. })
  875. }
  876. }
  877. func TestACMEClient_FinalizeOrder(t *testing.T) {
  878. type test struct {
  879. r1, r2 interface{}
  880. rc1, rc2 int
  881. err error
  882. }
  883. srv := httptest.NewServer(nil)
  884. defer srv.Close()
  885. dir := acmeAPI.Directory{
  886. NewNonce: srv.URL + "/foo",
  887. }
  888. // Retrieve transport from options.
  889. o := new(clientOptions)
  890. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  891. tr, err := o.getTransport(srv.URL)
  892. assert.FatalError(t, err)
  893. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  894. assert.FatalError(t, err)
  895. ord := acme.Order{
  896. Status: "valid",
  897. ExpiresAt: time.Now(), // "soon"
  898. FinalizeURL: "finalize-url",
  899. CertificateURL: "cert-url",
  900. }
  901. _csr, err := pemutil.Read("../authority/testdata/certs/foo.csr")
  902. assert.FatalError(t, err)
  903. csr, ok := _csr.(*x509.CertificateRequest)
  904. assert.Fatal(t, ok)
  905. fr := acmeAPI.FinalizeRequest{CSR: base64.RawURLEncoding.EncodeToString(csr.Raw)}
  906. frb, err := json.Marshal(fr)
  907. assert.FatalError(t, err)
  908. ac := &ACMEClient{
  909. client: &http.Client{
  910. Transport: tr,
  911. },
  912. dirLoc: srv.URL,
  913. dir: &dir,
  914. Key: jwk,
  915. kid: "foobar",
  916. }
  917. tests := map[string]func(t *testing.T) test{
  918. "fail/client-post": func(t *testing.T) test {
  919. return test{
  920. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  921. rc1: 400,
  922. err: errors.New("The request message was malformed"),
  923. }
  924. },
  925. "fail/finalizeOrder-error": func(t *testing.T) test {
  926. return test{
  927. r1: []byte{},
  928. rc1: 200,
  929. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  930. rc2: 400,
  931. err: errors.New("The request message was malformed"),
  932. }
  933. },
  934. "fail/bad-order": func(t *testing.T) test {
  935. return test{
  936. r1: []byte{},
  937. rc1: 200,
  938. r2: "foo",
  939. rc2: 200,
  940. err: errors.New("error reading http://127.0.0.1"),
  941. }
  942. },
  943. "ok": func(t *testing.T) test {
  944. return test{
  945. r1: []byte{},
  946. rc1: 200,
  947. r2: ord,
  948. rc2: 200,
  949. }
  950. },
  951. }
  952. expectedNonce := "abc123"
  953. url := srv.URL + "/hullaballoo"
  954. for name, run := range tests {
  955. t.Run(name, func(t *testing.T) {
  956. tc := run(t)
  957. i := 0
  958. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  959. w.Header().Set("Replay-Nonce", expectedNonce)
  960. if i == 0 {
  961. api.JSONStatus(w, tc.r1, tc.rc1)
  962. i++
  963. return
  964. }
  965. // validate jws request protected headers and body
  966. body, err := ioutil.ReadAll(req.Body)
  967. assert.FatalError(t, err)
  968. jws, err := jose.ParseJWS(string(body))
  969. assert.FatalError(t, err)
  970. hdr := jws.Signatures[0].Protected
  971. assert.Equals(t, hdr.Nonce, expectedNonce)
  972. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  973. assert.Fatal(t, ok)
  974. assert.Equals(t, jwsURL, url)
  975. assert.Equals(t, hdr.KeyID, ac.kid)
  976. payload, err := jws.Verify(ac.Key.Public())
  977. assert.FatalError(t, err)
  978. assert.Equals(t, payload, frb)
  979. api.JSONStatus(w, tc.r2, tc.rc2)
  980. })
  981. if err := ac.FinalizeOrder(url, csr); err != nil {
  982. if assert.NotNil(t, tc.err) {
  983. assert.HasPrefix(t, err.Error(), tc.err.Error())
  984. }
  985. }
  986. })
  987. }
  988. }
  989. func TestACMEClient_GetAccountOrders(t *testing.T) {
  990. type test struct {
  991. r1, r2 interface{}
  992. rc1, rc2 int
  993. err error
  994. client *ACMEClient
  995. }
  996. srv := httptest.NewServer(nil)
  997. defer srv.Close()
  998. dir := acmeAPI.Directory{
  999. NewNonce: srv.URL + "/foo",
  1000. }
  1001. // Retrieve transport from options.
  1002. o := new(clientOptions)
  1003. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  1004. tr, err := o.getTransport(srv.URL)
  1005. assert.FatalError(t, err)
  1006. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  1007. assert.FatalError(t, err)
  1008. orders := []string{"foo", "bar", "baz"}
  1009. ac := &ACMEClient{
  1010. client: &http.Client{
  1011. Transport: tr,
  1012. },
  1013. dirLoc: srv.URL,
  1014. dir: &dir,
  1015. Key: jwk,
  1016. kid: "foobar",
  1017. acc: &acme.Account{
  1018. Contact: []string{"max", "mariano"},
  1019. Status: "valid",
  1020. OrdersURL: srv.URL + "/orders-url",
  1021. },
  1022. }
  1023. tests := map[string]func(t *testing.T) test{
  1024. "fail/account-not-configured": func(t *testing.T) test {
  1025. return test{
  1026. client: &ACMEClient{},
  1027. err: errors.New("acme client not configured with account"),
  1028. }
  1029. },
  1030. "fail/client-post": func(t *testing.T) test {
  1031. return test{
  1032. client: ac,
  1033. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  1034. rc1: 400,
  1035. err: errors.New("The request message was malformed"),
  1036. }
  1037. },
  1038. "fail/getAccountOrders-error": func(t *testing.T) test {
  1039. return test{
  1040. client: ac,
  1041. r1: []byte{},
  1042. rc1: 200,
  1043. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  1044. rc2: 400,
  1045. err: errors.New("The request message was malformed"),
  1046. }
  1047. },
  1048. "fail/bad-accountOrders": func(t *testing.T) test {
  1049. return test{
  1050. client: ac,
  1051. r1: []byte{},
  1052. rc1: 200,
  1053. r2: "foo",
  1054. rc2: 200,
  1055. err: errors.New("error reading http://127.0.0.1"),
  1056. }
  1057. },
  1058. "ok": func(t *testing.T) test {
  1059. return test{
  1060. client: ac,
  1061. r1: []byte{},
  1062. rc1: 200,
  1063. r2: orders,
  1064. rc2: 200,
  1065. }
  1066. },
  1067. }
  1068. expectedNonce := "abc123"
  1069. for name, run := range tests {
  1070. t.Run(name, func(t *testing.T) {
  1071. tc := run(t)
  1072. i := 0
  1073. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  1074. w.Header().Set("Replay-Nonce", expectedNonce)
  1075. if i == 0 {
  1076. api.JSONStatus(w, tc.r1, tc.rc1)
  1077. i++
  1078. return
  1079. }
  1080. // validate jws request protected headers and body
  1081. body, err := ioutil.ReadAll(req.Body)
  1082. assert.FatalError(t, err)
  1083. jws, err := jose.ParseJWS(string(body))
  1084. assert.FatalError(t, err)
  1085. hdr := jws.Signatures[0].Protected
  1086. assert.Equals(t, hdr.Nonce, expectedNonce)
  1087. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  1088. assert.Fatal(t, ok)
  1089. assert.Equals(t, jwsURL, ac.acc.OrdersURL)
  1090. assert.Equals(t, hdr.KeyID, ac.kid)
  1091. payload, err := jws.Verify(ac.Key.Public())
  1092. assert.FatalError(t, err)
  1093. assert.Equals(t, len(payload), 0)
  1094. api.JSONStatus(w, tc.r2, tc.rc2)
  1095. })
  1096. if res, err := tc.client.GetAccountOrders(); err != nil {
  1097. if assert.NotNil(t, tc.err) {
  1098. assert.HasPrefix(t, err.Error(), tc.err.Error())
  1099. }
  1100. } else {
  1101. if assert.Nil(t, tc.err) {
  1102. assert.Equals(t, res, orders)
  1103. }
  1104. }
  1105. })
  1106. }
  1107. }
  1108. func TestACMEClient_GetCertificate(t *testing.T) {
  1109. type test struct {
  1110. r1, r2 interface{}
  1111. certBytes []byte
  1112. rc1, rc2 int
  1113. err error
  1114. }
  1115. srv := httptest.NewServer(nil)
  1116. defer srv.Close()
  1117. dir := acmeAPI.Directory{
  1118. NewNonce: srv.URL + "/foo",
  1119. }
  1120. // Retrieve transport from options.
  1121. o := new(clientOptions)
  1122. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  1123. tr, err := o.getTransport(srv.URL)
  1124. assert.FatalError(t, err)
  1125. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  1126. assert.FatalError(t, err)
  1127. leaf, err := pemutil.ReadCertificate("../authority/testdata/certs/foo.crt")
  1128. assert.FatalError(t, err)
  1129. leafb := pem.EncodeToMemory(&pem.Block{
  1130. Type: "Certificate",
  1131. Bytes: leaf.Raw,
  1132. })
  1133. certBytes := append(leafb, leafb...)
  1134. certBytes = append(certBytes, leafb...)
  1135. ac := &ACMEClient{
  1136. client: &http.Client{
  1137. Transport: tr,
  1138. },
  1139. dirLoc: srv.URL,
  1140. dir: &dir,
  1141. Key: jwk,
  1142. kid: "foobar",
  1143. acc: &acme.Account{
  1144. Contact: []string{"max", "mariano"},
  1145. Status: "valid",
  1146. OrdersURL: srv.URL + "/orders-url",
  1147. },
  1148. }
  1149. tests := map[string]func(t *testing.T) test{
  1150. "fail/client-post": func(t *testing.T) test {
  1151. return test{
  1152. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  1153. rc1: 400,
  1154. err: errors.New("The request message was malformed"),
  1155. }
  1156. },
  1157. "fail/getAccountOrders-error": func(t *testing.T) test {
  1158. return test{
  1159. r1: []byte{},
  1160. rc1: 200,
  1161. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  1162. rc2: 400,
  1163. err: errors.New("The request message was malformed"),
  1164. }
  1165. },
  1166. "fail/bad-certificate": func(t *testing.T) test {
  1167. return test{
  1168. r1: []byte{},
  1169. rc1: 200,
  1170. r2: "foo",
  1171. rc2: 200,
  1172. err: errors.New("failed to parse any certificates from response"),
  1173. }
  1174. },
  1175. "ok": func(t *testing.T) test {
  1176. return test{
  1177. r1: []byte{},
  1178. rc1: 200,
  1179. certBytes: certBytes,
  1180. }
  1181. },
  1182. }
  1183. expectedNonce := "abc123"
  1184. url := srv.URL + "/cert/foo"
  1185. for name, run := range tests {
  1186. t.Run(name, func(t *testing.T) {
  1187. tc := run(t)
  1188. i := 0
  1189. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  1190. w.Header().Set("Replay-Nonce", expectedNonce)
  1191. if i == 0 {
  1192. api.JSONStatus(w, tc.r1, tc.rc1)
  1193. i++
  1194. return
  1195. }
  1196. // validate jws request protected headers and body
  1197. body, err := ioutil.ReadAll(req.Body)
  1198. assert.FatalError(t, err)
  1199. jws, err := jose.ParseJWS(string(body))
  1200. assert.FatalError(t, err)
  1201. hdr := jws.Signatures[0].Protected
  1202. assert.Equals(t, hdr.Nonce, expectedNonce)
  1203. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  1204. assert.Fatal(t, ok)
  1205. assert.Equals(t, jwsURL, url)
  1206. assert.Equals(t, hdr.KeyID, ac.kid)
  1207. payload, err := jws.Verify(ac.Key.Public())
  1208. assert.FatalError(t, err)
  1209. assert.Equals(t, len(payload), 0)
  1210. if tc.certBytes != nil {
  1211. w.Write(tc.certBytes)
  1212. } else {
  1213. api.JSONStatus(w, tc.r2, tc.rc2)
  1214. }
  1215. })
  1216. if crt, chain, err := ac.GetCertificate(url); err != nil {
  1217. if assert.NotNil(t, tc.err) {
  1218. assert.HasPrefix(t, err.Error(), tc.err.Error())
  1219. }
  1220. } else {
  1221. if assert.Nil(t, tc.err) {
  1222. assert.Equals(t, crt, leaf)
  1223. assert.Equals(t, chain, []*x509.Certificate{leaf, leaf})
  1224. }
  1225. }
  1226. })
  1227. }
  1228. }