函数

时间:2017-02-21 10:58:27

标签: c++11 global-variables

我将这些变量定义为任何函数之外的全局变量

//testing parameters (init to all nonfail)
int serverRandom = 0; //nonzero == fail > gotofail for first sha1 methode
SSLBuffer sigpar= 0; //nonzero == fail > gotofail for second sha1 methode
string hashOut = "nonfail"; //"fail" == error condition > gotofail for third sha1 methode

这个变量用在这个函数中:

static OSStatus SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams, uint8_t *signature, uint16_t signatureLen)
{
    OSStatus err;
    cout << "initval:"<< serverRandom<< signedParams<< hashOut<<endl;

    if ((err = SSLHashSHA1::update(&hashCtx, &serverRandom)) != 0)
        cout << "firstfail" <<endl;
        goto fail;
    if ((err = SSLHashSHA1::update(&hashCtx, &signedParams)) != 0)
        cout <<"secondfail"<<endl;
        goto fail;
        //goto fail;
    if ((err = SSLHashSHA1::final(&hashCtx, &hashOut)) != 0)
        cout << "thirdfail" << endl;
        goto fail;

    cout << "nonfail" << endl;

    fail:
        SSLFreeBuffer(&signedHashes);
        SSLFreeBuffer(&hashCtx);
        return err;
}

请注意,全局变量ServerRandom和hashout直接在该函数中访问,而sigpar变量则赋予sslVerify函数的signedParams参数。

现在,我编写了一个单元测试来通过此函数单独测试每个失败的案例

void unittest(){
    //all tests passing
    cout << "-------------nonfail-------------" << endl;
    result = SSLVerifySignedServerKeyExchange(&ctx,isrsa,sigpar,&sig,siglen);


    //first test fail
    cout << "-------------firstfail-------------" << endl;
    serverRandom = 1;
    result = SSLVerifySignedServerKeyExchange(&ctx,isrsa,sigpar,&sig,siglen);
    serverRandom = 0;

    //second test fail
    cout << "-------------secondfail-------------" << endl;
    sigpar= 1;
    result = SSLVerifySignedServerKeyExchange(&ctx,isrsa,sigpar,&sig,siglen);
    sigpar= 0;

    //third test fail
    cout << "-------------thirdfail-------------" << endl;
    hashOut = "fail";
    result = SSLVerifySignedServerKeyExchange(&ctx,isrsa,sigpar,&sig,siglen);
    hashOut = "nonfail";

}

现在它适用于第一个失败案例,例如。输出firstfail线。之后它不再起作用,因为它只输出--------- secondfail --------&amp; ------ thirdfail -------无需输出failchecks(secondfail / thirdfail)。

请注意,sslverify ...()中的initval显示正确的值,但第一次testcause后的update / final函数显示0作为他们收到的相应值的值。

1 个答案:

答案 0 :(得分:0)

好吧,最后很容易解决:

在SSLVerifySignedServerKeyExchange()中的

我添加{}之后忘记在多行if周围添加cout<<所以它总是会在首先是