소스 검색

Add test framework

master
Timo Röhling 7 년 전
부모
커밋
908f96bcdc
3개의 변경된 파일104개의 추가작업 그리고 1개의 파일을 삭제
  1. 4
    0
      CMakeLists.txt
  2. 1
    1
      makefile
  3. 99
    0
      tests.c

+ 4
- 0
CMakeLists.txt 파일 보기

@@ -81,9 +81,13 @@ if(NOT DEFINED INIT_FLAVOR)
81 81
 endif()
82 82
 
83 83
 add_executable(${PROJECT_NAME} postsrsd.c sha1.c srs2.c)
84
+enable_testing()
85
+add_executable(${PROJECT_NAME}_tests tests.c sha1.c srs2.c)
86
+add_test(AllTests ${PROJECT_NAME}_tests)
84 87
 
85 88
 if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
86 89
     target_link_libraries(${PROJECT_NAME} ${LIBSOCKET} ${LIBNSL})
90
+    target_link_libraries(${PROJECT_NAME}_tests ${LIBSOCKET} ${LIBNSL})
87 91
 endif()
88 92
 
89 93
 get_target_property(POSTSRSD ${PROJECT_NAME} LOCATION)

+ 1
- 1
makefile 파일 보기

@@ -1,4 +1,4 @@
1
-all install: build/Makefile
1
+all install test: build/Makefile
2 2
 	$(MAKE) -C build $@
3 3
 
4 4
 clean distclean:

+ 99
- 0
tests.c 파일 보기

@@ -0,0 +1,99 @@
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
+
18
+/* This program uses the libsrs2 library. The relevant source
19
+ * files have been added to this distribution. */
20
+#include "srs2.h"
21
+#include <string.h>
22
+
23
+static int run_srs(srs_t* srs, const char* address, const char* domain)
24
+{
25
+  int result, i;
26
+  char buf1[1024];
27
+  char buf2[1024];
28
+
29
+  printf("srs_forward(\"%s\", \"%s\") = ", address, domain);
30
+  result = srs_forward(srs, buf1, sizeof(buf1), address, domain);
31
+  printf ("%d\n", result);
32
+  if (result != SRS_SUCCESS) return 0;
33
+  printf("srs_reverse(\"%s\") = ", buf1);
34
+  result = srs_reverse(srs, buf2, sizeof(buf2), buf1);
35
+  printf("%d\n", result);
36
+  if (result != SRS_SUCCESS) return 0;
37
+  if (strcasecmp(address, buf2))
38
+  {
39
+    printf("SRS not idempotent: \"%s\" != \"%s\"\n", address, buf2);
40
+    return 0;
41
+  }
42
+
43
+  i = strchr(buf1, '@') - buf1;
44
+  while (i > 0)
45
+  {
46
+    --i;
47
+    if (buf1[i] == '=' || buf1[i] == '-' || buf1[i] == '+') continue;
48
+    buf1[i]++;
49
+    printf("srs_reverse(\"%s\") = ", buf1);
50
+    result = srs_reverse(srs, buf2, sizeof(buf2), buf1);
51
+    printf("%d\n", result);
52
+    if (result == SRS_SUCCESS) return 0;
53
+    buf1[i]--;
54
+  }
55
+  return 1;
56
+}
57
+
58
+static void generate_random_address(char* buf, size_t len1, size_t len2)
59
+{
60
+  static const char chars[] = "abcdefghijklmnopqrstuvwxyz0123456789-+=";
61
+  size_t i = 0, l1 = len1, l2 = len2;
62
+  while (l1 > 0)
63
+  {
64
+    buf[i++] = chars[random() % 39];
65
+    if (l1 < len1 && l1 > 1 && buf[i - 1] != '.' && (random() % 16 == 0)) buf[i - 1] = '.';
66
+    --l1;
67
+  }
68
+  buf[i++] = '@';
69
+  while (l2 > 0)
70
+  {
71
+    buf[i++] = 'a' + random() % 26;
72
+    if (l2 < len2 && l2 > 1 && buf[i - 1] != '.' && (random() % 16 == 0)) buf[i - 1] = '.';
73
+    --l2;
74
+  }
75
+  buf[i++] = 0;
76
+}
77
+
78
+#define ASSERT_SRS_OK(...) if (!run_srs(srs, __VA_ARGS__)) return EXIT_FAILURE
79
+
80
+int main (int argc, char** argv)
81
+{
82
+  srs_t* srs;
83
+  size_t l1, l2;
84
+  char addr[128];
85
+
86
+  srs = srs_new();
87
+  srs_add_secret (srs, "t0ps3cr3t");
88
+
89
+  for (l1 = 1; l1 <= 63; ++l1)
90
+  {
91
+    for (l2 = 1; l2 <= 63; ++l2)
92
+    {
93
+      generate_random_address(addr, l1, l2);
94
+      ASSERT_SRS_OK(addr, "example.com");
95
+    }
96
+  }
97
+  return EXIT_SUCCESS;
98
+}
99
+

Loading…
취소
저장