stanza.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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;
  79. const char *value;
  80. for (s->numpairs = 0; BIO_gets(s->fp, s->buff, sizeof(s->buff)); ) {
  81. s->curr++;
  82. if (!TEST_ptr(p = strchr(s->buff, '\n'))) {
  83. TEST_info("Line %d too long", s->curr);
  84. return 0;
  85. }
  86. *p = '\0';
  87. /* Blank line marks end of tests. */
  88. if (s->buff[0] == '\0')
  89. break;
  90. /* Lines starting with a pound sign are ignored. */
  91. if (s->buff[0] == '#')
  92. continue;
  93. /* Parse into key=value */
  94. if (!TEST_ptr(equals = strchr(s->buff, '='))) {
  95. TEST_info("Missing = at line %d\n", s->curr);
  96. return 0;
  97. }
  98. *equals++ = '\0';
  99. if (!TEST_ptr(key = strip_spaces(s->buff))) {
  100. TEST_info("Empty field at line %d\n", s->curr);
  101. return 0;
  102. }
  103. if ((value = strip_spaces(equals)) == NULL)
  104. value = "";
  105. if (strcmp(key, "Title") == 0) {
  106. TEST_info("Starting \"%s\" tests at line %d", value, s->curr);
  107. continue;
  108. }
  109. if (s->numpairs == 0)
  110. s->start = s->curr;
  111. if (strcmp(key, "PrivateKey") == 0) {
  112. if (!read_key(s))
  113. return 0;
  114. }
  115. if (strcmp(key, "PublicKey") == 0) {
  116. if (!read_key(s))
  117. return 0;
  118. }
  119. if (!TEST_int_lt(s->numpairs++, TESTMAXPAIRS)
  120. || !TEST_ptr(pp->key = OPENSSL_strdup(key))
  121. || !TEST_ptr(pp->value = OPENSSL_strdup(value)))
  122. return 0;
  123. pp++;
  124. }
  125. /* If we read anything, return ok. */
  126. return 1;
  127. }
  128. void test_clearstanza(STANZA *s)
  129. {
  130. PAIR *pp = s->pairs;
  131. int i = s->numpairs;
  132. for ( ; --i >= 0; pp++) {
  133. OPENSSL_free(pp->key);
  134. OPENSSL_free(pp->value);
  135. }
  136. s->numpairs = 0;
  137. }