netcdf-c/ncdap_test/t_auth.c

119 lines
2.7 KiB
C
Raw Normal View History

#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
2014-03-15 04:07:35 +08:00
#include "netcdf.h"
#define USERPWD "tiggeUser:tigge"
#define URL1 "https://tiggeUser:tigge@%s/thredds/dodsC/restrict/testData.nc"
#define URL2 "https://%s/thredds/dodsC/restrict/testData.nc"
2014-04-16 11:25:44 +08:00
/* Embedded user:pwd */
static char url1[1024];
2014-04-16 11:25:44 +08:00
/* user:pwd from .dodsrc*/
static char url2[1024];
2014-07-30 01:04:29 +08:00
/* Test redirect */
static char* url3 =
2014-07-30 01:04:29 +08:00
"http://tiggeUser:tigge@thredds-test.ucar.edu/thredds/dodsC/restrict/testData.nc";
2014-04-16 11:25:44 +08:00
/* .dodsrc file */
static char* CONTENT = "HTTP.CREDENTIALS.USER=tiggeUser\nHTTP.CREDENTIALS.PASSWORD=tigge\n";
#ifdef DEBUG
static void
CHECK(int e, const char* msg)
{
if(e == NC_NOERR) return;
if(msg == NULL) msg = "Error";
printf("%s: %s\n", msg, nc_strerror(e));
exit(1);
}
#endif
int
main(int argc, char** argv)
{
2014-03-15 04:07:35 +08:00
int ncid,retval,pass;
2014-04-16 11:25:44 +08:00
FILE* dodsrc;
char* envv;
envv = getenv("THREDDSTESTSERVER");
if(envv == NULL) {
envv = "remotetest.unidata.ucar.edu";
} else { /* walk past the schema and // */
char* p = strchr(envv,':');
if(p == NULL) {
fprintf(stderr,"URL has no schema: %s\n",url1);
exit(1);
}
envv = p+3;
}
snprintf(url1,sizeof(url1),URL1,envv);
snprintf(url2,sizeof(url2),URL2,envv);
printf("url1: %s\n",url1);
printf("url2: %s\n",url2);
fflush(stdout);
2014-04-16 11:25:44 +08:00
pass = 1; /* assume success */
2014-04-16 11:25:44 +08:00
dodsrc = fopen(".dodsrc","w");
if(dodsrc == NULL) {
fprintf(stderr,"Cannot create .dodsrc\n");
exit(1);
2014-04-16 11:25:44 +08:00
}
fprintf(dodsrc,CONTENT);
2014-04-16 11:25:44 +08:00
fclose(dodsrc);
printf("Testing: Http Basic Authorization\n\n");
if(1) {
printf("Embedded user:pwd: %s\n",url1);
retval = nc_open(url1, 0, &ncid);
if(retval != NC_NOERR) {
pass = 0;
printf("*** FAIL: Embedded user:pwd\n");
} else {
printf("*** PASS: Embedded user:pwd\n");
retval = nc_close(ncid);
}
fflush(stdout);
}
if(1) {
printf(".dodsrc user:pwd: %s\n",url2);
retval = nc_open(url2, 0, &ncid);
if(retval != NC_NOERR) {
pass = 0;
printf("*** FAIL: .dodsrc user:pwd\n");
} else {
retval = nc_close(ncid);
printf("*** PASS: .dodsrc user:pwd\n");
}
fflush(stdout);
}
unlink(".dodsrc"); /* delete the file */
2014-07-30 01:04:29 +08:00
printf("Testing: Http Basic Redirect\n\n");
if(1) {
printf("Basic redirect: %s\n",url3);
retval = nc_open(url3, 0, &ncid);
2014-07-30 01:04:29 +08:00
if(retval != NC_NOERR) {
printf("*** XFAIL: Basic redirect\n");
} else {
printf("*** PASS: Basic redirect\n");
retval = nc_close(ncid);
}
fflush(stdout);
}
return !pass;
}