1 /*
2    Copyright (C) 2013 Ronnie Sahlberg <ronniesahlberg@gmail.com>
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 
22 #include <CUnit/CUnit.h>
23 
24 #include "iscsi.h"
25 #include "scsi-lowlevel.h"
26 #include "iscsi-support.h"
27 #include "iscsi-test-cu.h"
28 
29 
30 void
test_orwrite_verify(void)31 test_orwrite_verify(void)
32 {
33         int i, ret;
34         unsigned char *buf     = &scratch[0];
35         unsigned char *readbuf = &scratch[256 * block_size];
36 
37 
38         CHECK_FOR_DATALOSS;
39         CHECK_FOR_SBC;
40 
41         logging(LOG_VERBOSE, LOG_BLANK_LINE);
42         logging(LOG_VERBOSE, "Test ORWRITE of 1-256 blocks at the start of the LUN");
43         for (i = 1; i <= 256; i++) {
44                 if (maximum_transfer_length && maximum_transfer_length < i) {
45                         break;
46                 }
47 
48                 logging(LOG_VERBOSE, "Write %d blocks of all-zero", i);
49                 memset(buf, 0, block_size * i);
50                 ret = write10(sd, 0, i * block_size,
51                               block_size, 0, 0, 0, 0, 0, buf,
52                               EXPECT_STATUS_GOOD);
53                 CU_ASSERT_EQUAL(ret, 0);
54 
55                 logging(LOG_VERBOSE, "OrWrite %d blocks with 0xa5", i);
56                 memset(buf, 0xa5, block_size * i);
57                 ORWRITE(sd, 0, i * block_size,
58                         block_size, 0, 0, 0, 0, 0, buf,
59                         EXPECT_STATUS_GOOD);
60 
61                 logging(LOG_VERBOSE, "Read %d blocks back", i);
62                 READ10(sd, NULL, 0, i * block_size,
63                        block_size, 0, 0, 0, 0, 0, readbuf,
64                        EXPECT_STATUS_GOOD);
65 
66                 logging(LOG_VERBOSE, "Verify that the blocks are all 0xa5");
67                 ret = memcmp(buf, readbuf, block_size * i);
68                 CU_ASSERT_EQUAL(ret, 0);
69 
70                 logging(LOG_VERBOSE, "OrWrite %d blocks with 0x5a", i);
71                 memset(buf, 0x5a, block_size * i);
72                 ORWRITE(sd, 0, i * block_size,
73                         block_size, 0, 0, 0, 0, 0, buf,
74                         EXPECT_STATUS_GOOD);
75 
76                 logging(LOG_VERBOSE, "Read %d blocks back", i);
77                 READ10(sd, NULL, 0, i * block_size,
78                        block_size, 0, 0, 0, 0, 0, readbuf,
79                        EXPECT_STATUS_GOOD);
80 
81                 logging(LOG_VERBOSE, "Verify that the blocks are all 0xff");
82                 memset(buf, 0xff, block_size * i);
83                 ret = memcmp(buf, readbuf, block_size * i);
84                 CU_ASSERT_EQUAL(ret, 0);
85         }
86 
87         logging(LOG_VERBOSE, "Test ORWRITE of 1-256 blocks at the end of the LUN");
88         for (i = 1; i <= 256; i++) {
89                 if (maximum_transfer_length && maximum_transfer_length < i) {
90                         break;
91                 }
92 
93                 logging(LOG_VERBOSE, "Write %d blocks of all-zero", i);
94                 memset(buf, 0, block_size * i);
95                 WRITE16(sd, num_blocks - i, i * block_size,
96                         block_size, 0, 0, 0, 0, 0, buf,
97                         EXPECT_STATUS_GOOD);
98 
99                 logging(LOG_VERBOSE, "OrWrite %d blocks with 0xa5", i);
100                 memset(buf, 0xa5, block_size * i);
101                 ORWRITE(sd, num_blocks - i, i * block_size,
102                         block_size, 0, 0, 0, 0, 0, buf,
103                         EXPECT_STATUS_GOOD);
104 
105                 logging(LOG_VERBOSE, "Read %d blocks back", i);
106                 READ16(sd, NULL, num_blocks - i, i * block_size,
107                        block_size, 0, 0, 0, 0, 0, readbuf,
108                        EXPECT_STATUS_GOOD);
109 
110                 logging(LOG_VERBOSE, "Verify that the blocks are all 0xa5");
111                 ret = memcmp(buf, readbuf, block_size * i);
112                 CU_ASSERT_EQUAL(ret, 0);
113 
114                 logging(LOG_VERBOSE, "OrWrite %d blocks with 0x5a", i);
115                 memset(buf, 0x5a, block_size * i);
116                 ORWRITE(sd, num_blocks - i, i * block_size,
117                         block_size, 0, 0, 0, 0, 0, buf,
118                         EXPECT_STATUS_GOOD);
119 
120                 logging(LOG_VERBOSE, "Read %d blocks back", i);
121                 READ16(sd, NULL, num_blocks - i, i * block_size,
122                        block_size, 0, 0, 0, 0, 0, readbuf,
123                        EXPECT_STATUS_GOOD);
124 
125                 logging(LOG_VERBOSE, "Verify that the blocks are all 0xff");
126                 memset(buf, 0xff, block_size * i);
127                 ret = memcmp(buf, readbuf, block_size * i);
128                 CU_ASSERT_EQUAL(ret, 0);
129         }
130 }
131