Mercurial > ecos-v3_0-branch
annotate packages/net/sntp/current/tests/sntp1.c @ 2729:74dbf4c3f2e1 after-copyright-change-20090129
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
| author | jlarmour |
|---|---|
| date | Thu, 29 Jan 2009 17:47:46 +0000 |
| parents | 16a975af0355 |
| children |
| rev | line source |
|---|---|
| 748 | 1 //================================================================= |
| 2 // | |
| 3 // sntp1.c | |
| 4 // | |
| 5 // Simple Network Time Protocol test 1 | |
| 6 // | |
| 7 //========================================================================== | |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
8 // ####ECOSGPLCOPYRIGHTBEGIN#### |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
9 // ------------------------------------------- |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
10 // This file is part of eCos, the Embedded Configurable Operating System. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
11 // Copyright (C) 2003 Free Software Foundation, Inc. |
| 748 | 12 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
13 // eCos is free software; you can redistribute it and/or modify it under |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
14 // the terms of the GNU General Public License as published by the Free |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
15 // Software Foundation; either version 2 or (at your option) any later |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
16 // version. |
| 748 | 17 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
18 // eCos is distributed in the hope that it will be useful, but WITHOUT |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
19 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
21 // for more details. |
| 748 | 22 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
23 // You should have received a copy of the GNU General Public License |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
24 // along with eCos; if not, write to the Free Software Foundation, Inc., |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
25 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 748 | 26 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
27 // As a special exception, if other files instantiate templates or use |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
28 // macros or inline functions from this file, or you compile this file |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
29 // and link it with other works to produce a work based on this file, |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
30 // this file does not by itself cause the resulting work to be covered by |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
31 // the GNU General Public License. However the source code for this file |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
32 // must still be made available in accordance with section (3) of the GNU |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
33 // General Public License v2. |
| 748 | 34 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
35 // This exception does not invalidate any other reasons why a work based |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
36 // on this file might be covered by the GNU General Public License. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
37 // ------------------------------------------- |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
1323
diff
changeset
|
38 // ####ECOSGPLCOPYRIGHTEND#### |
| 748 | 39 //========================================================================== |
| 40 //#####DESCRIPTIONBEGIN#### | |
| 41 // | |
| 42 // Author(s): Andrew Lunn | |
| 43 // Contributors: | |
| 44 // Date: 2003-02-11 | |
| 45 // Description: Tests the sntp client | |
| 46 //####DESCRIPTIONEND#### | |
| 47 | |
|
759
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
48 #include <pkgconf/isoinfra.h> |
|
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
49 #include <cyg/infra/testcase.h> |
|
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
50 |
|
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
51 #if defined(CYGINT_ISO_STDIO_FORMATTED_IO) && defined(CYGINT_ISO_STRING_STRFUNCS) |
| 748 | 52 #include <network.h> |
| 53 #include <time.h> | |
| 54 #include <cyg/sntp/sntp.h> | |
| 55 #include <stdio.h> | |
| 56 | |
| 57 #define SECONDSPERMINUTE (cyg_uint32)60 | |
| 58 #define SECONDSPERHOUR (cyg_uint32)(SECONDSPERMINUTE * 60) | |
| 59 #define SECONDSPERDAY (cyg_uint32)(SECONDSPERHOUR * 24) | |
| 60 #define SECONDSPERYEAR (cyg_uint32)(SECONDSPERDAY * 365) | |
| 61 | |
| 62 void | |
| 63 net_test(cyg_addrword_t param) | |
| 64 { | |
| 65 int seconds; | |
| 66 time_t now, build_time; | |
| 67 struct tm tm={ 0,0,0,0,0,0,0,0,0 }; | |
|
1323
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
68 int i, loop, waittime; |
| 748 | 69 char month[4]; |
| 70 char months[12][4] = { "Jan", "Feb", "Mar", | |
| 71 "Apr", "May", "Jun", | |
| 72 "Jul", "Aug", "Sep", | |
| 73 "Oct", "Nov", "Dec" }; | |
|
759
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
74 char time_info[32]; |
| 748 | 75 |
| 76 CYG_TEST_INIT(); | |
| 77 | |
| 78 CYG_TEST_INFO("sntp1 test build " __DATE__); | |
| 79 | |
| 80 init_all_network_interfaces(); | |
| 81 | |
| 82 cyg_sntp_start(); | |
| 83 | |
|
1323
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
84 /* The SNTP client will try to obtain NTP time updates by |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
85 * listening for multicasts. It can also be configured |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
86 * to send unicast requests to specific NTP servers. By |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
87 * default, unicast NTP servers are obtained from DHCP. |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
88 * |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
89 * If unicast mode is enabled, the run the test loop twice. |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
90 * The first time, unicast requests will be sent. The |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
91 * second time, the unicast list will be unconfigured and |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
92 * the client will listen only for multicasts. |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
93 * |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
94 * Note that this test is somewhat bogus since multicast |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
95 * NTP packets will actually allow both test loops to |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
96 * pass. But it is the best we can do for the automated |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
97 * test, so consider this more of a usage example. |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
98 */ |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
99 #ifdef CYGPKG_NET_SNTP_UNICAST |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
100 loop = 2; |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
101 #else |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
102 loop = 1; |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
103 #endif |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
104 while (loop-- > 0) |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
105 { |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
106 if (loop == 1) { |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
107 CYG_TEST_INFO("Testing SNTP unicast mode."); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
108 waittime=14; |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
109 } else { |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
110 CYG_TEST_INFO("Testing SNTP multicast mode."); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
111 waittime=20; |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
112 } |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
113 for (seconds = waittime; seconds > 0; seconds--) { |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
114 now = time(NULL); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
115 ctime_r(&now, time_info); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
116 time_info[strlen(time_info)-1] = '\0'; // Strip \n |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
117 CYG_TEST_INFO(time_info); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
118 cyg_thread_delay(100); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
119 } |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
120 |
| 748 | 121 now = time(NULL); |
| 122 | |
|
1323
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
123 if ( now < (5 * 60)) { |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
124 CYG_TEST_FAIL_FINISH("Nothing recieved from the SNTP server"); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
125 } else { |
| 748 | 126 |
|
1323
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
127 i=sscanf(__DATE__, "%s %d %d",month,&tm.tm_mday,&tm.tm_year); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
128 CYG_ASSERT(3==i,"sscanf did not return enough results"); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
129 for (i=0; i < 12; i++) { |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
130 if (!strcmp(month,months[i])) |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
131 break; |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
132 } |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
133 tm.tm_mon = i; |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
134 tm.tm_year -= 1900; |
| 748 | 135 |
|
1323
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
136 build_time = mktime(&tm); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
137 CYG_ASSERT(-1 != build_time,"mktime returned -1"); |
| 748 | 138 |
|
1323
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
139 if (build_time > time(NULL)) { |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
140 CYG_TEST_FAIL_FINISH("Build time is ahead of SNTP time"); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
141 } else { |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
142 if ((build_time + 60 * 60 * 24 * 90) < time(NULL)) { |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
143 CYG_TEST_FAIL_FINISH("Build time is more than 90 days old"); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
144 } |
| 748 | 145 } |
| 146 } | |
|
1323
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
147 |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
148 #ifdef CYGPKG_NET_SNTP_UNICAST |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
149 /* For the second pass of the test, we set the time |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
150 * back to epoch and unconfigure the list of SNTP |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
151 * unicast servers. This will test non-unicast mode. |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
152 */ |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
153 cyg_sntp_set_servers(NULL, 0); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
154 cyg_libc_time_settime(0); |
|
16a975af0355
* src/sntp.c: Added DHCP support for SNTP unicast mode.
asl
parents:
1033
diff
changeset
|
155 #endif |
| 748 | 156 } |
|
759
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
157 CYG_TEST_PASS_FINISH("sntp1 test is complete"); |
| 748 | 158 } |
| 159 | |
| 160 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL*2) | |
| 161 static char thread_stack[STACK_SIZE]; | |
| 162 static cyg_thread thread_data; | |
| 163 static cyg_handle_t thread_handle; | |
| 164 | |
| 165 void | |
| 166 cyg_user_start(void) | |
| 167 { | |
| 168 // Create a main thread, so we can run the scheduler and have time 'pass' | |
| 169 cyg_thread_create(10, // Priority - just a number | |
| 170 net_test, // entry | |
| 171 0, // entry parameter | |
| 172 "Network test", // Name | |
| 173 thread_stack, // Stack | |
| 174 STACK_SIZE, // Size | |
| 175 &thread_handle, // Handle | |
| 176 &thread_data // Thread data structure | |
| 177 ); | |
| 178 cyg_thread_resume(thread_handle); // Start it | |
| 179 } | |
|
759
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
180 |
|
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
181 #else //defined(CYGINT_ISO_STDIO_FORMATTED_IO) && defined(CYGINT_ISO_STRING_STRFUNCS) |
|
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
182 |
|
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
183 void cyg_user_start(void) |
|
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
184 { |
|
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
185 CYG_TEST_NA("Not all the required packages are available"); |
|
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
186 } |
|
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
187 |
|
018143c925ee
* tests/sntp1.c (net_test): Strip the trailing "\n" making it more
asl
parents:
748
diff
changeset
|
188 #endif |
