File: tests.c

package info (click to toggle)
postsrsd 1.5-2%2Bdeb10u2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 416 kB
  • sloc: ansic: 1,584; sh: 201; makefile: 22
file content (100 lines) | stat: -rw-r--r-- 2,881 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* PostSRSd - Sender Rewriting Scheme daemon for Postfix
 * Copyright (c) 2012 Timo Röhling <timo.roehling@gmx.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* This program uses the libsrs2 library. The relevant source
 * files have been added to this distribution. */
#include "srs2.h"
#include <string.h>

static int run_srs(srs_t* srs, const char* address, const char* domain)
{
  int result, i;
  char buf1[1024];
  char buf2[1024];

  printf("srs_forward(\"%s\", \"%s\") = ", address, domain);
  result = srs_forward(srs, buf1, sizeof(buf1), address, domain);
  printf ("%d\n", result);
  if (result != SRS_SUCCESS) return 0;
  printf("srs_reverse(\"%s\") = ", buf1);
  result = srs_reverse(srs, buf2, sizeof(buf2), buf1);
  printf("%d\n", result);
  if (result != SRS_SUCCESS) return 0;
  if (strcasecmp(address, buf2))
  {
    printf("SRS not idempotent: \"%s\" != \"%s\"\n", address, buf2);
    return 0;
  }

  i = strchr(buf1, '@') - buf1;
  while (i > 0)
  {
    --i;
    if (buf1[i] == '=' || buf1[i] == '-' || buf1[i] == '+') continue;
    buf1[i]++;
    printf("srs_reverse(\"%s\") = ", buf1);
    result = srs_reverse(srs, buf2, sizeof(buf2), buf1);
    printf("%d\n", result);
    if (result == SRS_SUCCESS) return 0;
    buf1[i]--;
  }
  return 1;
}

static void generate_random_address(char* buf, size_t len1, size_t len2)
{
  static const char chars[] = "abcdefghijklmnopqrstuvwxyz0123456789-+=";
  size_t i = 0, l1 = len1, l2 = len2;
  while (l1 > 0)
  {
    buf[i++] = chars[random() % 39];
    if (l1 < len1 && l1 > 1 && buf[i - 1] != '.' && (random() % 16 == 0)) buf[i - 1] = '.';
    --l1;
  }
  buf[i++] = '@';
  while (l2 > 0)
  {
    buf[i++] = 'a' + random() % 26;
    if (l2 < len2 && l2 > 1 && buf[i - 1] != '.' && (random() % 16 == 0)) buf[i - 1] = '.';
    --l2;
  }
  buf[i++] = 0;
}

#define ASSERT_SRS_OK(...) if (!run_srs(srs, __VA_ARGS__)) return EXIT_FAILURE

int main (int argc, char** argv)
{
  srs_t* srs;
  size_t l1, l2;
  char addr[128];

  srs = srs_new();
  srs_add_secret (srs, "t0ps3cr3t");
  srs_set_hashlength(srs, 16);

  for (l1 = 1; l1 <= 63; ++l1)
  {
    for (l2 = 1; l2 <= 63; ++l2)
    {
      generate_random_address(addr, l1, l2);
      ASSERT_SRS_OK(addr, "example.com");
    }
  }
  return EXIT_SUCCESS;
}