summaryrefslogtreecommitdiffstats
path: root/security/nss/tests/libpkix/common/libpkix_init.sh
blob: 01eb070e19408318dd3750385d54b76ef8ec0aa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/bin/sh
# 
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# libpkix_init.sh
#

### when the script is exiting, handle it in the Cleanup routine...the result
### value will get set to 0 if all the tests completed successfully, so we can
### use that value in the handler

trap 'Cleanup' EXIT

result=1
checkmem=0
arenas=0
quiet=0

doNIST=1
doNIST_PDTest=0
doPD=0
doTop=0
doModule=0
doPki=0
doOCSP=0
doOCSPTest=0

combinedErrors=0
totalErrors=0
prematureTermination=0
errors=0

if [ -z "${INIT_SOURCED}" ] ; then
    libpkixCommondir=`pwd`
    cd ../../common
    . ./init.sh > /dev/null
    cd ${libpkixCommondir}
fi

DIST_BIN=${DIST}/${OBJDIR}/bin

### setup some defaults
WD=`pwd`
prog=`basename $0`
testOut=${HOSTDIR}/${prog}.$$
testOutMem=${HOSTDIR}/${prog}_mem.$$

####################
# cleanup from tests
####################
Cleanup()
{
    if [ ${testOut} != "" ]; then
        rm -f ${testOut}
    fi

    if [ ${testOutMem} != "" ]; then
        rm -f ${testOutMem}
    fi

    if [ -d ../../nist_pkits/certs ]; then
        rm -f ../../nist_pkits/certs
    fi

    if [ ${doTop} -eq 1 ]; then
        for i in ${linkMStoreNistFiles}; do
            if [ -f ${HOSTDIR}/rev_data/multiple_certstores/$i ]; then
                rm -f ${HOSTDIR}/rev_data/multiple_certstores/$i
            fi
        done
        if [ -d ${HOSTDIR}/rev_data/multiple_certstores ]; then
            rm -fr ${HOSTDIR}/rev_data/multiple_certstores
        fi
    fi

    if [ ${doModule} -eq 1 ]; then
        for i in ${linkModuleNistFiles}; do
            if [ -f ${HOSTDIR}/rev_data/local/$i ]; then
                rm -f ${HOSTDIR}/rev_data/local/$i
            fi
        done
        for i in ${localCRLFiles}; do
            if [ -f ${HOSTDIR}/rev_data/local/$i ]; then
                rm -f ${HOSTDIR}/rev_data/local/$i
            fi
        done
    fi

    if [ ${doPki} -eq 1 ]; then
        for i in ${linkPkiNistFiles}; do
            if [ -f ${HOSTDIR}/rev_data/local/$i ]; then
                rm -f ${HOSTDIR}/rev_data/local/$i
            fi
        done
    fi

    return ${result}
}

### ParseArgs
ParseArgs() # args
{
    while [ $# -gt 0 ]; do
        if [ $1 = "-checkmem" ]; then
            checkmem=1
        elif [ $1 = "-quiet" ]; then
            quiet=1
        elif [ $1 = "-arenas" ]; then
            arenas=1
        fi
        shift
    done
}

Display() # string
{
    if [ ${quiet} -eq 0 ]; then
        echo "$1"
    fi
}

testHeadingEcho()
{
    echo "*******************************************************************************"
    echo "START OF TESTS FOR ${testunit}${memText}"
    echo "*******************************************************************************"
    echo ""
}

testEndingEcho()
{
    if [ ${totalErrors} -eq 0 ]; then
        echo ""
        echo "************************************************************"
        echo "END OF TESTS FOR ${testunit}: ALL TESTS COMPLETED SUCCESSFULLY"
        echo "************************************************************"
        echo ""
        return 0
    fi

    if [ ${totalErrors} -eq 1 ]; then
        plural=""
    else
        plural="S"
    fi

    echo ""
    echo "************************************************************"
    echo "END OF TESTS FOR ${testunit}: ${totalErrors} TEST${plural} FAILED"
    echo "************************************************************"
    echo ""
    return ${totalErrors}
}

###########
# RunTests
###########
RunTests()
{
    errors=0
    memErrors=0
    prematureErrors=0

    failedpgms=""
    failedmempgms=""
    failedprematurepgms=""
    memText=""
    arenaCmd=""

    if [ ${checkmem} -eq 1 ]; then
            memText="   (Memory Checking Enabled)"
    fi

    if [ ${arenas} -eq 1 ]; then
            arenaCmd="-arenas"
    fi

    #
    # Announce start of tests
    #
    Display "*******************************************************************************"
    Display "START OF TESTS FOR PKIX ${testunit} ${memText}"
    Display "*******************************************************************************"
    Display ""

    # run each test specified by the input redirection below

    while read testPgm args; do

        shortTestPurpose=`echo $args | awk '{print $1 " " $2 " "}'`
        fullTestPurpose=${args}
        if [ ${doTop} -eq 1 -o ${doModule} -eq 1 -o ${doPki} -eq 1 ]; then
            testPurpose=${shortTestPurpose}
        else
            testPurpose=${fullTestPurpose}
        fi

        # If we want shorter command printout for NIST tests, delete next line
        testPurpose=${fullTestPurpose}

        # Skip OCSP tests if OCSP is not defined in the environment
        if [ ${doOCSPTest} -eq 0 ]; then
            hasOCSP=`echo ${args} | grep OCSP-Test`
            if [ ! -z "${hasOCSP}" ]; then
                Display "SKIPPING ${testPgm} ${testPurpose}"
	        continue
	    fi
        fi

        if [ ${doNIST} -eq 0 ]; then
            hasNIST=`echo ${args} | grep NIST-Test`
            if [ ! -z "${hasNIST}" ]; then
                Display "SKIPPING ${testPgm} ${testPurpose}"
	        continue
	    fi
        fi

        # This "if" is not reached when doNIST is not set. The assumption
        # is that NIST tests are basic, NIST Path Discovery tests are
        # additional
        if [ ${doNIST_PDTest} -eq 0 ]; then
            hasNIST=`echo ${args} | grep NIST-PDTest`
            if [ ! -z "${hasNIST}" ]; then
                Display "SKIPPING ${testPgm} ${testPurpose}"
	        continue
	    fi
        fi

        Display "RUNNING ${testPgm} ${arenaCmd} ${testPurpose}"

        numtests=`expr ${numtests} + 1`

        if [ ${checkmem} -eq 1 ]; then
            dbx -C -c "runargs ${arenaCmd} ${args};check -all;run;exit" ${DIST_BIN}/${testPgm} > ${testOut} 2>&1
        else
            ${DIST_BIN}/${testPgm} ${arenaCmd} ${args} > ${testOut} 2>&1
        fi

        # Examine output file to see if test failed and keep track of number
        # of failures and names of failed tests. This assumes that the test
        # uses our utility library for displaying information

        cat ${testOut} | tail -2 | grep "COMPLETED SUCCESSFULLY" >/dev/null 2>&1
        
        if [ $? -ne 0 ]; then
            testFail=1
            errors=`expr ${errors} + 1`
            failedpgms="${failedpgms}\n${testPgm} ${testPurpose} "
#            cat ${testOut}
        else
            testFail=0
            passed=`expr ${passed} + 1`
        fi
        cat ${testOut}
        html_msg ${testFail} 0 "${testPgm} ${arenaCmd} ${shortTestPurpose}"

        if [ ${checkmem} -eq 1 ]; then
            grep "(actual leaks:" ${testOut} > ${testOutMem} 2>&1
            if [ $? -ne 0 ]; then
                prematureErrors=`expr ${prematureErrors} + 1`
                failedprematurepgms="${failedprematurepgms}${testPgm} "
                Display "...program terminated prematurely (unable to check for memory leak errors) ..."
            else
                #grep "(actual leaks:         0" ${testOut} > /dev/null 2>&1
                # special consideration for memory leak in NSS_NoDB_Init
                grep  "(actual leaks:         1  total size:       4 bytes)" ${testOut} > /dev/null 2>&1
                if [ $? -ne 0 ]; then
                    memErrors=`expr ${memErrors} + 1`
                    failedmempgms="${failedmempgms}${testPgm} "
                    cat ${testOutMem}
                fi
            fi
        fi

    done

    if [ ${errors} -eq 0 ]; then
        if [ ${memErrors} -eq 0 ]; then
            Display ""
            Display "************************************************************"
            Display "END OF TESTS FOR PKIX ${testunit}: ALL TESTS COMPLETED SUCCESSFULLY"
            Display "************************************************************"
            Display ""
            return 0
        fi
    fi

    if [ ${errors} -eq 1 ]; then
        plural=""
    else
        plural="S"
    fi

    Display ""
    Display "*******************************************************************************"
    Display "END OF TESTS FOR PKIX ${testunit}: ${errors} UNIT TEST${plural} FAILED: ${failedpgms}"
    Display ""
    if [ ${checkmem} -eq 1 ]; then
        if [ ${memErrors} -eq 1 ]; then
            memPlural=""
        else
            memPlural="S"
        fi
        Display "                          ${memErrors} MEMORY LEAK TEST${memPlural} FAILED: ${failedmempgms}"
        
        if [ ${prematureErrors} -ne 0 ]; then
            if [ ${prematureErrors} -eq 1 ]; then
                prematurePlural=""
            else
                prematurePlural="S"
            fi
            Display "                          ${prematureErrors} MEMORY LEAK TEST${prematurePlural} INDETERMINATE: ${failedprematurepgms}"
        fi

    fi
    Display "*******************************************************************************"
    Display ""
    combinedErrors=`expr ${errors} + ${memErrors} + ${prematureErrors}`

    return ${combinedErrors}

}