Test Programs

Author:

Stefan Eletzhofer

Date:

Feb 27, 2024

Abstract

In order to test the functionality, and also to demo the library features, we provide several test programs.

They’re listed here for reference.

Warning

All the examples below need a Windchill system with nexiles|gateway and the nexiles|gateway file service extension installed.

Test Account

To aid testing w/o the need to roll-out on the customer’s server, we have created a test account on our Windchill system:

Windchill base URL

http://skynet.solunor.com:1080/Windchill

user name

siemens

password

SiemensDemo

The account has access to one product container Siemens TDSM Test. For testing convenience, there’s also a existing business object with the number 0000000663.

Test Program: nxfs_example_upload.c

Purpose

Show the steps needed to upload a file to a Windchill business object.

Building

The provided make file contains a test target, which will build the program.

Usage

To use the example, you need to specify the Windchill base URL and your credentials. For testing, we’ve created a account on our Windchill system:

So – using the nexiles Windchill server – the test program needs to be called with:

$ ./nxfs_example_upload http://skynet.solunor.com:1080/Windchill siemens SiemensDemo 0000000663 <<somefile>>

Where <<somefile>> is the name of a file to upload.

The file should then visible in Windchill.

Source Code

  1/****************************************************************************\
  2 * nxfs_example_upload.c -- a sample demonstrating the upload of a file
  3 */
  4
  5#include <stdio.h>
  6#include <stdlib.h>
  7#include <string.h>
  8#include <assert.h>
  9#include <fcntl.h>
 10#include <sys/stat.h>
 11#ifndef WIN32
 12# include <unistd.h>
 13#endif
 14
 15#include "nxdebug.h"              /* logging macros */
 16#include "nxwin.h"                /* macros and types used for windows only */
 17#include "nxfileservice.h"        /* nexiles|gateway fileservice */
 18
 19/*****************************************************************************
 20 * defines
 21 */
 22
 23/*****************************************************************************
 24 * types
 25 */
 26
 27/*****************************************************************************
 28 * externs
 29 */
 30
 31/*****************************************************************************
 32 * module globals
 33 */
 34
 35/* enable logging for this module */
 36static int dbg = 1;
 37
 38/*****************************************************************************
 39 * module private pototypes
 40 */
 41
 42/*****************************************************************************/
 43/*****************************************************************************/
 44/*****************************************************************************/
 45
 46/*****************************************************************************
 47 * main()
 48 */
 49int main(int argc, const char *argv[])
 50{
 51  int         ret           = NX_OK;
 52  nxfs_t      *nxfs         = NULL;
 53
 54  const char *filename      = NULL;
 55  const char *url           = NULL;
 56  const char *number        = NULL;
 57  const char *username      = NULL;
 58  const char *password      = NULL;
 59
 60  nxfs_loc_h  location      = NULL;
 61  nx_buffer_t *buffer       = NULL;
 62  int fd                    = -1;
 63  nxfs_post_result_t result;
 64
 65  /***************************************************************************
 66   * Set the logfile to 'nxfs_example_upload.log'. This file will contain all
 67   * the log statements of the logging macros.
 68   */
 69  _DBGSET(("nxfs_example_upload.log"));
 70
 71  /***************************************************************************
 72   * set the loglevel of the lib to "2".  This is a bit verbose, but lets us
 73   * see the inner workings of the library.
 74   */
 75  nxfs_loglevel(2);
 76
 77#ifdef WIN32
 78  _DBG((dbg, "I'm running on windows."));
 79#endif
 80
 81  _DBG(( dbg, "<main()" ));
 82
 83  /***************************************************************************
 84   * get the command line args, complain and exit if they're missing.
 85   */
 86  if (argc < 6) {
 87    fprintf(stderr, "usage: nxfs_example_upload url username password number file\n");
 88    exit(1);
 89  }
 90  url      = argv[1];
 91  username = argv[2];
 92  password = argv[3];
 93  number   = argv[4];
 94  filename = argv[5];
 95
 96  _DBGS(dbg, url);
 97  _DBGS(dbg, username);
 98  _DBGS(dbg, number);
 99  _DBGS(dbg, filename);
100
101  /***************************************************************************
102   * allocate a handle for the file service.  This handle is used by all the
103   * file service calls.
104   */
105  ret = nxfs_alloc(&nxfs);
106  if (ret)                                        _DBGERR(dbg, ret);
107
108
109  /***************************************************************************
110   * Set the windchill base url and the credentials to be used.
111   */
112  ret = nxfs_set_url(nxfs, url, username, password);
113
114  /***************************************************************************
115   * Allocate a location handle.  This 'points' to the location of a Windchill
116   * WTDocument business object identified by the document's number.  The
117   * file service calls use this handle to identify the location they're
118   * operate on.
119   */
120  ret = nxfs_loc_alloc(&location, "", number);
121  if (ret)                                        _DBGERRX(dbg, "nxfs_loc_alloc()", ret);
122
123  /***************************************************************************
124   * Now activate this location.  This builds internal data structures which
125   * are used across calls.
126   */
127  ret = nxfs_set_location(nxfs, location);
128  if (ret)                                        _DBGERRX(dbg, "nxfs_set_location()", ret);
129
130  /***************************************************************************
131   * The next few calls just fetch the file's content into a memory buffer.
132   */
133  ret = nx_buf_alloc(&buffer);
134  if (ret)                                        _DBGERRX(dbg, "nx_buf_alloc()", ret);
135
136  fd = open(filename, OPEN_FLAGS);
137
138  ret = nx_buf_fromfile(buffer, fd);
139  if (ret)                                        _DBGERRX(dbg, "nx_buf_fromfile()", ret);
140
141  /***************************************************************************
142   * Now we have the file content and we'll upload the file.
143   */
144  ret = nxfs_file_post(nxfs, location, filename, buffer,
145      1,            // iterate
146      "text_nxfs",  // checkin message
147      &result);
148  if (ret)                   if (ret)          _DBGERRX(dbg, "nxfs_set_url()", ret);
149
150  /***************************************************************************
151   * Log and print the result.
152   */
153  _DBGB(dbg, result.success);
154  _DBGS(dbg, result.oid);
155  _DBGS(dbg, result.url);
156
157  printf("success: %s\n", result.success?"YES":"NO");
158  printf("oid: %s\n", result.oid);
159  printf("url: %s\n", result.url);
160
161  ret = NX_OK;
162DONE:
163  if (nxfs)     nxfs_free(&nxfs);
164  if (fd>0)     close(fd);
165  if (buffer)   nx_buf_free(&buffer);
166  if (location) nxfs_loc_free(&location);
167
168  _DBGDRETURN( dbg, ret );
169}
170
171/* vim: set ts=2 sw=2 expandtab si cin ai tw=78: */

Test Program: test_nxfs.c

Purpose

Test every feature of the nexiles|gateway C-API available.

Building

The provided make file contains a test target, which will build the program.

Usage

Just start the executable. Optionally, you can provide a log file name:

$ test_nxfs test.log

If no log file name is given, the log is written to stdout.

Source Code

  1/****************************************************************************\
  2 * test_nxfs.c
  3 *
  4 * (c) 2012 nexiles GmbH
  5 *
  6 \****************************************************************************/
  7
  8#include <stdio.h>
  9#include <stdlib.h>
 10#include <string.h>
 11#include <assert.h>
 12#include <fcntl.h>
 13#include <sys/stat.h>
 14#ifndef WIN32
 15# include <unistd.h>
 16#endif
 17
 18#include "nxdebug.h"
 19#include "nxerror.h"
 20#include "nxbase64.h"
 21#include "nxbuffer.h"
 22#include "nxfileservice.h"
 23
 24/*****************************************************************************/
 25/* Defines */
 26
 27/*****************************************************************************/
 28/* Typen */
 29
 30/*****************************************************************************/
 31/* Programmglobale Variable */
 32
 33/*****************************************************************************/
 34/* Modulglobale Variable */
 35
 36/* Debug-Flag: Debug-Ausgaben aktiv/inaktiv (1/0) */
 37static int dbg = 1;
 38
 39/*****************************************************************************/
 40/* PROTOTYPEN */
 41
 42
 43/*****************************************************************************
 44 * test_buffer()
 45 */
 46int test_buffer()
 47{
 48  int    ret           = NX_OK;
 49  nxfs_loc_h  location = NULL;
 50  nx_buffer_t *buffer  = NULL;
 51  struct stat st;
 52  struct stat st1;
 53  int fd, fd2;
 54
 55  _DBG(( dbg, "<test_buffer( )" ));
 56
 57  ret = nx_buf_alloc(&buffer);
 58  if (ret)                                        _DBGERRX(dbg, "nx_buf_alloc()", ret);
 59
 60  fd = open("nexiles.pdf", OPEN_FLAGS);
 61
 62  _DBG((dbg, "OPEN_FLAGS   = 0x%04x", OPEN_FLAGS));
 63  _DBG((dbg, "CREATE_FLAGS = 0x%04x", CREATE_FLAGS));
 64
 65#ifdef WIN32
 66  assert(OPEN_FLAGS | _O_BINARY);
 67  assert(CREATE_FLAGS | _O_BINARY);
 68  assert(sizeof(size_t) == 4);
 69#endif
 70
 71  stat("nexiles.pdf", &st);
 72  _DBGD(dbg, fd);
 73  _DBGD(dbg, st.st_size);
 74
 75  fstat(fd, &st);
 76  _DBGD(dbg, st.st_size);
 77
 78  ret = nx_buf_fromfile(buffer, fd);
 79  if (ret)                                        _DBGERRX(dbg, "nx_buf_fromfile()", ret);
 80
 81  close(fd); fd = -1;
 82
 83  assert(buffer->size == st.st_size);
 84
 85  fd2 = open("nexiles1.pdf", CREATE_FLAGS);
 86
 87  ret = nx_buf_tofile(buffer, fd2);
 88  if (ret)                                        _DBGERRX(dbg, "nx_buf_tofile()", ret);
 89
 90  close(fd2); fd2 = -1;
 91
 92  stat("nexiles1.pdf", &st1);
 93  _DBGD(dbg, st.st_size);
 94  _DBGD(dbg, st1.st_size);
 95
 96  assert(buffer->size == st1.st_size);
 97  assert(st.st_size == st1.st_size);
 98
 99  ret = 0;
100DONE:
101  if (fd>=0)    close(fd);
102  if (fd2>=0)    close(fd2);
103  if (buffer)   nx_buf_free(&buffer);
104  _DBGDRETURN( dbg, ret );
105}
106
107/*****************************************************************************
108 * test_base64()
109 */
110int test_base64()
111{
112  int    ret  =  NX_OK;
113
114  nx_buffer_t *in = NULL;
115  nx_buffer_t *out = NULL;
116  nx_buffer_t *back = NULL;
117
118  int fd, fd2, fd3;
119
120  static char *test = "the quick brown fox jumps over the lazy dog";
121
122  _DBG(( dbg, "<test_base64()" ));
123
124  nx_buf_alloc(&in);
125  strcat(in->buffer, test);
126  in->size = strlen(test) + 1;
127
128  nx_buf_alloc(&out);
129  nx_buf_alloc(&back);
130
131  _DBGS(dbg, in->buffer);
132  _DBGS(dbg, out->buffer);
133
134  ret = nxb64_encode(in, out);
135  if (ret)                                   _DBGERRX(dbg, "nxb64_encode", ret);
136
137  _DBGS(dbg, in->buffer);
138  _DBGS(dbg, out->buffer);
139
140  ret = nxb64_decode(out, back);
141  if (ret)                                   _DBGERRX(dbg, "nxb64_encode", ret);
142
143  _DBGS(dbg, out->buffer);
144  _DBGS(dbg, back->buffer);
145
146  assert(strcmp(back->buffer, test) == 0);
147
148  /* reset buffers */
149  out->size = back->size = in->size = 0;
150
151  fd = open("nexiles.pdf", OPEN_FLAGS);
152  nx_buf_fromfile(in, fd);
153  close(fd);
154  nxb64_encode(in, out);
155  nxb64_decode(out, back);
156
157  fd3 = open("nexiles2.enc", CREATE_FLAGS, 0644);
158  nx_buf_tofile(out, fd3);
159  close(fd3);
160
161  fd2 = open("nexiles2.pdf", CREATE_FLAGS, 0644);
162  nx_buf_tofile(back, fd2);
163  close(fd2);
164
165  assert(memcmp(in->buffer, back->buffer, in->size) == 0);
166  assert(in->size == back->size);
167
168  ret = 0;
169DONE:
170  if (in)       nx_buf_free(&in);
171  if (out)      nx_buf_free(&out);
172  if (back)     nx_buf_free(&back);
173  _DBGDRETURN( dbg, ret );
174}
175
176/*****************************************************************************
177 * test_set_url()
178 */
179int test_set_url(nxfs_h nxfs)
180{
181  int    ret  =  NX_OK;
182
183  const char *url      = getenv("NXFS_URL");
184  const char *username = getenv("NXFS_USERNAME");
185  const char *password = getenv("NXFS_PASSWORD");
186
187  _DBG(( dbg, "<test_set_url( )" ));
188
189  if ( !nxfs )      _DBGERRX(dbg, "nxfs", -NX_EINVAL);
190
191  ret = nxfs_set_url(nxfs, url, username, password);
192  if (ret)          _DBGERRX(dbg, "nxfs_set_url()", ret);
193
194  assert(nxfs->pBaseUrl);
195  assert(nxfs->pUsername);
196  assert(nxfs->pPassword);
197
198DONE:
199  _DBGDRETURN( dbg, ret );
200}
201
202/*****************************************************************************
203 * test_location()
204 */
205int test_location(nxfs_h nxfs)
206{
207  int    ret  =  NX_OK;
208
209  nxfs_loc_h   location = NULL;
210
211  _DBG(( dbg, "<test_location( )" ));
212
213  if ( !nxfs )      _DBGERR( dbg, -NX_EINVAL );
214
215  ret = nxfs_loc_alloc(&location, "TestProduct", "NEXILES.KNOW.APP");
216  if (ret)          _DBGERRX(dbg, "nxfs_loc_alloc", ret);
217
218  ret = nxfs_loc_free(&location);
219  if (ret)          _DBGERRX(dbg, "nxfs_loc_alloc", ret);
220
221  ret = 0;
222DONE:
223  _DBGDRETURN( dbg, ret );
224}
225
226/*****************************************************************************
227 * test_list_files()
228 */
229int test_list_files(nxfs_h nxfs)
230{
231  int    ret  =  NX_OK;
232  nxfs_loc_h  location      = NULL;
233  nxfs_fileinfo_t *pInfo = NULL;
234
235  _DBG(( dbg, "<test_list_files( )" ));
236
237  if ( !nxfs )          _DBGERRX( dbg, "nxfs", -NX_EINVAL );
238
239  ret = nxfs_loc_alloc(&location, "", "0000000663");
240  if (ret)                                        _DBGERRX(dbg, "nxfs_loc_alloc()", ret);
241
242  ret = nxfs_list_files(nxfs, location, &pInfo);
243  if (ret)                                        _DBGERRX(dbg, "nxfs_list_files()", ret);
244  while (pInfo) {
245    _DBGS(dbg, pInfo->pFilename);
246    _DBGS(dbg, pInfo->pMimetype);
247    _DBGS(dbg, pInfo->pRole);
248    _DBGD(dbg, pInfo->size);
249
250    ret = nxfs_list_files_next(nxfs, location, &pInfo);
251    if (ret)                                      _DBGERRX(dbg, "nxfs_list_files_next()", ret);
252  }
253
254  ret = nxfs_loc_free(&location);
255  if (ret)                                        _DBGERRX(dbg, "nxfs_loc_free()", ret);
256  assert(location == NULL);
257
258  ret = 0;
259DONE:
260  _DBGDRETURN( dbg, ret );
261}
262
263/*****************************************************************************
264 * test_get_files()
265 */
266int test_get_files(nxfs_h nxfs)
267{
268  int    ret  =  NX_OK;
269  nxfs_loc_h  location      = NULL;
270  nx_buffer_t *buffer = NULL;
271  int fd;
272
273  _DBG(( dbg, "<test_get_files( )" ));
274
275  if ( !nxfs )          _DBGERRX( dbg, "nxfs", -NX_EINVAL );
276
277  ret = nxfs_loc_alloc(&location, "", "0000000663");
278  if (ret)                                        _DBGERRX(dbg, "nxfs_loc_alloc()", ret);
279
280  ret = nxfs_set_location(nxfs, location);
281  if (ret)                                        _DBGERRX(dbg, "nxfs_set_location()", ret);
282
283  ret = nx_buf_alloc(&buffer);
284  if (ret)                                        _DBGERRX(dbg, "nx_buf_alloc()", ret);
285
286  ret = nxfs_file_get(nxfs, location, "latest-xkcd.png", buffer);
287  if (ret)                                        _DBGERRX(dbg, "nxfs_file_get()", ret);
288
289  fd = open("latest-xkcd-downloaded.png", CREATE_FLAGS, 0644);
290  ret = nx_buf_tofile(buffer, fd);
291  if (ret)                                        _DBGERRX(dbg, "nx_buf_tofile()", ret);
292
293  ret = 0;
294DONE:
295  if (fd>=0)    close(fd);
296  if (buffer)   nx_buf_free(&buffer);
297  if (location) nxfs_loc_free(&location);
298  _DBGDRETURN( dbg, ret );
299}
300
301/*****************************************************************************
302 * test_post_file()
303 */
304int test_post_file(nxfs_h nxfs)
305{
306  int    ret           = NX_OK;
307  nxfs_loc_h  location = NULL;
308  nx_buffer_t *buffer  = NULL;
309  nxfs_post_result_t result;
310  int fd;
311
312  _DBG(( dbg, "<test_post_file( )" ));
313
314  if ( !nxfs )          _DBGERRX( dbg, "nxfs", -NX_EINVAL );
315
316  ret = nxfs_loc_alloc(&location, "", "0000000663");
317  if (ret)                                        _DBGERRX(dbg, "nxfs_loc_alloc()", ret);
318
319  ret = nxfs_set_location(nxfs, location);
320  if (ret)                                        _DBGERRX(dbg, "nxfs_set_location()", ret);
321
322  ret = nx_buf_alloc(&buffer);
323  if (ret)                                        _DBGERRX(dbg, "nx_buf_alloc()", ret);
324
325  fd = open("latest-xkcd.png", OPEN_FLAGS);
326
327  ret = nx_buf_fromfile(buffer, fd);
328  if (ret)                                        _DBGERRX(dbg, "nx_buf_fromfile()", ret);
329
330  _DBGD(dbg, buffer->size);
331
332  ret = nxfs_file_post(nxfs, location, "latest-xkcd.png", buffer,
333      0,            // iterate
334      "text_nxfs",  // checkin message
335      &result);
336  if (ret)                                        _DBGERRX(dbg, "nxfs_file_post()", ret);
337
338  _DBGD(dbg, buffer->size);
339
340  ret = 0;
341DONE:
342  if (fd>=0)    close(fd);
343  if (buffer)   nx_buf_free(&buffer);
344  if (location) nxfs_loc_free(&location);
345  _DBGDRETURN( dbg, ret );
346}
347
348
349/*****************************************************************************
350 * main()
351 */
352int main(int argc, const char *argv[])
353{
354  int         ret           =  NX_OK;
355  nxfs_t      *pfs          = NULL;
356
357  if (dbg) {
358    if ( argc>1)
359      nxfs_logfile(argv[1]);
360
361    if (argc>2)
362      nxfs_loglevel(atoi(argv[2]));
363    else {
364      nxfs_loglevel(1);
365    }
366  }
367
368#ifdef WIN32
369  _DBG((dbg, "I'm running on windows."));
370#endif
371
372  _DBG(( dbg, "<test_nxfs( )" ));
373
374
375  ret = test_buffer();
376  if (ret)                                        _DBGERRX(dbg, "test_buffer()", ret);
377
378  ret = test_base64();
379  if (ret)                                        _DBGERRX(dbg, "test_base64()", ret);
380
381  ret = nxfs_alloc(&pfs);
382  if (ret)                                        _DBGERR(dbg, ret);
383
384  _DBGP(dbg, pfs);
385
386  assert(pfs->version == 1);
387  _DBGD(dbg, pfs->version);
388
389  ret = test_set_url(pfs);
390  if (ret)                                        _DBGERRX(dbg, "test_location()", ret);
391
392  ret = test_location(pfs);
393  if (ret)                                        _DBGERRX(dbg, "test_location()", ret);
394
395  ret = test_list_files(pfs);
396  if (ret)                                        _DBGERRX(dbg, "test_list_files()", ret);
397
398  ret = test_get_files(pfs);
399  if (ret)                                        _DBGERRX(dbg, "test_get_files()", ret);
400
401  ret = test_post_file(pfs);
402  if (ret)                                        _DBGERRX(dbg, "test_post_file()", ret);
403
404  ret = nxfs_free(&pfs);
405  if (ret)                                        _DBGERR(dbg, ret);
406  assert(pfs == NULL);
407
408  ret = NX_OK;
409DONE:
410  if (pfs) nxfs_free(&pfs);
411
412  _DBGDRETURN( dbg, ret );
413}
414
415/* vim: set ts=2 sw=2 expandtab: */