1 | /********************************************************************************************************* |
---|
2 | * Software License Agreement (BSD License) * |
---|
3 | * Author: Sebastien Decugis <sdecugis@nict.go.jp> * |
---|
4 | * * |
---|
5 | * Copyright (c) 2011, WIDE Project and NICT * |
---|
6 | * All rights reserved. * |
---|
7 | * * |
---|
8 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
---|
9 | * permitted provided that the following conditions are met: * |
---|
10 | * * |
---|
11 | * * Redistributions of source code must retain the above * |
---|
12 | * copyright notice, this list of conditions and the * |
---|
13 | * following disclaimer. * |
---|
14 | * * |
---|
15 | * * Redistributions in binary form must reproduce the above * |
---|
16 | * copyright notice, this list of conditions and the * |
---|
17 | * following disclaimer in the documentation and/or other * |
---|
18 | * materials provided with the distribution. * |
---|
19 | * * |
---|
20 | * * Neither the name of the WIDE Project or NICT nor the * |
---|
21 | * names of its contributors may be used to endorse or * |
---|
22 | * promote products derived from this software without * |
---|
23 | * specific prior written permission of WIDE Project and * |
---|
24 | * NICT. * |
---|
25 | * * |
---|
26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
---|
27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
---|
28 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
---|
29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
---|
30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
---|
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
---|
32 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
---|
33 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
---|
34 | *********************************************************************************************************/ |
---|
35 | |
---|
36 | #include "tests.h" |
---|
37 | |
---|
38 | #define TEST_STR (os0_t)"This is my test string (with extra unused data)" |
---|
39 | |
---|
40 | /* The following string contains UTF-8 encoded characters (Chinese characters) */ |
---|
41 | #define TEST_IDN_UTF8 "freeDiameter.中国" |
---|
42 | #define TEST_IDN_CONV "freeDiameter.xn--fiqs8s" |
---|
43 | |
---|
44 | /* Main test routine */ |
---|
45 | int main(int argc, char *argv[]) |
---|
46 | { |
---|
47 | /* First, initialize the daemon modules */ |
---|
48 | INIT_FD(); |
---|
49 | |
---|
50 | /* Check the hash function */ |
---|
51 | { |
---|
52 | uint8_t buf[30]; |
---|
53 | |
---|
54 | uint32_t hash = fd_os_hash(TEST_STR, CONSTSTRLEN(TEST_STR)); /* reference value */ |
---|
55 | |
---|
56 | /* Check that a hash of a substring / surstring is different */ |
---|
57 | CHECK( 1, hash != fd_os_hash(TEST_STR, CONSTSTRLEN(TEST_STR) - 1) ? 1 : 0 ); |
---|
58 | CHECK( 1, hash != fd_os_hash(TEST_STR, CONSTSTRLEN(TEST_STR) + 1) ? 1 : 0 ); |
---|
59 | |
---|
60 | /* Check alignment of the string is not important */ |
---|
61 | memcpy(buf + 4, TEST_STR, CONSTSTRLEN(TEST_STR)); |
---|
62 | CHECK( hash, fd_os_hash(buf + 4, CONSTSTRLEN(TEST_STR)) ); |
---|
63 | |
---|
64 | memcpy(buf + 3, TEST_STR, CONSTSTRLEN(TEST_STR)); |
---|
65 | CHECK( hash, fd_os_hash(buf + 3, CONSTSTRLEN(TEST_STR)) ); |
---|
66 | |
---|
67 | memcpy(buf + 2, TEST_STR, CONSTSTRLEN(TEST_STR)); |
---|
68 | CHECK( hash, fd_os_hash(buf + 2, CONSTSTRLEN(TEST_STR)) ); |
---|
69 | |
---|
70 | memcpy(buf + 1, TEST_STR, CONSTSTRLEN(TEST_STR)); |
---|
71 | CHECK( hash, fd_os_hash(buf + 1, CONSTSTRLEN(TEST_STR)) ); |
---|
72 | } |
---|
73 | |
---|
74 | /* Check the Diameter Identity functions */ |
---|
75 | { |
---|
76 | char * res; |
---|
77 | size_t len=0; |
---|
78 | |
---|
79 | /* A valid ASCII domain name */ |
---|
80 | res = TEST_IDN_CONV; |
---|
81 | CHECK( 0, fd_os_validate_DiameterIdentity(&res, &len, 1) ); |
---|
82 | CHECK( 0, strcasecmp(res, TEST_IDN_CONV) ); /* the function does not change a valid DN */ |
---|
83 | CHECK( 0, fd_os_validate_DiameterIdentity(&res, &len, 0) ); |
---|
84 | CHECK( 0, strcasecmp(res, TEST_IDN_CONV) ); |
---|
85 | CHECK( CONSTSTRLEN(TEST_IDN_CONV), len ); |
---|
86 | free(res); |
---|
87 | |
---|
88 | /* Now, an invalid string */ |
---|
89 | res = TEST_IDN_UTF8; |
---|
90 | len = 0; |
---|
91 | |
---|
92 | #ifdef DIAMID_IDNA_IGNORE |
---|
93 | |
---|
94 | /* The UTF-8 chars are considered valid */ |
---|
95 | CHECK( 1, fd_os_is_valid_DiameterIdentity((os0_t)TEST_IDN_UTF8, CONSTSTRLEN(TEST_IDN_UTF8) ) ); |
---|
96 | |
---|
97 | /* The string should be passed unmodified */ |
---|
98 | CHECK( 0, fd_os_validate_DiameterIdentity(&res, &len, 1) ); |
---|
99 | CHECK( 0, strcasecmp(res, TEST_IDN_UTF8) ); |
---|
100 | CHECK( 0, fd_os_cmp(res, len, TEST_IDN_UTF8, CONSTSTRLEN(TEST_IDN_UTF8)) ); |
---|
101 | CHECK( 0, fd_os_almostcasecmp(res, len, TEST_IDN_UTF8, CONSTSTRLEN(TEST_IDN_UTF8)) ); |
---|
102 | CHECK( 0, fd_os_validate_DiameterIdentity(&res, &len, 0) ); |
---|
103 | CHECK( 0, strcasecmp(res, TEST_IDN_UTF8) ); |
---|
104 | CHECK( CONSTSTRLEN(TEST_IDN_UTF8), len ); |
---|
105 | free(res); |
---|
106 | |
---|
107 | #else /* DIAMID_IDNA_IGNORE */ |
---|
108 | |
---|
109 | /* The UTF-8 chars are recognized as invalid DiameterIdentity */ |
---|
110 | CHECK( 0, fd_os_is_valid_DiameterIdentity((os0_t)TEST_IDN_UTF8, CONSTSTRLEN(TEST_IDN_UTF8) )); |
---|
111 | |
---|
112 | # ifdef DIAMID_IDNA_REJECT |
---|
113 | |
---|
114 | /* The string must be rejected */ |
---|
115 | CHECK( EINVAL, fd_os_validate_DiameterIdentity(&res, &len, 1) ); |
---|
116 | |
---|
117 | # else /* DIAMID_IDNA_REJECT */ |
---|
118 | |
---|
119 | /* The string should be transformed into TEST_IDN_CONV */ |
---|
120 | CHECK( 0, fd_os_validate_DiameterIdentity(&res, &len, 1) ); |
---|
121 | CHECK( 0, strcasecmp(res, TEST_IDN_CONV) ); |
---|
122 | CHECK( CONSTSTRLEN(TEST_IDN_CONV), len ); |
---|
123 | free(res); |
---|
124 | |
---|
125 | # endif /* DIAMID_IDNA_REJECT */ |
---|
126 | #endif /* DIAMID_IDNA_IGNORE */ |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | /* That's all for the tests yet */ |
---|
131 | PASSTEST(); |
---|
132 | } |
---|
133 | |
---|