選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

tests.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* PostSRSd - Sender Rewriting Scheme daemon for Postfix
  2. * Copyright (c) 2012 Timo Röhling <timo.roehling@gmx.de>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /* This program uses the libsrs2 library. The relevant source
  18. * files have been added to this distribution. */
  19. #include "srs2.h"
  20. #include <string.h>
  21. static int run_srs(srs_t* srs, const char* address, const char* domain)
  22. {
  23. int result, i;
  24. char buf1[1024];
  25. char buf2[1024];
  26. printf("srs_forward(\"%s\", \"%s\") = ", address, domain);
  27. result = srs_forward(srs, buf1, sizeof(buf1), address, domain);
  28. printf ("%d\n", result);
  29. if (result != SRS_SUCCESS) return 0;
  30. printf("srs_reverse(\"%s\") = ", buf1);
  31. result = srs_reverse(srs, buf2, sizeof(buf2), buf1);
  32. printf("%d\n", result);
  33. if (result != SRS_SUCCESS) return 0;
  34. if (strcasecmp(address, buf2))
  35. {
  36. printf("SRS not idempotent: \"%s\" != \"%s\"\n", address, buf2);
  37. return 0;
  38. }
  39. i = strchr(buf1, '@') - buf1;
  40. while (i > 0)
  41. {
  42. --i;
  43. if (buf1[i] == '=' || buf1[i] == '-' || buf1[i] == '+') continue;
  44. buf1[i]++;
  45. printf("srs_reverse(\"%s\") = ", buf1);
  46. result = srs_reverse(srs, buf2, sizeof(buf2), buf1);
  47. printf("%d\n", result);
  48. if (result == SRS_SUCCESS) return 0;
  49. buf1[i]--;
  50. }
  51. return 1;
  52. }
  53. static void generate_random_address(char* buf, size_t len1, size_t len2)
  54. {
  55. static const char chars[] = "abcdefghijklmnopqrstuvwxyz0123456789-+=";
  56. size_t i = 0, l1 = len1, l2 = len2;
  57. while (l1 > 0)
  58. {
  59. buf[i++] = chars[random() % 39];
  60. if (l1 < len1 && l1 > 1 && buf[i - 1] != '.' && (random() % 16 == 0)) buf[i - 1] = '.';
  61. --l1;
  62. }
  63. buf[i++] = '@';
  64. while (l2 > 0)
  65. {
  66. buf[i++] = 'a' + random() % 26;
  67. if (l2 < len2 && l2 > 1 && buf[i - 1] != '.' && (random() % 16 == 0)) buf[i - 1] = '.';
  68. --l2;
  69. }
  70. buf[i++] = 0;
  71. }
  72. #define ASSERT_SRS_OK(...) if (!run_srs(srs, __VA_ARGS__)) return EXIT_FAILURE
  73. int main (int argc, char** argv)
  74. {
  75. srs_t* srs;
  76. size_t l1, l2;
  77. char addr[128];
  78. srs = srs_new();
  79. srs_add_secret (srs, "t0ps3cr3t");
  80. for (l1 = 1; l1 <= 63; ++l1)
  81. {
  82. for (l2 = 1; l2 <= 63; ++l2)
  83. {
  84. generate_random_address(addr, l1, l2);
  85. ASSERT_SRS_OK(addr, "example.com");
  86. }
  87. }
  88. return EXIT_SUCCESS;
  89. }