2018-12-07 06:36:53 +08:00
|
|
|
/*! \file
|
|
|
|
|
|
|
|
Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
|
|
|
|
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
|
|
|
|
2015, 2016, 2017, 2018
|
|
|
|
University Corporation for Atmospheric Research/Unidata.
|
|
|
|
|
|
|
|
See \ref copyright file for more info.
|
|
|
|
|
|
|
|
*/
|
2013-11-16 02:38:54 +08:00
|
|
|
#include "config.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-10-05 05:59:16 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2022-02-09 11:53:30 +08:00
|
|
|
#undef DEBUG
|
2014-12-01 11:30:23 +08:00
|
|
|
|
2014-03-15 04:07:35 +08:00
|
|
|
#include "netcdf.h"
|
2017-03-09 08:01:10 +08:00
|
|
|
#include "nctestserver.h"
|
2013-11-16 02:38:54 +08:00
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
#undef NOEMBED
|
|
|
|
#undef NOLOCAL
|
|
|
|
#undef NOHOME
|
|
|
|
#define NOREDIR
|
|
|
|
|
|
|
|
#define KEEPRC
|
|
|
|
|
2021-05-30 11:30:33 +08:00
|
|
|
#define AUTHTESTSERVER "thredds.ucar.edu"
|
2014-12-01 11:30:23 +08:00
|
|
|
|
2021-05-30 11:30:33 +08:00
|
|
|
#define RC ".daprc"
|
|
|
|
#define SPECRC "./daprc"
|
|
|
|
|
|
|
|
#define USERPWD "authtester:auth"
|
2014-12-01 11:30:23 +08:00
|
|
|
#define COOKIEFILE "./cookies"
|
2014-10-05 05:59:16 +08:00
|
|
|
|
2021-05-30 11:30:33 +08:00
|
|
|
#define URL1 "https://%s@%s/thredds/dodsC/test3/testData.nc"
|
|
|
|
#define URL2 "https://thredds/%s/dodsC/test3/testData.nc"
|
|
|
|
#define URL3 "https://%s@" AUTHTESTSERVER "/thredds/dodsC/test3/testData.nc"
|
2014-10-05 05:59:16 +08:00
|
|
|
|
2014-04-16 11:25:44 +08:00
|
|
|
/* Embedded user:pwd */
|
2014-10-05 05:59:16 +08:00
|
|
|
static char url1[1024];
|
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
/* user:pwd from RC*/
|
2014-10-05 05:59:16 +08:00
|
|
|
static char url2[1024];
|
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
/* Test redirect from different machine*/
|
2021-05-30 11:30:33 +08:00
|
|
|
#ifndef NOREDIR
|
2014-12-01 11:30:23 +08:00
|
|
|
static char url3[1024];
|
2021-05-30 11:30:33 +08:00
|
|
|
#endif
|
2014-04-16 11:25:44 +08:00
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
static int testrc(const char* prefix, const char* url);
|
|
|
|
static void fillrc(const char* path);
|
|
|
|
static void killrc();
|
2013-11-16 02:38:54 +08:00
|
|
|
|
|
|
|
int
|
2014-04-17 04:08:12 +08:00
|
|
|
main(int argc, char** argv)
|
2013-11-16 02:38:54 +08:00
|
|
|
{
|
2014-03-15 04:07:35 +08:00
|
|
|
int ncid,retval,pass;
|
2014-12-01 11:30:23 +08:00
|
|
|
const char* dfaltsvc;
|
|
|
|
const char* home;
|
2014-10-05 05:59:16 +08:00
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
fprintf(stderr,"Testing: Authorization\n");
|
2014-10-05 05:59:16 +08:00
|
|
|
|
2021-05-30 11:30:33 +08:00
|
|
|
dfaltsvc = nc_findtestserver("thredds",AUTHTESTSERVER);
|
|
|
|
if(dfaltsvc == NULL) {
|
2020-01-01 06:42:58 +08:00
|
|
|
fprintf(stderr,"WARNING: Cannot locate test server\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
snprintf(url1,sizeof(url1),URL1,USERPWD,dfaltsvc); /* embedded */
|
|
|
|
snprintf(url2,sizeof(url2),URL2,dfaltsvc); /* using rc file */
|
2014-04-17 04:08:12 +08:00
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"url1: %s\n",url1);
|
|
|
|
fprintf(stderr,"url2: %s\n",url2);
|
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
2014-04-17 04:08:12 +08:00
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
pass = 1; /* assume success */
|
|
|
|
killrc();
|
|
|
|
|
|
|
|
fprintf(stderr,"Testing: Http Basic Authorization\n\n");
|
|
|
|
#ifndef NOEMBED
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Testing: Embedded user:pwd: %s\n",url1);
|
2014-10-05 05:59:16 +08:00
|
|
|
retval = nc_open(url1, 0, &ncid);
|
2014-04-17 04:08:12 +08:00
|
|
|
if(retval != NC_NOERR) {
|
|
|
|
pass = 0;
|
2014-12-01 11:30:23 +08:00
|
|
|
fprintf(stderr,"*** FAIL: Testing embedded user:pwd\n");
|
2014-04-17 04:08:12 +08:00
|
|
|
} else {
|
2014-12-01 11:30:23 +08:00
|
|
|
fprintf(stderr,"*** PASS: Testing embedded user:pwd\n");
|
2014-04-17 04:08:12 +08:00
|
|
|
retval = nc_close(ncid);
|
|
|
|
}
|
2014-12-01 11:30:23 +08:00
|
|
|
fflush(stderr);
|
2014-04-17 04:08:12 +08:00
|
|
|
}
|
2014-12-01 11:30:23 +08:00
|
|
|
#endif
|
2014-04-17 04:08:12 +08:00
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
#ifndef NOLOCAL
|
|
|
|
{
|
|
|
|
/* Test 1: RC in ./ */
|
2021-05-30 11:30:33 +08:00
|
|
|
fprintf(stderr,"Testing: user:pwd in %s/%s\n",".",RC);
|
2014-12-01 11:30:23 +08:00
|
|
|
if(!testrc(".",url2)) {
|
|
|
|
fprintf(stderr,"user:pwd in %s/%s failed\n",".",RC);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-04-17 04:08:12 +08:00
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
#ifndef NOHOME
|
|
|
|
{
|
|
|
|
/* Test 1: RC in HOME */
|
2022-06-15 04:44:23 +08:00
|
|
|
#if defined(_WIN32) && !defined(__MINGW32__)
|
|
|
|
home = getenv("HOME");
|
|
|
|
#else
|
|
|
|
home = getenv("USERPROFILE");
|
|
|
|
#endif
|
2021-05-30 11:30:33 +08:00
|
|
|
fprintf(stderr,"user:pwd in %s/%s\n",home,RC);
|
2014-12-01 11:30:23 +08:00
|
|
|
if(!testrc(home,url2)) {
|
|
|
|
fprintf(stderr,"user:pwd in %s/%s failed\n",home,RC);
|
|
|
|
exit(1);
|
2014-04-17 04:08:12 +08:00
|
|
|
}
|
|
|
|
}
|
2014-12-01 11:30:23 +08:00
|
|
|
#endif
|
2014-07-30 01:04:29 +08:00
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
#ifndef NOREDIR
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Testing: Http Basic Redirect\n\n");
|
|
|
|
snprintf(url3,sizeof(url3),URL3,USERPWD);
|
|
|
|
fprintf(stderr,"Basic redirect: %s\n",url3);
|
2014-10-05 05:59:16 +08:00
|
|
|
retval = nc_open(url3, 0, &ncid);
|
2014-07-30 01:04:29 +08:00
|
|
|
if(retval != NC_NOERR) {
|
2014-12-01 11:30:23 +08:00
|
|
|
fprintf(stderr,"*** XFAIL: Basic redirect\n");
|
2014-07-30 01:04:29 +08:00
|
|
|
} else {
|
2014-12-01 11:30:23 +08:00
|
|
|
fprintf(stderr,"*** PASS: Basic redirect\n");
|
2014-07-30 01:04:29 +08:00
|
|
|
retval = nc_close(ncid);
|
|
|
|
}
|
2014-12-01 11:30:23 +08:00
|
|
|
fflush(stderr);
|
2014-07-30 01:04:29 +08:00
|
|
|
}
|
2014-12-01 11:30:23 +08:00
|
|
|
#endif
|
2014-07-30 01:04:29 +08:00
|
|
|
|
2014-04-17 04:08:12 +08:00
|
|
|
return !pass;
|
2013-11-16 02:38:54 +08:00
|
|
|
}
|
2014-12-01 11:30:23 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
testrc(const char* prefix, const char* url)
|
|
|
|
{
|
|
|
|
int pass = 1;
|
|
|
|
int retval;
|
|
|
|
int ncid;
|
|
|
|
char rcpath[8192];
|
|
|
|
FILE* rc;
|
|
|
|
|
|
|
|
snprintf(rcpath,sizeof(rcpath),"%s/%s",prefix,RC);
|
2022-02-09 11:53:30 +08:00
|
|
|
rc = NCfopen(rcpath,"w");
|
2014-12-01 11:30:23 +08:00
|
|
|
if(rc == NULL) {
|
|
|
|
fprintf(stderr,"Cannot create ./%s\n",RC);
|
|
|
|
exit(1);
|
2018-12-07 06:36:53 +08:00
|
|
|
}
|
2014-12-01 11:30:23 +08:00
|
|
|
fclose(rc);
|
|
|
|
fillrc(rcpath);
|
|
|
|
retval = nc_open(url, 0, &ncid);
|
|
|
|
if(retval != NC_NOERR) {
|
|
|
|
pass = 0;
|
|
|
|
fprintf(stderr,"*** FAIL: Testing: user:pwd in %s\n",rcpath);
|
|
|
|
} else {
|
|
|
|
retval = nc_close(ncid);
|
|
|
|
fprintf(stderr,"*** PASS: Testing: user:pwd in %s\n",rcpath);
|
|
|
|
}
|
|
|
|
fflush(stderr);
|
|
|
|
#ifndef KEEPRC
|
|
|
|
unlink(rcpath); /* delete the file */
|
|
|
|
#endif
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fillrc(const char* path)
|
|
|
|
{
|
|
|
|
FILE* rc;
|
|
|
|
killrc();
|
|
|
|
|
2022-02-09 11:53:30 +08:00
|
|
|
rc = NCfopen(path,"w");
|
2014-12-01 11:30:23 +08:00
|
|
|
if(rc == NULL) {
|
|
|
|
fprintf(stderr,"cannot create rc file: %s\n",path);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(rc,"HTTP.VERBOSE=1\n");
|
|
|
|
#endif
|
|
|
|
fprintf(rc,"HTTP.COOKIEJAR=%s\n",COOKIEFILE);
|
|
|
|
fprintf(rc,"HTTP.VALIDATE=1\n");
|
|
|
|
fprintf(rc,"HTTP.CREDENTIALS.USERPASSWORD=%s\n",USERPWD);
|
|
|
|
fclose(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
killrc()
|
|
|
|
{
|
|
|
|
const char* home;
|
|
|
|
#ifdef KEEPRC
|
|
|
|
fprintf(stderr,"kill: ./%s\n",RC);
|
2021-05-30 11:30:33 +08:00
|
|
|
#else
|
|
|
|
char path[1024];
|
2014-12-01 11:30:23 +08:00
|
|
|
snprintf(path,sizeof(path),"%s/%s",".",RC);
|
|
|
|
unlink(path); /* delete the file */
|
|
|
|
#endif
|
|
|
|
home = getenv("HOME");
|
|
|
|
#ifdef KEEPRC
|
|
|
|
fprintf(stderr,"kill: %s/%s\n",home,RC);
|
|
|
|
#else
|
|
|
|
snprintf(path,sizeof(path),"%s/%s",home,RC);
|
|
|
|
unlink(path);
|
|
|
|
#endif
|
|
|
|
}
|