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 */
26 
27 /**
28 * Unit tests for GUID generation
29 */
30 
31 #include <ktst/unit_test.hpp>
32 
33 #include <klib/guid.h>
34 
35 using namespace std;
36 
37 TEST_SUITE(GUIDTestSuite);
38 
TEST_CASE(Make_NullBuf)39 TEST_CASE(Make_NullBuf)
40 {
41     REQUIRE_RC_FAIL ( KGUIDMake( nullptr, 37 ) );
42 }
43 
TEST_CASE(Make_TooShort)44 TEST_CASE(Make_TooShort)
45 {
46     char buf[37];
47     REQUIRE_RC_FAIL ( KGUIDMake( buf, 36 ) );
48 }
49 
fromxdigit(unsigned char ch)50 static uint8_t fromxdigit( unsigned char ch )
51 {
52     if ( ch < '0' )
53         return '?';
54     if ( ch <= '9' )
55         return ch - '0';
56     if ( ch < 'a' )
57         return '?';
58     if ( ch <= 'f' )
59         return ch - 'a' + 10;
60     return '?';
61 }
62 
TEST_CASE(Make_Success)63 TEST_CASE(Make_Success)
64 {
65     char buf[ 37 ];
66     memset( buf, 0, sizeof buf );
67     REQUIRE_RC ( KGUIDMake( buf, sizeof buf ) );
68 
69 //cout<< buf << endl;
70 
71     REQUIRE ( isxdigit ( buf[0] ) );
72     REQUIRE ( isxdigit ( buf[1] ) );
73     REQUIRE ( isxdigit ( buf[2] ) );
74     REQUIRE ( isxdigit ( buf[3] ) );
75     REQUIRE ( isxdigit ( buf[4] ) );
76     REQUIRE ( isxdigit ( buf[5] ) );
77     REQUIRE ( isxdigit ( buf[7] ) );
78     REQUIRE_EQ ( '-', buf[8] );
79 
80     REQUIRE ( isxdigit ( buf[9] ) );
81     REQUIRE ( isxdigit ( buf[10] ) );
82     REQUIRE ( isxdigit ( buf[11] ) );
83     REQUIRE ( isxdigit ( buf[12] ) );
84     REQUIRE_EQ ( '-', buf[13] );
85 
86     REQUIRE_EQ ( '4', buf[14] ); // version
87     REQUIRE ( isxdigit ( buf[15] ) );
88     REQUIRE ( isxdigit ( buf[16] ) );
89     REQUIRE ( isxdigit ( buf[17] ) );
90     REQUIRE_EQ ( '-', buf[18] );
91 
92     // 0b10xx, variant 1 in the 2 msb
93     REQUIRE ( isxdigit ( buf[19] ) );
94     REQUIRE_EQ ( 2, fromxdigit( buf[ 19 ] ) >> 2 );
95 
96     REQUIRE ( isxdigit ( buf[20] ) );
97     REQUIRE ( isxdigit ( buf[21] ) );
98     REQUIRE ( isxdigit ( buf[22] ) );
99     REQUIRE_EQ ( '-', buf[23] );
100 
101     REQUIRE ( isxdigit ( buf[24] ) );
102     REQUIRE ( isxdigit ( buf[25] ) );
103     REQUIRE ( isxdigit ( buf[26] ) );
104     REQUIRE ( isxdigit ( buf[27] ) );
105     REQUIRE ( isxdigit ( buf[28] ) );
106     REQUIRE ( isxdigit ( buf[29] ) );
107     REQUIRE ( isxdigit ( buf[30] ) );
108     REQUIRE ( isxdigit ( buf[31] ) );
109     REQUIRE ( isxdigit ( buf[32] ) );
110     REQUIRE ( isxdigit ( buf[33] ) );
111     REQUIRE ( isxdigit ( buf[34] ) );
112     REQUIRE ( isxdigit ( buf[35] ) );
113     REQUIRE_EQ ( 0, (int) buf[ 36 ] );
114 }
115 
116 //////////////////////////////////////////////////// Main
117 extern "C"
118 {
119 #ifdef WINDOWS
120 #define main wmain
121 #endif
main(int argc,char * argv[])122 int main ( int argc, char *argv [] )
123 {
124     rc_t rc=GUIDTestSuite(argc, argv);
125     return rc;
126 }
127 
128 }
129