trema 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (292) hide show
  1. data/.gitignore +1 -1
  2. data/Doxyfile +1 -1
  3. data/Gemfile +18 -6
  4. data/README.md +112 -5
  5. data/Rakefile +3 -1
  6. data/Rantfile +88 -1
  7. data/bin/trema +248 -0
  8. data/bin/trema-config +59 -0
  9. data/cruise.rb +1 -1
  10. data/features/example.dumper.feature +45 -65
  11. data/features/example.hello_trema.feature +21 -0
  12. data/features/example.learning_switch.feature +26 -28
  13. data/features/example.list_switches.feature +19 -33
  14. data/features/example.message.echo_reply.feature +10 -10
  15. data/features/example.message.echo_request.feature +2 -2
  16. data/features/example.message.features_request.feature +6 -5
  17. data/features/example.message.hello.feature +47 -13
  18. data/features/example.message.set_config.feature +4 -4
  19. data/features/example.message.vendor-action.feature +23 -0
  20. data/features/example.multi_learning_switch.feature +14 -42
  21. data/features/example.packet_in.feature +31 -0
  22. data/features/example.packetin_filter_config.feature +11 -7
  23. data/features/example.patch_panel.feature +29 -0
  24. data/features/example.repeater_hub.feature +43 -40
  25. data/features/example.switch_info.feature +51 -0
  26. data/features/example.switch_monitor.feature +24 -42
  27. data/features/packetin_filter.feature +28 -22
  28. data/features/step_definitions/misc_steps.rb +19 -8
  29. data/features/step_definitions/send_packets_steps.rb +1 -10
  30. data/features/step_definitions/stats_steps.rb +4 -4
  31. data/features/support/env.rb +2 -1
  32. data/features/support/hooks.rb +35 -2
  33. data/features/switch_manager.feature +18 -12
  34. data/features/trema-config.feature +26 -39
  35. data/features/trema.dump_flows.feature +26 -12
  36. data/features/trema.help.feature +26 -0
  37. data/features/trema.kill.feature +39 -41
  38. data/features/trema.killall.feature +23 -23
  39. data/features/trema.reset_stats.feature +50 -10
  40. data/features/trema.run.feature +28 -39
  41. data/features/trema.send_packets.feature +29 -40
  42. data/features/trema.show_stats.feature +30 -33
  43. data/features/trema.up.feature +33 -0
  44. data/features/trema.version.feature +9 -0
  45. data/ruby/extconf.rb +7 -0
  46. data/ruby/trema/aggregate-stats-reply.rb +3 -3
  47. data/ruby/trema/app.rb +5 -1
  48. data/ruby/trema/barrier-request.c +55 -30
  49. data/ruby/trema/cli.rb +8 -8
  50. data/ruby/trema/command.rb +1 -3
  51. data/ruby/trema/command/dump_flows.rb +5 -24
  52. data/ruby/trema/command/kill.rb +31 -36
  53. data/ruby/trema/command/killall.rb +1 -19
  54. data/{features/step_definitions/kill_steps.rb → ruby/trema/command/netns.rb} +13 -5
  55. data/ruby/trema/command/reset_stats.rb +3 -23
  56. data/ruby/trema/command/ruby.rb +2 -19
  57. data/ruby/trema/command/run.rb +6 -27
  58. data/ruby/trema/command/send_packets.rb +6 -90
  59. data/ruby/trema/command/show_stats.rb +21 -40
  60. data/ruby/trema/command/up.rb +5 -26
  61. data/ruby/trema/command/version.rb +1 -5
  62. data/ruby/trema/controller.c +14 -16
  63. data/ruby/trema/custom-switch.rb +56 -0
  64. data/ruby/trema/desc-stats-reply.rb +5 -5
  65. data/ruby/trema/dsl/configuration.rb +4 -0
  66. data/{features/step_definitions/up_steps.rb → ruby/trema/dsl/custom-switch.rb} +18 -4
  67. data/ruby/trema/dsl/netns.rb +78 -0
  68. data/ruby/trema/dsl/parser.rb +2 -0
  69. data/ruby/trema/dsl/runner.rb +8 -0
  70. data/ruby/trema/dsl/syntax.rb +18 -0
  71. data/ruby/trema/echo-reply.h +2 -2
  72. data/ruby/trema/echo-request.c +3 -5
  73. data/ruby/trema/enqueue.rb +2 -2
  74. data/ruby/trema/error.c +3 -3
  75. data/ruby/trema/executables.rb +26 -2
  76. data/ruby/trema/features-request.c +3 -5
  77. data/ruby/trema/flow-removed.c +6 -6
  78. data/ruby/trema/flow-stats-reply.rb +6 -8
  79. data/ruby/trema/flow.rb +12 -1
  80. data/ruby/trema/get-config-request.c +55 -28
  81. data/ruby/trema/hello.c +3 -5
  82. data/ruby/trema/host.rb +8 -0
  83. data/ruby/trema/list-switches-reply.c +1 -1
  84. data/ruby/trema/mac.rb +1 -1
  85. data/ruby/trema/match.c +15 -14
  86. data/ruby/trema/monkey-patch/module/deprecation.rb +0 -2
  87. data/ruby/trema/netns.rb +127 -0
  88. data/ruby/trema/ordered-hash.rb +5 -4
  89. data/ruby/trema/packet-in.c +136 -113
  90. data/ruby/trema/packet-queue.rb +9 -9
  91. data/ruby/trema/packetin-filter.rb +1 -1
  92. data/ruby/trema/phost.rb +16 -7
  93. data/ruby/trema/port-mod.c +6 -5
  94. data/ruby/trema/port-stats-reply.rb +2 -2
  95. data/ruby/trema/process.rb +29 -0
  96. data/ruby/trema/queue-stats-reply.rb +2 -4
  97. data/ruby/trema/send-out-port.rb +5 -3
  98. data/ruby/trema/set-eth-addr.rb +4 -0
  99. data/ruby/trema/set-ip-addr.rb +4 -2
  100. data/ruby/trema/set-ip-dst-addr.rb +2 -1
  101. data/ruby/trema/set-ip-src-addr.rb +2 -1
  102. data/ruby/trema/set-ip-tos.rb +3 -2
  103. data/ruby/trema/set-transport-port.rb +3 -2
  104. data/ruby/trema/set-vlan-priority.rb +3 -2
  105. data/ruby/trema/set-vlan-vid.rb +5 -4
  106. data/ruby/trema/shell/reset_stats.rb +2 -1
  107. data/ruby/trema/shell/run.rb +1 -1
  108. data/ruby/trema/shell/send_packets.rb +1 -1
  109. data/ruby/trema/shell/show_stats.rb +1 -1
  110. data/ruby/trema/stats-helper.rb +3 -3
  111. data/ruby/trema/stats-reply.c +26 -17
  112. data/ruby/trema/stats-request.c +39 -41
  113. data/ruby/trema/switch-daemon.rb +36 -31
  114. data/ruby/trema/switch.c +1 -1
  115. data/ruby/trema/table-stats-reply.rb +1 -1
  116. data/ruby/trema/timers.rb +13 -13
  117. data/ruby/trema/trema.c +3 -3
  118. data/ruby/trema/tremashark.rb +9 -2
  119. data/ruby/trema/util.rb +39 -15
  120. data/ruby/trema/vendor-action.rb +8 -3
  121. data/ruby/trema/vendor-stats-reply.rb +4 -6
  122. data/ruby/trema/vendor.c +1 -1
  123. data/ruby/trema/version.rb +1 -1
  124. data/spec/trema/barrier-request_spec.rb +47 -37
  125. data/spec/trema/controller_spec.rb +1 -0
  126. data/spec/trema/dsl/runner_spec.rb +8 -3
  127. data/spec/trema/dsl/vhost_spec.rb +8 -8
  128. data/spec/trema/echo-request_spec.rb +1 -0
  129. data/spec/trema/features-request_spec.rb +1 -0
  130. data/spec/trema/flow-removed_spec.rb +9 -9
  131. data/spec/trema/get-config-request_spec.rb +51 -39
  132. data/spec/trema/match_spec.rb +1 -1
  133. data/spec/trema/openflow-error_spec.rb +11 -11
  134. data/spec/trema/packet-out_spec.rb +5 -5
  135. data/spec/trema/queue-get-config-reply_spec.rb +1 -1
  136. data/spec/trema/queue-get-config-request_spec.rb +1 -1
  137. data/spec/trema/set-eth-dst-addr_spec.rb +1 -1
  138. data/spec/trema/set-eth-src-addr_spec.rb +1 -1
  139. data/spec/trema/set-ip-dst-addr_spec.rb +1 -0
  140. data/spec/trema/set-ip-src-addr_spec.rb +1 -0
  141. data/spec/trema/set-ip-tos_spec.rb +1 -0
  142. data/spec/trema/set-transport-dst-port_spec.rb +1 -0
  143. data/spec/trema/set-transport-src-port_spec.rb +1 -0
  144. data/spec/trema/set-vlan-priority_spec.rb +1 -0
  145. data/spec/trema/set-vlan-vid_spec.rb +2 -1
  146. data/spec/trema/stats-reply_spec.rb +38 -36
  147. data/spec/trema/stats-request_spec.rb +6 -6
  148. data/spec/trema/strip-vlan-header_spec.rb +1 -0
  149. data/spec/trema/util_spec.rb +3 -2
  150. data/spec/trema/vendor-action_spec.rb +9 -8
  151. data/src/examples/dumper/dumper.c +8 -6
  152. data/src/examples/dumper/dumper.rb +1 -1
  153. data/{features/step_definitions/killall_steps.rb → src/examples/hello_trema/hello-trema.rb} +6 -5
  154. data/src/examples/hello_trema/hello_trema.c +4 -4
  155. data/src/examples/learning_switch/learning_switch.c +1 -1
  156. data/src/examples/list_switches/list_switches.c +2 -2
  157. data/src/examples/match_compare/match-compare.rb +2 -2
  158. data/src/examples/multi_learning_switch/multi-learning-switch.rb +1 -1
  159. data/src/examples/multi_learning_switch/multi_learning_switch.c +3 -3
  160. data/src/examples/openflow_message/README +1 -1
  161. data/src/examples/openflow_message/echo-request.rb +1 -1
  162. data/src/examples/openflow_message/features_request.c +21 -21
  163. data/src/examples/openflow_message/hello.c +1 -3
  164. data/src/examples/openflow_message/hello.rb +9 -24
  165. data/src/examples/{hello_trema/hello_trema.rb → openflow_message/vendor-action.rb} +17 -7
  166. data/src/examples/openflow_message/vendor_action.c +105 -0
  167. data/src/examples/openflow_switch/hello_switch.c +81 -0
  168. data/src/examples/packet_in/packet-in.rb +9 -13
  169. data/src/examples/packetin_filter_config/packetin_filter_config.c +2 -2
  170. data/src/examples/patch_panel/network.conf +14 -0
  171. data/src/examples/patch_panel/patch-panel.conf +1 -0
  172. data/{features/step_definitions/show_stats_steps.rb → src/examples/patch_panel/patch-panel.rb} +29 -13
  173. data/src/examples/switch_info/{switch_info.rb → switch-info.rb} +2 -2
  174. data/src/examples/switch_info/switch_info.c +4 -4
  175. data/src/examples/switch_monitor/switch_monitor.c +3 -3
  176. data/src/examples/traffic_monitor/counter.c +9 -9
  177. data/src/examples/traffic_monitor/fdb.c +9 -9
  178. data/src/lib/arp.h +1 -1
  179. data/src/lib/buffer.c +4 -3
  180. data/src/lib/byteorder.c +31 -5
  181. data/src/lib/byteorder.h +2 -2
  182. data/src/lib/chibach.c +8 -6
  183. data/src/lib/daemon.c +29 -3
  184. data/src/lib/doubly_linked_list.c +6 -1
  185. data/src/lib/ether.c +1 -1
  186. data/src/lib/ether.h +1 -1
  187. data/src/lib/event_handler.c +3 -3
  188. data/src/lib/hash_table.c +1 -1
  189. data/src/lib/linked_list.c +50 -2
  190. data/src/lib/linked_list.h +2 -2
  191. data/src/lib/log.c +122 -22
  192. data/src/lib/log.h +13 -9
  193. data/src/lib/management_interface.c +361 -0
  194. data/src/lib/management_interface.h +42 -0
  195. data/src/lib/management_service_interface.c +104 -0
  196. data/src/lib/management_service_interface.h +136 -0
  197. data/src/lib/match_table.c +5 -5
  198. data/src/lib/message_queue.c +5 -3
  199. data/src/lib/message_queue.h +1 -1
  200. data/src/lib/messenger.c +73 -39
  201. data/src/lib/messenger.h +3 -2
  202. data/src/lib/openflow_application_interface.c +17 -17
  203. data/src/lib/openflow_message.c +175 -23
  204. data/src/lib/openflow_message.h +2 -1
  205. data/src/lib/openflow_switch_interface.c +12 -12
  206. data/src/lib/packet_parser.c +11 -32
  207. data/src/lib/packetin_filter_interface.c +5 -5
  208. data/src/lib/persistent_storage.c +7 -7
  209. data/src/lib/secure_channel.c +6 -6
  210. data/src/lib/secure_channel.h +1 -1
  211. data/src/lib/stat.c +54 -17
  212. data/src/lib/stat.h +9 -0
  213. data/src/lib/timer.c +11 -10
  214. data/src/lib/trema.c +38 -9
  215. data/src/lib/trema.h +1 -0
  216. data/src/lib/trema_private.h +6 -0
  217. data/src/lib/trema_wrapper.c +4 -4
  218. data/src/lib/trema_wrapper.h +10 -10
  219. data/src/lib/utility.c +6 -6
  220. data/src/management/application.c +224 -0
  221. data/src/management/echo.c +185 -0
  222. data/src/management/set_logging_level.c +153 -0
  223. data/src/management/show_stats.c +150 -0
  224. data/src/management/trema_manager +184 -0
  225. data/src/packetin_filter/packetin_filter.c +31 -29
  226. data/src/switch_manager/cookie_table.c +7 -7
  227. data/src/switch_manager/dpid_table.c +3 -3
  228. data/src/switch_manager/dpid_table.h +0 -1
  229. data/src/switch_manager/management_interface.h +0 -1
  230. data/src/switch_manager/ofpmsg_recv.c +3 -3
  231. data/src/switch_manager/ofpmsg_recv.h +0 -1
  232. data/src/switch_manager/ofpmsg_send.c +2 -2
  233. data/src/switch_manager/ofpmsg_send.h +0 -1
  234. data/src/switch_manager/secure_channel_listener.c +2 -2
  235. data/src/switch_manager/secure_channel_receiver.c +2 -2
  236. data/src/switch_manager/secure_channel_receiver.h +0 -1
  237. data/src/switch_manager/secure_channel_sender.c +8 -1
  238. data/src/switch_manager/service_interface.c +5 -4
  239. data/src/switch_manager/switch.c +79 -53
  240. data/src/switch_manager/switch_manager.c +24 -22
  241. data/src/switch_manager/xid_table.c +6 -6
  242. data/src/switch_manager/xid_table.h +0 -1
  243. data/src/tremashark/README +4 -3
  244. data/src/tremashark/packet_capture.c +11 -9
  245. data/src/tremashark/pcap_queue.c +2 -2
  246. data/src/tremashark/pcap_queue.h +2 -2
  247. data/src/tremashark/plugin/packet-trema/Makefile +1 -1
  248. data/src/tremashark/plugin/packet-trema/Makefile.common +1 -1
  249. data/src/tremashark/plugin/packet-trema/packet-trema.c +50 -37
  250. data/src/tremashark/queue.c +5 -4
  251. data/src/tremashark/queue.h +1 -1
  252. data/src/tremashark/stdin_relay.c +10 -8
  253. data/src/tremashark/syslog_relay.c +14 -12
  254. data/src/tremashark/tremashark.c +15 -18
  255. data/trema +2 -96
  256. data/trema-config +2 -52
  257. data/trema.gemspec +12 -1
  258. data/unittests/cmockery_trema.h +14 -13
  259. data/unittests/lib/buffer_test.c +1 -1
  260. data/unittests/lib/byteorder_test.c +108 -25
  261. data/unittests/lib/daemon_test.c +141 -5
  262. data/unittests/lib/doubly_linked_list_test.c +5 -1
  263. data/unittests/lib/ether_test.c +4 -4
  264. data/unittests/lib/hash_table_test.c +5 -3
  265. data/unittests/lib/linked_list_test.c +82 -4
  266. data/unittests/lib/log_test.c +96 -5
  267. data/unittests/lib/management_interface_test.c +240 -0
  268. data/unittests/lib/management_service_interface_test.c +319 -0
  269. data/unittests/lib/message_queue_test.c +6 -2
  270. data/unittests/lib/messenger_test.c +167 -2
  271. data/unittests/lib/openflow_application_interface_test.c +16 -22
  272. data/unittests/lib/openflow_message_test.c +721 -24
  273. data/unittests/lib/packet_info_test.c +2 -2
  274. data/unittests/lib/packet_parser_test.c +15 -16
  275. data/unittests/lib/packetin_filter_interface_test.c +6 -6
  276. data/unittests/lib/persistent_storage_test.c +11 -11
  277. data/unittests/lib/stat_test.c +151 -7
  278. data/unittests/lib/timer_test.c +1 -1
  279. data/unittests/lib/trema_private_test.c +2 -2
  280. data/unittests/lib/trema_test.c +40 -12
  281. data/unittests/lib/utility_test.c +3 -3
  282. data/unittests/lib/wrapper_test.c +3 -3
  283. data/unittests/packetin_filter/packetin_filter_test.c +12 -11
  284. data/unittests/switch_manager/switch_manager_test.c +29 -22
  285. metadata +99 -20
  286. data/features/step_definitions/log_steps.rb +0 -90
  287. data/features/step_definitions/run_steps.rb +0 -91
  288. data/features/trema.feature +0 -27
  289. data/features/tutorial.hello_trema.feature +0 -27
  290. data/features/tutorial.packet_in.feature +0 -47
  291. data/features/tutorial.switch_info.feature +0 -55
  292. data/ruby/trema/command/usage.rb +0 -63
@@ -1,7 +1,5 @@
1
1
  /*
2
2
  * Unit tests for daemon functions.
3
- *
4
- * Author: Yasuhito Takamiya <yasuhito@gmail.com>
5
3
  *
6
4
  * Copyright (C) 2008-2012 NEC Corporation
7
5
  *
@@ -22,6 +20,7 @@
22
20
 
23
21
  #include <errno.h>
24
22
  #include <fcntl.h>
23
+ #include <limits.h>
25
24
  #include <stdio.h>
26
25
  #include <stdlib.h>
27
26
  #include <string.h>
@@ -128,7 +127,7 @@ mock_unlink( char *pathname ) {
128
127
 
129
128
 
130
129
  int
131
- mock_access( char *pathname, int mode) {
130
+ mock_access( char *pathname, int mode ) {
132
131
  check_expected( pathname );
133
132
  check_expected( mode );
134
133
  return ( int ) mock();
@@ -158,6 +157,28 @@ mock_kill( pid_t pid, int sig ) {
158
157
  }
159
158
 
160
159
 
160
+ static size_t link_length = 0;
161
+ static char *link_buffer = NULL;
162
+
163
+ ssize_t
164
+ mock_readlink( const char *path, char *buf, size_t bufsiz ) {
165
+ check_expected( path );
166
+ check_expected( buf );
167
+ check_expected( bufsiz );
168
+ if ( link_length > 0 ) {
169
+ memcpy( buf, link_buffer, link_length );
170
+ }
171
+ return ( ssize_t ) mock();
172
+ }
173
+
174
+
175
+ char *
176
+ mock_basename( char *path ) {
177
+ check_expected( path );
178
+ return ( char * )( intptr_t ) mock();
179
+ }
180
+
181
+
161
182
  int
162
183
  mock_rename( char *oldpath, char *newpath ) {
163
184
  check_expected( oldpath );
@@ -198,7 +219,7 @@ test_daemonize_fail_if_chdir_fail() {
198
219
 
199
220
  errno = EACCES;
200
221
  expect_string( mock_die, message, "Could not cd to /: Permission denied." );
201
-
222
+
202
223
  expect_assert_failure( daemonize( "/" ) );
203
224
  errno = 0;
204
225
  }
@@ -348,7 +369,7 @@ test_read_pid_successed() {
348
369
  read_length = strlen( valid_pid_string );
349
370
  will_return( mock_read, read_length );
350
371
 
351
- // Test if correctly read.
372
+ // Test if correctly kill.
352
373
  expect_value( mock_kill, pid, valid_pid );
353
374
  expect_value( mock_kill, sig, 0 );
354
375
  will_return( mock_kill, 0 );
@@ -356,6 +377,20 @@ test_read_pid_successed() {
356
377
  // Test if correctly close.
357
378
  expect_value( mock_close, fd, pid_file_fd );
358
379
 
380
+ // Test if correctly readlink.
381
+ char proc_path[] = "/proc/123/exe";
382
+ expect_string( mock_readlink, path, proc_path );
383
+ expect_not_value( mock_readlink, buf, NULL );
384
+ expect_value( mock_readlink, bufsiz, PATH_MAX - 1 );
385
+ char valid_exe_path[] = "/home/yasuhito/trema/bin/chess";
386
+ link_buffer = valid_exe_path;
387
+ link_length = strlen( valid_exe_path );
388
+ will_return( mock_readlink, link_length );
389
+
390
+ // Test if correctly basename.
391
+ expect_string( mock_basename, path, valid_exe_path );
392
+ will_return( mock_basename, strdup( "chess" ) );
393
+
359
394
  // Go
360
395
  pid_t pid = read_pid( "/home/yasuhito/trema/tmp", "chess" );
361
396
  assert_true( pid == valid_pid );
@@ -577,6 +612,105 @@ test_read_pid_fail_if_kill_fail_with_EPERM() {
577
612
  }
578
613
 
579
614
 
615
+ static void
616
+ test_read_pid_fail_if_readlink_fail() {
617
+ // Test if correctly access.
618
+ char path[] = "/home/yasuhito/trema/tmp/chess.pid";
619
+ expect_string( mock_access, pathname, path );
620
+ expect_value( mock_access, mode, R_OK );
621
+ will_return( mock_access, 0 );
622
+
623
+ // Test if correctly opened.
624
+ int pid_file_fd = 111;
625
+ expect_string( mock_open, pathname, path );
626
+ expect_value( mock_open, flags, O_RDONLY );
627
+ expect_value( mock_open, mode, 0 );
628
+ will_return( mock_open, pid_file_fd );
629
+
630
+ // Test if correctly read.
631
+ expect_value( mock_read, fd, pid_file_fd );
632
+ expect_not_value( mock_read, buf, NULL );
633
+ expect_value( mock_read, count, 10 - 1 );
634
+ char valid_pid_string[] = "123\n";
635
+ pid_t valid_pid = 123;
636
+ read_buffer = valid_pid_string;
637
+ read_length = strlen( valid_pid_string );
638
+ will_return( mock_read, read_length );
639
+
640
+ // Test if correctly kill.
641
+ expect_value( mock_kill, pid, valid_pid );
642
+ expect_value( mock_kill, sig, 0 );
643
+ will_return( mock_kill, 0 );
644
+
645
+ // Test if correctly close.
646
+ expect_value( mock_close, fd, pid_file_fd );
647
+
648
+ // Test if correctly readlink.
649
+ char proc_path[] = "/proc/123/exe";
650
+ expect_string( mock_readlink, path, proc_path );
651
+ expect_not_value( mock_readlink, buf, NULL );
652
+ expect_value( mock_readlink, bufsiz, PATH_MAX - 1 );
653
+ will_return( mock_readlink, -1 );
654
+
655
+ // Go
656
+ pid_t pid = read_pid( "/home/yasuhito/trema/tmp", "chess" );
657
+ assert_true( pid == -1 );
658
+ }
659
+
660
+
661
+ static void
662
+ test_read_pid_fail_if_strcmp_fail() {
663
+ // Test if correctly access.
664
+ char path[] = "/home/yasuhito/trema/tmp/chess.pid";
665
+ expect_string( mock_access, pathname, path );
666
+ expect_value( mock_access, mode, R_OK );
667
+ will_return( mock_access, 0 );
668
+
669
+ // Test if correctly opened.
670
+ int pid_file_fd = 111;
671
+ expect_string( mock_open, pathname, path );
672
+ expect_value( mock_open, flags, O_RDONLY );
673
+ expect_value( mock_open, mode, 0 );
674
+ will_return( mock_open, pid_file_fd );
675
+
676
+ // Test if correctly read.
677
+ expect_value( mock_read, fd, pid_file_fd );
678
+ expect_not_value( mock_read, buf, NULL );
679
+ expect_value( mock_read, count, 10 - 1 );
680
+ char valid_pid_string[] = "123\n";
681
+ pid_t valid_pid = 123;
682
+ read_buffer = valid_pid_string;
683
+ read_length = strlen( valid_pid_string );
684
+ will_return( mock_read, read_length );
685
+
686
+ // Test if correctly kill.
687
+ expect_value( mock_kill, pid, valid_pid );
688
+ expect_value( mock_kill, sig, 0 );
689
+ will_return( mock_kill, 0 );
690
+
691
+ // Test if correctly close.
692
+ expect_value( mock_close, fd, pid_file_fd );
693
+
694
+ // Test if correctly readlink.
695
+ char proc_path[] = "/proc/123/exe";
696
+ expect_string( mock_readlink, path, proc_path );
697
+ expect_not_value( mock_readlink, buf, NULL );
698
+ expect_value( mock_readlink, bufsiz, PATH_MAX - 1 );
699
+ char INVALID_exe_path[] = "/home/yasuhito/trema/bin/chess2";
700
+ link_buffer = INVALID_exe_path;
701
+ link_length = strlen( INVALID_exe_path );
702
+ will_return( mock_readlink, link_length );
703
+
704
+ // Test if correctly basename.
705
+ expect_string( mock_basename, path, INVALID_exe_path );
706
+ will_return( mock_basename, strdup( "chess2" ) );
707
+
708
+ // Go
709
+ pid_t pid = read_pid( "/home/yasuhito/trema/tmp", "chess" );
710
+ assert_true( pid == -1 );
711
+ }
712
+
713
+
580
714
  static void
581
715
  test_rename_pid_successed() {
582
716
  // Test if correctly unlink.
@@ -646,6 +780,8 @@ main() {
646
780
  unit_test( test_read_pid_fail_if_pid_is_zero ),
647
781
  unit_test( test_read_pid_fail_if_kill_fail_with_ESRCH ),
648
782
  unit_test( test_read_pid_fail_if_kill_fail_with_EPERM ),
783
+ unit_test( test_read_pid_fail_if_readlink_fail ),
784
+ unit_test( test_read_pid_fail_if_strcmp_fail ),
649
785
 
650
786
  // rename_pid() tests.
651
787
  unit_test( test_rename_pid_successed ),
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  * Unit tests for doubly linked list library.
3
- *
3
+ *
4
4
  * Author: Yasuhito Takamiya <yasuhito@gmail.com>
5
5
  *
6
6
  * Copyright (C) 2008-2012 NEC Corporation
@@ -274,6 +274,10 @@ test_find_element() {
274
274
  assert_true( new_element->next->next == e );
275
275
  e = find_element( new_element, element_data1 );
276
276
  assert_true( new_element->next->next->next == e );
277
+
278
+ e = find_element( new_element->next->next->next, element_data2 );
279
+ assert_true( new_element->next->next == e );
280
+
277
281
  e = find_element( new_element, no_such_element );
278
282
  assert_true( e == NULL );
279
283
 
@@ -44,8 +44,8 @@ mock_die( const char *format, ... ) {
44
44
  va_end( args );
45
45
  check_expected( output );
46
46
 
47
- mock_assert( false, "mock_die", __FILE__, __LINE__ );
48
- }
47
+ mock_assert( false, "mock_die", __FILE__, __LINE__ );
48
+ }
49
49
 
50
50
 
51
51
  /******************************************************************************
@@ -60,9 +60,9 @@ setup() {
60
60
 
61
61
 
62
62
  static void
63
- teardown() {
63
+ teardown() {
64
64
  die = original_die;
65
- }
65
+ }
66
66
 
67
67
 
68
68
  /******************************************************************************
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  * Unit tests for hash table.
3
- *
3
+ *
4
4
  * Author: Yasuhito Takamiya <yasuhito@gmail.com>
5
5
  *
6
6
  * Copyright (C) 2008-2012 NEC Corporation
@@ -134,7 +134,7 @@ append_back_foreach( void *key, void *value, void *user_data ) {
134
134
  assert_true( user_data == NULL );
135
135
 
136
136
  int i;
137
- for ( i = 0 ; i < 3 ; i++ ) {
137
+ for ( i = 0; i < 3; i++ ) {
138
138
  if ( abc[ i ] == NULL ) {
139
139
  abc[ i ] = value;
140
140
  return;
@@ -241,7 +241,9 @@ test_iterate_empty_hash() {
241
241
  table = create_hash( compare_atom, hash_atom );
242
242
  init_hash_iterator( table, &iter );
243
243
 
244
- while ( iterate_hash_next( &iter ) != NULL ) { UNREACHABLE(); }
244
+ while ( iterate_hash_next( &iter ) != NULL ) {
245
+ UNREACHABLE();
246
+ }
245
247
 
246
248
  delete_hash( table );
247
249
  }
@@ -1,7 +1,5 @@
1
1
  /*
2
2
  * Unit tests for linked list library.
3
- *
4
- * Author: Yasuhito Takamiya <yasuhito@gmail.com>
5
3
  *
6
4
  * Copyright (C) 2008-2012 NEC Corporation
7
5
  *
@@ -22,6 +20,7 @@
22
20
 
23
21
  #include <stdio.h>
24
22
  #include <string.h>
23
+ #include "checks.h"
25
24
  #include "cmockery_trema.h"
26
25
  #include "linked_list.h"
27
26
  #include "utility.h"
@@ -72,7 +71,7 @@ static char delete_me[] = "delete me!";
72
71
  /********************************************************************************
73
72
  * Tests.
74
73
  ********************************************************************************/
75
-
74
+
76
75
  static void
77
76
  test_create_list() {
78
77
  assert_true( create_list( &new_list ) );
@@ -192,7 +191,7 @@ test_delete_first_element() {
192
191
  append_to_tail( &new_list, delete_me );
193
192
  append_to_tail( &new_list, bravo );
194
193
  append_to_tail( &new_list, charlie );
195
-
194
+
196
195
  assert_true( delete_element( &new_list, delete_me ) );
197
196
  assert_string_equal( new_list->data, "bravo" );
198
197
  assert_string_equal( new_list->next->data, "charlie" );
@@ -281,6 +280,81 @@ test_list_length_of_empty_list() {
281
280
  }
282
281
 
283
282
 
283
+ static void
284
+ get_sum_length( void *data, void *user_data ) {
285
+ size_t *sum_length = user_data;
286
+ *sum_length += strlen( data );
287
+ }
288
+
289
+
290
+ static void
291
+ test_iterate_list() {
292
+ create_list( &new_list );
293
+
294
+ append_to_tail( &new_list, &alpha );
295
+ append_to_tail( &new_list, &bravo );
296
+ append_to_tail( &new_list, &charlie );
297
+
298
+ size_t sum_length = 0;
299
+ iterate_list( new_list, get_sum_length, &sum_length );
300
+ assert_int_equal( 17, sum_length );
301
+
302
+ delete_list( new_list );
303
+ }
304
+
305
+
306
+ static bool
307
+ find_bravo( void *data, void *user_data ) {
308
+ UNUSED( user_data );
309
+
310
+ if ( strcmp( data, "bravo" ) == 0 ) {
311
+ return true;
312
+ }
313
+ else {
314
+ return false;
315
+ }
316
+ }
317
+
318
+
319
+ static bool
320
+ find_nothing( void *data, void *user_data ) {
321
+ UNUSED( data );
322
+ UNUSED( user_data );
323
+
324
+ return false;
325
+ }
326
+
327
+
328
+ static void
329
+ test_find_list_custom() {
330
+ create_list( &new_list );
331
+
332
+ append_to_tail( &new_list, &alpha );
333
+ append_to_tail( &new_list, &bravo );
334
+ append_to_tail( &new_list, &charlie );
335
+
336
+ void *found = find_list_custom( new_list, find_bravo, NULL );
337
+ assert_string_equal( found, "bravo" );
338
+
339
+ delete_list( new_list );
340
+ }
341
+
342
+
343
+ static void
344
+ test_find_list_custom_fail() {
345
+ create_list( &new_list );
346
+
347
+ append_to_tail( &new_list, &alpha );
348
+ append_to_tail( &new_list, &bravo );
349
+ append_to_tail( &new_list, &charlie );
350
+
351
+ void *found = find_list_custom( new_list, find_nothing, NULL );
352
+ assert_true( found == NULL );
353
+
354
+ delete_list( new_list );
355
+ }
356
+
357
+
284
358
  static void
285
359
  test_delete_list() {
286
360
  create_list( &new_list );
@@ -328,6 +402,10 @@ main() {
328
402
  unit_test( test_list_length ),
329
403
  unit_test( test_list_length_of_empty_list ),
330
404
 
405
+ unit_test( test_iterate_list ),
406
+ unit_test( test_find_list_custom ),
407
+ unit_test( test_find_list_custom_fail ),
408
+
331
409
  unit_test( test_delete_list ),
332
410
  };
333
411
  setup_leak_detector();
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  * Unit tests for logging functions and macros.
3
- *
3
+ *
4
4
  * Author: Yasuhito Takamiya <yasuhito@gmail.com>
5
5
  *
6
6
  * Copyright (C) 2008-2012 NEC Corporation
@@ -122,10 +122,17 @@ reset_LOGGING_LEVEL() {
122
122
  }
123
123
 
124
124
 
125
+ static void
126
+ reset_LOGGING_FACILITY() {
127
+ unsetenv( "LOGGING_FACILITY" );
128
+ }
129
+
130
+
125
131
  static void
126
132
  setup() {
127
133
  finalize_log();
128
134
  reset_LOGGING_LEVEL();
135
+ reset_LOGGING_FACILITY();
129
136
 
130
137
  setup_leak_detector();
131
138
 
@@ -177,6 +184,7 @@ static void
177
184
  teardown() {
178
185
  finalize_log();
179
186
  reset_LOGGING_LEVEL();
187
+ reset_LOGGING_FACILITY();
180
188
 
181
189
  teardown_leak_detector();
182
190
 
@@ -213,6 +221,19 @@ test_init_log_opens_syslog() {
213
221
  }
214
222
 
215
223
 
224
+ void
225
+ test_init_log_reads_LOGGING_FACILITY_environment_variable() {
226
+ const char *ident = "tetris";
227
+ setenv( "LOGGING_FACILITY", "LOCAL7", 1 );
228
+
229
+ expect_string( mock_openlog, ident, ident );
230
+ expect_value( mock_openlog, option, LOG_NDELAY );
231
+ expect_value( mock_openlog, facility, LOG_LOCAL7 );
232
+ init_log( ident, get_trema_tmp(), LOGGING_TYPE_SYSLOG );
233
+ assert_true( syslog_initialized );
234
+ }
235
+
236
+
216
237
  /********************************************************************************
217
238
  * Finalization test.
218
239
  ********************************************************************************/
@@ -235,14 +256,14 @@ test_default_logging_level_is_INFO() {
235
256
 
236
257
 
237
258
  void
238
- test_set_logging_level_succeed() {
259
+ test_set_logging_level_succeeds() {
239
260
  set_logging_level( "critical" );
240
261
  assert_int_equal( LOG_CRIT, get_logging_level() );
241
262
  }
242
263
 
243
264
 
244
265
  void
245
- test_set_logging_level_fail_with_invalid_value() {
266
+ test_set_logging_level_fails_with_invalid_value() {
246
267
  expect_assert_failure( set_logging_level( "INVALID_LEVEL" ) );
247
268
  }
248
269
 
@@ -264,6 +285,63 @@ test_LOGGING_LEVEL_overrides_logging_level() {
264
285
  }
265
286
 
266
287
 
288
+ void
289
+ test_valid_logging_level_returns_true_with_valid_logging_level() {
290
+ assert_true( valid_logging_level( "information" ) );
291
+ }
292
+
293
+
294
+ void
295
+ test_valid_logging_level_returns_false_with_invalid_logging_level() {
296
+ assert_false( valid_logging_level( "INVALID_LOGGING_LEVEL" ) );
297
+ }
298
+
299
+
300
+ /********************************************************************************
301
+ * Syslog facility tests.
302
+ ********************************************************************************/
303
+
304
+ void
305
+ test_default_faciliity_is_USER() {
306
+ const char *ident = "tetris";
307
+ expect_string( mock_openlog, ident, ident );
308
+ expect_value( mock_openlog, option, LOG_NDELAY );
309
+ expect_value( mock_openlog, facility, LOG_USER );
310
+ init_log( ident, get_trema_tmp(), LOGGING_TYPE_SYSLOG );
311
+ }
312
+
313
+
314
+ void
315
+ test_set_syslog_facility_succeeds() {
316
+ const char *ident = "tetris";
317
+ set_syslog_facility( "LOCAL0" );
318
+
319
+ expect_string( mock_openlog, ident, ident );
320
+ expect_value( mock_openlog, option, LOG_NDELAY );
321
+ expect_value( mock_openlog, facility, LOG_LOCAL0 );
322
+ init_log( ident, get_trema_tmp(), LOGGING_TYPE_SYSLOG );
323
+ }
324
+
325
+
326
+ void
327
+ test_set_syslog_facility_fails_with_invalid_value() {
328
+ expect_assert_failure( set_syslog_facility( "INVALID_FACILITY" ) );
329
+ }
330
+
331
+
332
+ void
333
+ test_LOGGING_FACILITY_overrides_logging_facility() {
334
+ const char *ident = "tetris";
335
+ setenv( "LOGGING_FACILITY", "LOCAL0", 1 );
336
+ set_syslog_facility( "LOCAL7" );
337
+
338
+ expect_string( mock_openlog, ident, ident );
339
+ expect_value( mock_openlog, option, LOG_NDELAY );
340
+ expect_value( mock_openlog, facility, LOG_LOCAL0 );
341
+ init_log( ident, get_trema_tmp(), LOGGING_TYPE_SYSLOG );
342
+ }
343
+
344
+
267
345
  /********************************************************************************
268
346
  * critical() tests.
269
347
  ********************************************************************************/
@@ -499,20 +577,33 @@ main() {
499
577
  reset_LOGGING_LEVEL, reset_LOGGING_LEVEL ),
500
578
  unit_test_setup_teardown( test_init_log_opens_syslog,
501
579
  setup, teardown ),
580
+ unit_test_setup_teardown( test_init_log_reads_LOGGING_FACILITY_environment_variable,
581
+ setup, teardown ),
502
582
 
503
583
  unit_test_setup_teardown( test_finalize_log_closes_syslog,
504
584
  setup_logger_syslog, teardown ),
505
585
 
506
586
  unit_test_setup_teardown( test_default_logging_level_is_INFO,
507
587
  setup_logger_file, teardown ),
508
- unit_test_setup_teardown( test_set_logging_level_succeed,
588
+ unit_test_setup_teardown( test_set_logging_level_succeeds,
509
589
  setup_logger_file, teardown ),
510
- unit_test_setup_teardown( test_set_logging_level_fail_with_invalid_value,
590
+ unit_test_setup_teardown( test_set_logging_level_fails_with_invalid_value,
511
591
  setup_logger_file, teardown ),
512
592
  unit_test_setup_teardown( test_set_logging_level_is_called_before_init_log,
513
593
  setup, teardown ),
514
594
  unit_test_setup_teardown( test_LOGGING_LEVEL_overrides_logging_level,
515
595
  setup, teardown ),
596
+ unit_test( test_valid_logging_level_returns_true_with_valid_logging_level ),
597
+ unit_test( test_valid_logging_level_returns_false_with_invalid_logging_level ),
598
+
599
+ unit_test_setup_teardown( test_default_faciliity_is_USER,
600
+ setup, teardown ),
601
+ unit_test_setup_teardown( test_set_syslog_facility_succeeds,
602
+ setup, teardown ),
603
+ unit_test_setup_teardown( test_set_syslog_facility_fails_with_invalid_value,
604
+ setup, teardown ),
605
+ unit_test_setup_teardown( test_LOGGING_FACILITY_overrides_logging_facility,
606
+ setup, teardown ),
516
607
 
517
608
  unit_test_setup_teardown( test_critical_logs_if_logging_level_is_CRITICAL,
518
609
  setup_logger_file, teardown ),