1 /*----------------------------------------------------------------------------
  2 Name:      xmlBlaster/testsuite/src/c/TestError.c
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Test C client library
  6 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
  7 Compile:   cd xmlBlaster; build c
  8 Invoke:    Start 'java org.xmlBlaster.Main' and then 'TestError'
  9 See:       http://www.xmlblaster.org/xmlBlaster/doc/requirements/c.client.socket.html
 10 See:       http://www.xmlblaster.org/xmlBlaster/doc/requirements/protocol.socket.html
 11 -----------------------------------------------------------------------------*/
 12 #include <stdio.h>
 13 #include <stdlib.h>
 14 #include <string.h>
 15 #include <XmlBlasterAccessUnparsed.h>
 16 #include "test.h"
 17 
 18 /**
 19  * Here we receive the callback messages from xmlBlaster
 20  * mu_assert() does not help here as it is another thread
 21  */
 22 static bool myUpdate(MsgUnitArr *msgUnitArr, void *userData, XmlBlasterException *xmlBlasterException)
 23 {
 24    if (msgUnitArr != 0) ;  /* Supress compiler warnings */
 25    if (userData != 0) ;
 26    if (xmlBlasterException != 0) ;
 27    return true;
 28 }
 29 
 30 /**
 31  * Test illegal arguments
 32  */
 33 static const char * test_illegalSubscribe()
 34 {
 35    char *response = (char *)0;
 36    XmlBlasterException xmlBlasterException;
 37    XmlBlasterAccessUnparsed *xa = 0;
 38 
 39    printf("\n[client] test_illegalSubscribe() ...\n");
 40 
 41    xa = getXmlBlasterAccessUnparsed(0, 0);
 42 
 43    { /* subscribe: With illegal arguments ... */
 44       printf("[client] Subscribe with all illegal args ...\n");
 45       response = xa->subscribe(0, 0, 0, 0);
 46       if (response == 0) {
 47          printf("[client] Subscribe response==NULL is OK, we provided illegal arguments\n");
 48       }
 49       else {
 50          xmlBlasterFree(response);
 51          mu_fail("[TEST FAIL] Subscribe response was not expected, we provided illegal arguments");
 52       }
 53    }
 54 
 55    { /* subscribe: With illegal arguments ... */
 56       printf("[client] Subscribe with illegal exception pointer ...\n");
 57       response = xa->subscribe(xa, 0, 0, 0);
 58       if (response == 0) {
 59          printf("[client] Subscribe response==NULL is OK, we provided illegal arguments\n");
 60       }
 61       else {
 62          xmlBlasterFree(response);
 63          mu_fail("[TEST FAIL] Subscribe response was not expected, we provided illegal arguments");
 64       }
 65    }
 66 
 67    { /* subscribe: With illegal arguments ... */
 68       printf("[client] Subscribe message 'NULL' ...\n");
 69       response = xa->subscribe(xa, 0, 0, &xmlBlasterException);
 70       if (*xmlBlasterException.errorCode != '\0') {
 71          printf("[client] Subscribe exception is OK, we provided illegal arguments: Caught exception in subscribe errorCode=%s, message=%s\n",
 72                   xmlBlasterException.errorCode, xmlBlasterException.message);
 73       }
 74       else {
 75          xmlBlasterFree(response);
 76          mu_fail("[TEST FAIL] Subscribe response was not expected, we provided illegal arguments\n");
 77       }
 78    }
 79 
 80    { /* subscribe: We are not connected yet !... */
 81       const char *key = "<key oid='HelloWorld'/>";
 82       const char *qos = "<qos/>";
 83       printf("[client] Subscribe message 'HelloWorld' ...\n");
 84       response = xa->subscribe(xa, key, qos, &xmlBlasterException);
 85       if (*xmlBlasterException.errorCode != '\0') {
 86          printf("[client] Subscribe exception is OK, we are not connected: Caught exception in subscribe errorCode=%s, message=%s\n",
 87                   xmlBlasterException.errorCode, xmlBlasterException.message);
 88       }
 89       else {
 90          xmlBlasterFree(response);
 91          mu_fail("[TEST FAIL] Subscribe response was not expected, we are not connected");
 92       }
 93    }
 94 
 95    freeXmlBlasterAccessUnparsed(xa);
 96    xa = 0;
 97    freeXmlBlasterAccessUnparsed(xa);
 98    printf("[client] SUCCESS test_illegalSubscribe()\n");
 99    return 0;
100 }
101 
102 
103 /**
104  * Test illegal arguments
105  */
106 static const char * test_illegalDisconnect()
107 {
108    XmlBlasterException xmlBlasterException;
109    XmlBlasterAccessUnparsed *xa = 0;
110    char *response = 0;
111 
112    initializeXmlBlasterException(&xmlBlasterException);
113 
114    printf("\n[client] test_illegalDisconnect() ...\n");
115 
116    xa = getXmlBlasterAccessUnparsed(0, 0);
117    
118    printf("[client] Testing disconnect behavior\n");
119    if (xa->disconnect(0, 0, 0) == false) {
120       printf("[client] Disconnect exception is OK, we are not connected: Caught exception in disconnect errorCode=%s, message=%s\n",
121                   xmlBlasterException.errorCode, xmlBlasterException.message);
122    }
123    else {
124       xmlBlasterFree(response);
125       mu_fail("[TEST FAIL] disconnect true was not expected, we are not connected");
126    }
127 
128    if (xa->disconnect(xa, 0, &xmlBlasterException) == false) {
129       printf("[client] Disconnect exception is OK, we are not connected: Caught exception in disconnect errorCode=%s, message=%s\n",
130                   xmlBlasterException.errorCode, xmlBlasterException.message);
131    }
132    else {
133       xmlBlasterFree(response);
134       mu_fail("[TEST FAIL] disconnect true was not expected, we are not connected");
135    }
136 
137 
138    freeXmlBlasterAccessUnparsed(xa);
139    printf("[client] SUCCESS test_illegalDisconnect()\n");
140    return 0;
141 }
142 
143 /**
144  * Test illegal arguments
145  */
146 static const char * test_illegalConnect()
147 {
148    XmlBlasterException xmlBlasterException;
149    XmlBlasterAccessUnparsed *xa = 0;
150    char *response = 0;
151    
152    initializeXmlBlasterException(&xmlBlasterException);
153 
154    printf("\n[client] test_illegalConnect() ...\n");
155 
156    xa = getXmlBlasterAccessUnparsed(0, 0);
157    
158    printf("[client] Testing initialize() behavior\n");
159    if (xa->initialize(0, 0, 0) == false) {
160       printf("[client] Initialize exception is OK, we are not connected: errorCode=%s, message=%s\n",
161                   xmlBlasterException.errorCode, xmlBlasterException.message);
162    }
163    else {
164       xmlBlasterFree(response);
165       mu_fail("[TEST FAIL] initialize true was not expected, we are not connected");
166    }
167 
168    if (xa->initialize(xa, 0, &xmlBlasterException) == false) {
169       mu_assert("initialize false was not expected (check if a server is running), we provided a NULL callback", false);
170    }
171    else {
172       printf("[client] Initialize with updateP is NULL is OK, the default handler is used.");
173       xmlBlasterFree(response);
174    }
175 
176    printf("[client] Testing connect() behavior\n");
177    response = xa->connect(0, 0, 0, 0);
178    if (response == 0) {
179       printf("[client] connect() return NULL is OK with NULL args\n");
180    }
181    else {
182       xmlBlasterFree(response);
183       mu_fail("[TEST FAIL] connect() return was not expected, we are not connected");
184    }
185 
186    response = xa->connect(xa, 0, 0, 0);
187    if (response == 0) {
188       printf("[client] connect() return NULL is OK with NULL exception\n");
189    }
190    else {
191       xmlBlasterFree(response);
192       mu_fail("[TEST FAIL] connect() return was not expected, we are not connected");
193    }
194 
195    response = xa->connect(xa, 0, myUpdate, &xmlBlasterException);
196    if (response == 0) {
197       printf("[client] connect() return NULL is OK with NULL ConnectQos: , errorCode=%s, message=%s\n",
198                xmlBlasterException.errorCode, xmlBlasterException.message);
199    }
200    else {
201       xmlBlasterFree(response);
202       mu_fail("[TEST FAIL] connect() return was not expected for ConnectQos==NULL");
203    }
204 
205    freeXmlBlasterAccessUnparsed(xa);
206    printf("[client] SUCCESS test_illegalConnect()\n");
207    return 0;
208 }
209 
210 static const char *all_tests()
211 {
212    mu_run_test(test_illegalSubscribe);
213    mu_run_test(test_illegalDisconnect);
214    mu_run_test(test_illegalConnect);
215    printf("[client] Good bye.\n");
216    return 0;
217 }
218 
219 int main(int argc, char **argv)
220 {
221    const char *result = all_tests();
222 
223    if (result != 0) {
224       printf("%s\n", result);
225    }
226    else {
227       printf("ALL TESTS PASSED\n");
228    }
229    printf("Tests run: %d\n", tests_run);
230 
231    return result != 0;
232 }


syntax highlighted by Code2HTML, v. 0.9.1