stanza.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <assert.h>
  10. #include <errno.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include "internal/nelem.h"
  15. #include "../testutil.h"
  16. #include "tu_local.h"
  17. int test_start_file(STANZA *s, const char *testfile)
  18. {
  19. TEST_info("Reading %s", testfile);
  20. set_test_title(testfile);
  21. memset(s, 0, sizeof(*s));
  22. if (!TEST_ptr(s->fp = BIO_new_file(testfile, "r")))
  23. return 0;
  24. s->test_file = testfile;
  25. return 1;
  26. }
  27. int test_end_file(STANZA *s)
  28. {
  29. TEST_info("Completed %d tests with %d errors and %d skipped",
  30. s->numtests, s->errors, s->numskip);
  31. BIO_free(s->fp);
  32. return 1;
  33. }
  34. /*
  35. * Read a PEM block. Return 1 if okay, 0 on error.
  36. */
  37. static int read_key(STANZA *s)
  38. {
  39. char tmpbuf[128];
  40. if (s->key == NULL) {
  41. if (!TEST_ptr(s->key = BIO_new(BIO_s_mem())))
  42. return 0;
  43. } else if (!TEST_int_gt(BIO_reset(s->key), 0)) {
  44. return 0;
  45. }
  46. /* Read to PEM end line and place content in memory BIO */
  47. while (BIO_gets(s->fp, tmpbuf, sizeof(tmpbuf))) {
  48. s->curr++;
  49. if (!TEST_int_gt(BIO_puts(s->key, tmpbuf), 0))
  50. return 0;
  51. if (strncmp(tmpbuf, "-----END", 8) == 0)
  52. return 1;
  53. }
  54. TEST_error("Can't find key end");
  55. return 0;
  56. }
  57. /*
  58. * Delete leading and trailing spaces from a string
  59. */
  60. static char *strip_spaces(char *p)
  61. {
  62. char *q;
  63. /* Skip over leading spaces */
  64. while (*p && isspace((unsigned char)*p))
  65. p++;
  66. if (!*p)
  67. return NULL;
  68. for (q = p + strlen(p) - 1; q != p && isspace((unsigned char)*q); )
  69. *q-- = '\0';
  70. return *p ? p : NULL;
  71. }
  72. /*
  73. * Read next test stanza; return 1 if found, 0 on EOF or error.
  74. */
  75. int test_readstanza(STANZA *s)
  76. {
  77. PAIR *pp = s->pairs;
  78. char *p, *equals, *key, *value;
  79. for (s->numpairs = 0; BIO_gets(s->fp, s->buff, sizeof(s->buff)); ) {
  80. s->curr++;
  81. if (!TEST_ptr(p = strchr(s->buff, '\n'))) {
  82. TEST_info("Line %d too long", s->curr);
  83. return 0;
  84. }
  85. *p = '\0';
  86. /* Blank line marks end of tests. */
  87. if (s->buff[0] == '\0')
  88. break;
  89. /* Lines starting with a pound sign are ignored. */
  90. if (s->buff[0] == '#')
  91. continue;
  92. /* Parse into key=value */
  93. if (!TEST_ptr(equals = strchr(s->buff, '='))) {
  94. TEST_info("Missing = at line %d\n", s->curr);
  95. return 0;
  96. }
  97. *equals++ = '\0';
  98. if (!TEST_ptr(key = strip_spaces(s->buff))) {
  99. TEST_info("Empty field at line %d\n", s->curr);
  100. return 0;
  101. }
  102. if ((value = strip_spaces(equals)) == NULL)
  103. value = "";
  104. if (strcmp(key, "Title") == 0) {
  105. TEST_info("Starting \"%s\" tests at line %d", value, s->curr);
  106. continue;
  107. }
  108. if (s->numpairs == 0)
  109. s->start = s->curr;
  110. if (strcmp(key, "PrivateKey") == 0) {
  111. if (!read_key(s))
  112. return 0;
  113. }
  114. if (strcmp(key, "PublicKey") == 0) {
  115. if (!read_key(s))
  116. return 0;
  117. }
  118. if (!TEST_int_lt(s->numpairs++, TESTMAXPAIRS)
  119. || !TEST_ptr(pp->key = OPENSSL_strdup(key))
  120. || !TEST_ptr(pp->value = OPENSSL_strdup(value)))
  121. return 0;
  122. pp++;
  123. }
  124. /* If we read anything, return ok. */
  125. return 1;
  126. }
  127. void test_clearstanza(STANZA *s)
  128. {
  129. PAIR *pp = s->pairs;
  130. int i = s->numpairs;
  131. for ( ; --i >= 0; pp++) {
  132. OPENSSL_free(pp->key);
  133. OPENSSL_free(pp->value);
  134. }
  135. s->numpairs = 0;
  136. }