netcdf-c/ncdap_test/t_auth.c

218 lines
4.8 KiB
C
Raw Normal View History

/*! \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.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
Improve UTF8 Support On Windows re: Issue https://github.com/Unidata/netcdf-c/issues/2190 The primary purpose of this PR is to improve the utf8 support for windows. This is persuant to a change in Windows that supports utf8 natively (almost). The almost means that it is still utf16 internally and the set of characters representable by utf8 is larger than those representable by utf16. This leaves open the question in the Issue about handling the Windows 1252 character set. This required the following changes: 1. Test the Windows build and major version in order to see if native utf8 is supported. 2. If native utf8 is supported, Modify dpathmgr.c to call the 8-bit version of the windows fopen() and open() functions. 3. In support of this, programs that use XGetOpt (Windows versions) need to get the command line as utf8 and then parse to arc+argv as utf8. This requires using a homegrown command line parser named XCommandLineToArgvA. 4. Add a utility program called "acpget" that prints out the current Windows code page and locale. Additionally, some technical debt was cleaned up as follows: 1. Unify all the places which attempt to read all or a part of a file into the dutil.c#NC_readfile code. 2. Similary unify all the code that creates temp files into dutil.c#NC_mktmp code. 3. Convert almost all remaining calls to fopen() and open() to NCfopen() and NCopen3(). This is to ensure that path management is used consistently. This touches a number of files. 4. extern->EXTERNL as needed to get it to work under Windows.
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"
Primary change: add dap4 support Specific changes: 1. Add dap4 code: libdap4 and dap4_test. Note that until the d4ts server problem is solved, dap4 is turned off. 2. Modify various files to support dap4 flags: configure.ac, Makefile.am, CMakeLists.txt, etc. 3. Add nc_test/test_common.sh. This centralizes the handling of the locations of various things in the build tree: e.g. where is ncgen.exe located. See nc_test/test_common.sh for details. 4. Modify .sh files to use test_common.sh 5. Obsolete separate oc2 by moving it to be part of netcdf-c. This means replacing code with netcdf-c equivalents. 5. Add --with-testserver to configure.ac to allow override of the servers to be used for --enable-dap-remote-tests. 6. There were multiple versions of nctypealignment code. Try to centralize in libdispatch/doffset.c and include/ncoffsets.h 7. Add a unit test for the ncuri code because of its complexity. 8. Move the findserver code out of libdispatch and into a separate, self contained program in ncdap_test and dap4_test. 9. Move the dispatch header files (nc{3,4}dispatch.h) to .../include because they are now shared by modules. 10. Revamp the handling of TOPSRCDIR and TOPBUILDDIR for shell scripts. 11. Make use of MREMAP if available 12. Misc. minor changes e.g. - #include <config.h> -> #include "config.h" - Add some no-install headers to /include - extern -> EXTERNL and vice versa as needed - misc header cleanup - clean up checking for misc. unix vs microsoft functions 13. Change copyright decls in some files to point to LICENSE file. 14. Add notes to RELEASENOTES.md
2017-03-09 08:01:10 +08:00
#include "nctestserver.h"
2014-12-01 11:30:23 +08:00
#undef NOEMBED
#undef NOLOCAL
#undef NOHOME
#define NOREDIR
#define KEEPRC
#define AUTHTESTSERVER "thredds.ucar.edu"
2014-12-01 11:30:23 +08:00
#define RC ".daprc"
#define SPECRC "./daprc"
#define USERPWD "authtester:auth"
2014-12-01 11:30:23 +08:00
#define COOKIEFILE "./cookies"
#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-04-16 11:25:44 +08:00
/* Embedded user:pwd */
static char url1[1024];
2014-12-01 11:30:23 +08:00
/* user:pwd from RC*/
static char url2[1024];
2014-12-01 11:30:23 +08:00
/* Test redirect from different machine*/
#ifndef NOREDIR
2014-12-01 11:30:23 +08:00
static char url3[1024];
#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();
int
main(int argc, char** argv)
{
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-12-01 11:30:23 +08:00
fprintf(stderr,"Testing: Authorization\n");
dfaltsvc = nc_findtestserver("thredds",AUTHTESTSERVER);
if(dfaltsvc == NULL) {
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-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-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);
retval = nc_open(url1, 0, &ncid);
if(retval != NC_NOERR) {
pass = 0;
2014-12-01 11:30:23 +08:00
fprintf(stderr,"*** FAIL: Testing embedded user:pwd\n");
} else {
2014-12-01 11:30:23 +08:00
fprintf(stderr,"*** PASS: Testing embedded user:pwd\n");
retval = nc_close(ncid);
}
2014-12-01 11:30:23 +08:00
fflush(stderr);
}
2014-12-01 11:30:23 +08:00
#endif
2014-12-01 11:30:23 +08:00
#ifndef NOLOCAL
{
/* Test 1: RC in ./ */
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-12-01 11:30:23 +08:00
#ifndef NOHOME
{
/* Test 1: RC in HOME */
#if defined(_WIN32) && !defined(__MINGW32__)
home = getenv("HOME");
#else
home = getenv("USERPROFILE");
#endif
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-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);
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
return !pass;
}
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);
Improve UTF8 Support On Windows re: Issue https://github.com/Unidata/netcdf-c/issues/2190 The primary purpose of this PR is to improve the utf8 support for windows. This is persuant to a change in Windows that supports utf8 natively (almost). The almost means that it is still utf16 internally and the set of characters representable by utf8 is larger than those representable by utf16. This leaves open the question in the Issue about handling the Windows 1252 character set. This required the following changes: 1. Test the Windows build and major version in order to see if native utf8 is supported. 2. If native utf8 is supported, Modify dpathmgr.c to call the 8-bit version of the windows fopen() and open() functions. 3. In support of this, programs that use XGetOpt (Windows versions) need to get the command line as utf8 and then parse to arc+argv as utf8. This requires using a homegrown command line parser named XCommandLineToArgvA. 4. Add a utility program called "acpget" that prints out the current Windows code page and locale. Additionally, some technical debt was cleaned up as follows: 1. Unify all the places which attempt to read all or a part of a file into the dutil.c#NC_readfile code. 2. Similary unify all the code that creates temp files into dutil.c#NC_mktmp code. 3. Convert almost all remaining calls to fopen() and open() to NCfopen() and NCopen3(). This is to ensure that path management is used consistently. This touches a number of files. 4. extern->EXTERNL as needed to get it to work under Windows.
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);
}
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();
Improve UTF8 Support On Windows re: Issue https://github.com/Unidata/netcdf-c/issues/2190 The primary purpose of this PR is to improve the utf8 support for windows. This is persuant to a change in Windows that supports utf8 natively (almost). The almost means that it is still utf16 internally and the set of characters representable by utf8 is larger than those representable by utf16. This leaves open the question in the Issue about handling the Windows 1252 character set. This required the following changes: 1. Test the Windows build and major version in order to see if native utf8 is supported. 2. If native utf8 is supported, Modify dpathmgr.c to call the 8-bit version of the windows fopen() and open() functions. 3. In support of this, programs that use XGetOpt (Windows versions) need to get the command line as utf8 and then parse to arc+argv as utf8. This requires using a homegrown command line parser named XCommandLineToArgvA. 4. Add a utility program called "acpget" that prints out the current Windows code page and locale. Additionally, some technical debt was cleaned up as follows: 1. Unify all the places which attempt to read all or a part of a file into the dutil.c#NC_readfile code. 2. Similary unify all the code that creates temp files into dutil.c#NC_mktmp code. 3. Convert almost all remaining calls to fopen() and open() to NCfopen() and NCopen3(). This is to ensure that path management is used consistently. This touches a number of files. 4. extern->EXTERNL as needed to get it to work under Windows.
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);
#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
}