File: test_sanitize_overwrite_reserved.c

package info (click to toggle)
libiscsi 1.20.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,080 kB
  • sloc: ansic: 40,928; xml: 749; sh: 689; makefile: 352
file content (89 lines) | stat: -rw-r--r-- 2,813 bytes parent folder | download
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
/* 
   Copyright (C) 2013 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <CUnit/CUnit.h>

#include "iscsi.h"
#include "iscsi-private.h"
#include "scsi-lowlevel.h"
#include "iscsi-test-cu.h"

static int change_num;

static int my_iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu)
{
        switch (change_num) {
        case 1:
                /* Set reserved bit 0x40 in byte 1 of the CDB */
                pdu->outdata.data[33] |= 0x40;
                break;
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
                /* Set reserved byte in the CDB */
                pdu->outdata.data[32 + change_num] = change_num;
                break;
        }

        change_num = 0;
        return orig_queue_pdu(iscsi, pdu);
}

void test_sanitize_overwrite_reserved(void)
{
        int i;
        struct iscsi_data data;

        data.size = block_size + 4;
        data.data = alloca(data.size);
        memset(&data.data[4], 0xaa, block_size);

        data.data[0] = 0x01;
        data.data[1] = 0x00;
        data.data[2] = block_size >> 8;
        data.data[3] = block_size & 0xff;

        logging(LOG_VERBOSE, LOG_BLANK_LINE);
        logging(LOG_VERBOSE, "Test SANITIZE OVERWRITE Reserved bits/bytes");

        CHECK_FOR_SANITIZE;
        CHECK_FOR_DATALOSS;
        CHECK_FOR_ISCSI(sd);

        /* override transport queue_pdu callback for PDU manipulation */
        sd->iscsi_ctx->drv->queue_pdu = my_iscsi_queue_pdu;

        logging(LOG_VERBOSE, "Send SANITIZE command with the reserved "
                "bit in byte 1 set to 1");
        change_num = 1;
        SANITIZE(sd, 0, 0, SCSI_SANITIZE_OVERWRITE, data.size, &data,
                 EXPECT_INVALID_FIELD_IN_CDB);

        for (i = 2; i < 7; i++) {
                logging(LOG_VERBOSE, "Send SANITIZE command with the reserved "
                        "byte %d set to non-zero", i);
                change_num = i;

                SANITIZE(sd, 0, 0, SCSI_SANITIZE_OVERWRITE, data.size, &data,
                         EXPECT_INVALID_FIELD_IN_CDB);
        }
}