test-qnetd-cluster-list.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2015-2016 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <assert.h>
  36. #include <string.h>
  37. #include "qnetd-cluster-list.h"
  38. #include "qnetd-client.h"
  39. #include "qnetd-client-list.h"
  40. static struct qnetd_client_list clients;
  41. static struct qnetd_cluster_list clusters;
  42. static void
  43. add_client(const char *cluster_name, size_t cluster_name_len,
  44. struct qnetd_client **client, struct qnetd_cluster **cluster)
  45. {
  46. PRNetAddr addr;
  47. struct qnetd_client *tmp_client;
  48. struct qnetd_cluster *tmp_cluster;
  49. char *client_addr_str;
  50. memset(&addr, 0, sizeof(addr));
  51. client_addr_str = strdup("addrstr");
  52. assert(client_addr_str != NULL);
  53. tmp_client = qnetd_client_list_add(&clients, NULL, &addr, client_addr_str, 1000, 2, 1000, NULL);
  54. assert(tmp_client != NULL);
  55. tmp_client->cluster_name = malloc(cluster_name_len + 1);
  56. assert(tmp_client->cluster_name != NULL);
  57. memcpy(tmp_client->cluster_name, cluster_name, cluster_name_len);
  58. tmp_client->cluster_name_len = cluster_name_len;
  59. tmp_cluster = qnetd_cluster_list_add_client(&clusters, tmp_client);
  60. assert(cluster != NULL);
  61. tmp_client->cluster = tmp_cluster;
  62. *client = tmp_client;
  63. *cluster = tmp_cluster;
  64. }
  65. static int
  66. no_clients_in_cluster(struct qnetd_cluster *cluster)
  67. {
  68. int i;
  69. struct qnetd_client *client;
  70. i = 0;
  71. TAILQ_FOREACH(client, &cluster->client_list, cluster_entries) {
  72. i++;
  73. }
  74. return (i);
  75. }
  76. static int
  77. no_clusters(void)
  78. {
  79. int i;
  80. struct qnetd_cluster *cluster;
  81. i = 0;
  82. TAILQ_FOREACH(cluster, &clusters, entries) {
  83. i++;
  84. }
  85. return (i);
  86. }
  87. static int
  88. is_client_in_cluster(struct qnetd_cluster *cluster, const struct qnetd_client *client)
  89. {
  90. struct qnetd_client *tmp_client;
  91. TAILQ_FOREACH(tmp_client, &cluster->client_list, cluster_entries) {
  92. if (tmp_client == client) {
  93. return (1);
  94. }
  95. }
  96. return (0);
  97. }
  98. static void
  99. del_client(struct qnetd_client *client)
  100. {
  101. qnetd_cluster_list_del_client(&clusters, client->cluster, client);
  102. qnetd_client_list_del(&clients, client);
  103. }
  104. int
  105. main(void)
  106. {
  107. struct qnetd_client *client[4];
  108. struct qnetd_cluster *cluster[4];
  109. const char *cl_name;
  110. qnetd_client_list_init(&clients);
  111. qnetd_cluster_list_init(&clusters);
  112. assert(no_clusters() == 0);
  113. cl_name = "test_cluster";
  114. add_client(cl_name, strlen(cl_name), &client[0], &cluster[0]);
  115. assert(no_clusters() == 1);
  116. add_client(cl_name, strlen(cl_name), &client[1], &cluster[1]);
  117. assert(no_clusters() == 1);
  118. cl_name = "cluster2";
  119. add_client(cl_name, strlen(cl_name), &client[2], &cluster[2]);
  120. assert(no_clusters() == 2);
  121. add_client(cl_name, strlen(cl_name), &client[3], &cluster[3]);
  122. assert(no_clusters() == 2);
  123. assert(cluster[0] == cluster[1]);
  124. assert(cluster[2] == cluster[3]);
  125. assert(cluster[0] != cluster[2]);
  126. assert(no_clients_in_cluster(cluster[0]) == 2);
  127. assert(no_clients_in_cluster(cluster[2]) == 2);
  128. assert(is_client_in_cluster(cluster[0], client[0]));
  129. assert(is_client_in_cluster(client[0]->cluster, client[0]));
  130. assert(is_client_in_cluster(cluster[0], client[1]));
  131. assert(!is_client_in_cluster(cluster[0], client[2]));
  132. assert(!is_client_in_cluster(cluster[0], client[3]));
  133. assert(!is_client_in_cluster(cluster[2], client[0]));
  134. assert(!is_client_in_cluster(cluster[2], client[1]));
  135. assert(is_client_in_cluster(cluster[2], client[2]));
  136. assert(is_client_in_cluster(cluster[2], client[3]));
  137. assert(is_client_in_cluster(client[2]->cluster, client[2]));
  138. del_client(client[0]);
  139. assert(no_clusters() == 2);
  140. assert(no_clients_in_cluster(cluster[0]) == 1);
  141. assert(no_clients_in_cluster(cluster[2]) == 2);
  142. assert(!is_client_in_cluster(cluster[0], client[0]));
  143. assert(is_client_in_cluster(cluster[0], client[1]));
  144. assert(!is_client_in_cluster(cluster[0], client[2]));
  145. assert(!is_client_in_cluster(cluster[0], client[3]));
  146. add_client(cl_name, strlen(cl_name), &client[0], &cluster[0]);
  147. assert(no_clients_in_cluster(cluster[1]) == 1);
  148. assert(no_clients_in_cluster(cluster[2]) == 3);
  149. assert(!is_client_in_cluster(cluster[1], client[0]));
  150. assert(is_client_in_cluster(cluster[1], client[1]));
  151. assert(!is_client_in_cluster(cluster[1], client[2]));
  152. assert(!is_client_in_cluster(cluster[1], client[3]));
  153. assert(is_client_in_cluster(cluster[2], client[0]));
  154. assert(!is_client_in_cluster(cluster[2], client[1]));
  155. assert(is_client_in_cluster(cluster[2], client[2]));
  156. assert(is_client_in_cluster(cluster[2], client[3]));
  157. del_client(client[1]);
  158. assert(no_clusters() == 1);
  159. del_client(client[2]);
  160. assert(no_clusters() == 1);
  161. del_client(client[3]);
  162. assert(no_clusters() == 1);
  163. del_client(client[0]);
  164. assert(no_clusters() == 0);
  165. return (0);
  166. }