1 /*===========================================================================
2 *
3 * PUBLIC DOMAIN NOTICE
4 * National Center for Biotechnology Information
5 *
6 * This software/database is a "United States Government Work" under the
7 * terms of the United States Copyright Act. It was written as part of
8 * the author's official duties as a United States Government employee and
9 * thus cannot be copyrighted. This software/database is freely available
10 * to the public for use. The National Library of Medicine and the U.S.
11 * Government have not placed any restriction on its use or reproduction.
12 *
13 * Although all reasonable efforts have been taken to ensure the accuracy
14 * and reliability of the software and data, the NLM and the U.S.
15 * Government do not and cannot warrant the performance or results that
16 * may be obtained by using this software or data. The NLM and the U.S.
17 * Government disclaim all warranties, express or implied, including
18 * warranties of performance, merchantability or fitness for any particular
19 * purpose.
20 *
21 * Please cite the author in any work or product based on this material.
22 *
23 * =========================================================================== */
24
25 #include <kapp/args.h> /* ArgsMakeAndHandle */
26 #include <kfg/kfg-priv.h> /* KConfigMakeEmpty */
27 #include <kfs/file.h> /* KFileRelease */
28 #include <klib/debug.h> /* KDbgSetString */
29 #include <klib/out.h> /* KOutMsg */
30 #include <kns/http.h> /* KNSManagerMakeHttpFile */
31 #include <kns/manager.h> /* KNSManagerRelease */
32 #include <kns/tls.h> /* KNSManagerRelease */
33 #include <ktst/unit_test.hpp> /* TEST_SUITE_WITH_ARGS_HANDLER */
34
35 #define RELEASE( type, obj ) do { rc_t rc2 = type##Release ( obj ); \
36 if (rc2 != 0 && rc == 0) { rc = rc2; } obj = NULL; } while ( false )
37
38 /*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
39 * Simple two tests:
40 *
41 * Conf ( false ) = false
42 * Conf ( false ) and SetAllowAllCerts ( true ) = true
43 * Conf ( false ) and SetAllowAllCerts ( false ) = false
44 *
45 * Conf ( true ) = true
46 * Conf ( true ) and SetAllowAllCerts ( false ) = false
47 * Conf ( true ) and SetAllowAllCerts ( true ) = true
48 *
49 *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*/
50 const char * Bizdrapuda = "https://storage.googleapis.com/yan-blastdb/2018-09-12-08-33-02/fuse.xml";
51
argsHandler(int argc,char * argv[])52 static rc_t argsHandler ( int argc, char * argv [] )
53 { return ArgsMakeAndHandle ( NULL, argc, argv, 0, NULL, 0 ); }
54
TEST_SUITE_WITH_ARGS_HANDLER(ALLOW_ALL_CERTS,argsHandler)55 TEST_SUITE_WITH_ARGS_HANDLER ( ALLOW_ALL_CERTS, argsHandler )
56
57 TEST_CASE ( Test_ConfDisAllowCerts ) {
58
59 KConfig * kfg = NULL;
60 rc_t rc = KConfigMakeEmpty ( & kfg );
61 REQUIRE_RC ( rc );
62 REQUIRE_RC ( KConfigWriteString ( kfg, "/tls/allow-all-certs", "true" ) );
63
64 KNSManager * mgr = NULL;
65 REQUIRE_RC ( KNSManagerMake ( & mgr ) );
66
67 /* First we shoud fail to read without any setting
68 */
69 KOutMsg ( "##[4] Conf (true ) = true\n" );
70 const KFile * file = NULL;
71
72 #if GOOGLE_FILE_EXISTS
73 REQUIRE_RC ( KNSManagerMakeHttpFile ( mgr, & file, NULL, 0x01010000, Bizdrapuda ) );
74 REQUIRE_NOT_NULL ( file );
75
76 uint64_t size = 0;
77 REQUIRE_RC ( KFileSize ( file, & size ) );
78
79 char buffer [ 64 ];
80 size_t bsize = sizeof ( buffer ) - 1;
81 size_t num_read = 0;
82
83 REQUIRE_RC ( KFileRead ( file, 0, buffer, bsize, & num_read ) );
84 REQUIRE_EQ ( num_read, bsize );
85
86 REQUIRE_RC ( KFileRelease ( file ) );
87 KOutMsg ( "##[4] OK : Conf (true ) = true\n" );
88
89 /* Second, we call KNSManagerSetAllowAllCerts again
90 */
91 KOutMsg ( "##[5] Conf (true ) + SetAllow ( false ) = false\n" );
92 REQUIRE_RC ( KNSManagerSetAllowAllCerts ( mgr, false ) );
93
94 file = NULL;
95 rc = KNSManagerMakeHttpFile ( mgr, & file, NULL, 0x01010000, Bizdrapuda );
96 CHECK_NE ( rc, ( rc_t ) 0 );
97 REQUIRE_NULL ( file );
98
99 rc = 0;
100 REQUIRE_RC ( KFileRelease ( file ) );
101
102 KOutMsg ( "##[5] OK : Conf (true ) + SetAllow ( false ) = false\n" );
103
104 /* Second, we call KNSManagerSetAllowAllCerts
105 */
106 KOutMsg ( "##[6] Conf (true ) + SetAllow ( true ) = true\n" );
107 REQUIRE_RC ( KNSManagerSetAllowAllCerts ( mgr, true ) );
108
109 file = NULL;
110 REQUIRE_RC ( KNSManagerMakeHttpFile ( mgr, & file, NULL, 0x01010000, Bizdrapuda ) );
111 REQUIRE_NOT_NULL ( file );
112
113 size = 0;
114 REQUIRE_RC ( KFileSize ( file, & size ) );
115
116 bsize = sizeof ( buffer ) - 1;
117 num_read = 0;
118
119 REQUIRE_RC ( KFileRead ( file, 0, buffer, bsize, & num_read ) );
120 REQUIRE_EQ ( num_read, bsize );
121 #endif
122
123 REQUIRE_RC ( KFileRelease ( file ) );
124 KOutMsg ( "##[6] OK : Conf (true ) + SetAllow ( true ) = true\n" );
125
126
127 REQUIRE_RC ( KNSManagerRelease ( mgr ) );
128
129 RELEASE ( KConfig, kfg );
130 }
131
132 extern "C" {
133 const char UsageDefaultName[] = "test200for-whole-file";
UsageSummary(const char * progname)134 rc_t CC UsageSummary ( const char * progname) { return 0; }
Usage(const struct Args * args)135 rc_t CC Usage ( const struct Args * args ) { return 0; }
KAppVersion(void)136 ver_t CC KAppVersion ( void ) { return 0; }
137
KMain(int argc,char * argv[])138 rc_t CC KMain ( int argc, char * argv [] ) { if (
139 0 ) assert ( ! KDbgSetString ( "KNS-HTTP" ) );
140 KConfigDisableUserSettings ();
141
142 // turn off certificate validation to download from storage.googleapis.com
143
144 rc_t rc = ALLOW_ALL_CERTS ( argc, argv );
145
146 return rc;
147 }
148 }
149