test-qnetd-cluster-list.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. memset(&addr, 0, sizeof(addr));
  50. tmp_client = qnetd_client_list_add(&clients, NULL, &addr, 1000, 2, 1000);
  51. assert(tmp_client != NULL);
  52. tmp_client->cluster_name = malloc(cluster_name_len + 1);
  53. assert(tmp_client->cluster_name != NULL);
  54. memcpy(tmp_client->cluster_name, cluster_name, cluster_name_len);
  55. tmp_client->cluster_name_len = cluster_name_len;
  56. tmp_cluster = qnetd_cluster_list_add_client(&clusters, tmp_client);
  57. assert(cluster != NULL);
  58. tmp_client->cluster = tmp_cluster;
  59. *client = tmp_client;
  60. *cluster = tmp_cluster;
  61. }
  62. static int
  63. no_clients_in_cluster(struct qnetd_cluster *cluster)
  64. {
  65. int i;
  66. struct qnetd_client *client;
  67. i = 0;
  68. TAILQ_FOREACH(client, &cluster->client_list, cluster_entries) {
  69. i++;
  70. }
  71. return (i);
  72. }
  73. static int
  74. no_clusters(void)
  75. {
  76. int i;
  77. struct qnetd_cluster *cluster;
  78. i = 0;
  79. TAILQ_FOREACH(cluster, &clusters, entries) {
  80. i++;
  81. }
  82. return (i);
  83. }
  84. static int
  85. is_client_in_cluster(struct qnetd_cluster *cluster, const struct qnetd_client *client)
  86. {
  87. struct qnetd_client *tmp_client;
  88. TAILQ_FOREACH(tmp_client, &cluster->client_list, cluster_entries) {
  89. if (tmp_client == client) {
  90. return (1);
  91. }
  92. }
  93. return (0);
  94. }
  95. static void
  96. del_client(struct qnetd_client *client)
  97. {
  98. qnetd_cluster_list_del_client(&clusters, client->cluster, client);
  99. qnetd_client_list_del(&clients, client);
  100. }
  101. int
  102. main(void)
  103. {
  104. struct qnetd_client *client[4];
  105. struct qnetd_cluster *cluster[4];
  106. const char *cl_name;
  107. qnetd_client_list_init(&clients);
  108. qnetd_cluster_list_init(&clusters);
  109. assert(no_clusters() == 0);
  110. cl_name = "test_cluster";
  111. add_client(cl_name, strlen(cl_name), &client[0], &cluster[0]);
  112. assert(no_clusters() == 1);
  113. add_client(cl_name, strlen(cl_name), &client[1], &cluster[1]);
  114. assert(no_clusters() == 1);
  115. cl_name = "cluster2";
  116. add_client(cl_name, strlen(cl_name), &client[2], &cluster[2]);
  117. assert(no_clusters() == 2);
  118. add_client(cl_name, strlen(cl_name), &client[3], &cluster[3]);
  119. assert(no_clusters() == 2);
  120. assert(cluster[0] == cluster[1]);
  121. assert(cluster[2] == cluster[3]);
  122. assert(cluster[0] != cluster[2]);
  123. assert(no_clients_in_cluster(cluster[0]) == 2);
  124. assert(no_clients_in_cluster(cluster[2]) == 2);
  125. assert(is_client_in_cluster(cluster[0], client[0]));
  126. assert(is_client_in_cluster(client[0]->cluster, client[0]));
  127. assert(is_client_in_cluster(cluster[0], client[1]));
  128. assert(!is_client_in_cluster(cluster[0], client[2]));
  129. assert(!is_client_in_cluster(cluster[0], client[3]));
  130. assert(!is_client_in_cluster(cluster[2], client[0]));
  131. assert(!is_client_in_cluster(cluster[2], client[1]));
  132. assert(is_client_in_cluster(cluster[2], client[2]));
  133. assert(is_client_in_cluster(cluster[2], client[3]));
  134. assert(is_client_in_cluster(client[2]->cluster, client[2]));
  135. del_client(client[0]);
  136. assert(no_clusters() == 2);
  137. assert(no_clients_in_cluster(cluster[0]) == 1);
  138. assert(no_clients_in_cluster(cluster[2]) == 2);
  139. assert(!is_client_in_cluster(cluster[0], client[0]));
  140. assert(is_client_in_cluster(cluster[0], client[1]));
  141. assert(!is_client_in_cluster(cluster[0], client[2]));
  142. assert(!is_client_in_cluster(cluster[0], client[3]));
  143. add_client(cl_name, strlen(cl_name), &client[0], &cluster[0]);
  144. assert(no_clients_in_cluster(cluster[1]) == 1);
  145. assert(no_clients_in_cluster(cluster[2]) == 3);
  146. assert(!is_client_in_cluster(cluster[1], client[0]));
  147. assert(is_client_in_cluster(cluster[1], client[1]));
  148. assert(!is_client_in_cluster(cluster[1], client[2]));
  149. assert(!is_client_in_cluster(cluster[1], client[3]));
  150. assert(is_client_in_cluster(cluster[2], client[0]));
  151. assert(!is_client_in_cluster(cluster[2], client[1]));
  152. assert(is_client_in_cluster(cluster[2], client[2]));
  153. assert(is_client_in_cluster(cluster[2], client[3]));
  154. del_client(client[1]);
  155. assert(no_clusters() == 1);
  156. del_client(client[2]);
  157. assert(no_clusters() == 1);
  158. del_client(client[3]);
  159. assert(no_clusters() == 1);
  160. del_client(client[0]);
  161. assert(no_clusters() == 0);
  162. return (0);
  163. }