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
|
/*
* Copyright (c) International Business Machines Corp., 2006
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Author: Artem B. Bityutskiy
*
* Test UBI volume creation and deletion ioctl()s with bad input and in case of
* incorrect usage.
*/
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "libubi.h"
#define PROGRAM_NAME "mkvol_bad"
#include "common.h"
#include "helpers.h"
static libubi_t libubi;
static struct ubi_dev_info dev_info;
const char *node;
/**
* test_mkvol - test that UBI mkvol ioctl rejects bad input parameters.
*
* This function returns %0 if the test passed and %-1 if not.
*/
static int test_mkvol(void)
{
int ret, i;
struct ubi_mkvol_request req;
const char *name = PROGRAM_NAME ":test_mkvol()";
req.alignment = 1;
req.bytes = dev_info.avail_bytes;
req.vol_type = UBI_DYNAMIC_VOLUME;
req.name = name;
/* Bad volume ID */
req.vol_id = -2;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EINVAL, "ubi_mkvol", "vol_id = %d", req.vol_id))
return -1;
req.vol_id = dev_info.max_vol_count;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EINVAL, "ubi_mkvol", "vol_id = %d", req.vol_id))
return -1;
/* Bad alignment */
req.vol_id = 0;
req.alignment = 0;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EINVAL, "ubi_mkvol", "alignment = %d",
req.alignment))
return -1;
req.alignment = -1;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EINVAL, "ubi_mkvol", "alignment = %d",
req.alignment))
return -1;
req.alignment = dev_info.leb_size + 1;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EINVAL, "ubi_mkvol", "alignment = %d",
req.alignment))
return -1;
if (dev_info.min_io_size > 1) {
req.alignment = dev_info.min_io_size + 1;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EINVAL, "ubi_mkvol", "alignment = %d",
req.alignment))
return -1;
}
/* Bad bytes */
req.alignment = 1;
req.bytes = -1;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EINVAL, "ubi_mkvol", "bytes = %lld", req.bytes))
return -1;
req.bytes = 0;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EINVAL, "ubi_mkvol", "bytes = %lld", req.bytes))
return -1;
req.bytes = dev_info.avail_bytes + 1;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, ENOSPC, "ubi_mkvol", "bytes = %lld", req.bytes))
return -1;
req.alignment = dev_info.leb_size - dev_info.min_io_size;
req.bytes = (long long)(dev_info.leb_size - dev_info.leb_size % req.alignment) *
dev_info.avail_lebs + 1;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, ENOSPC, "ubi_mkvol", "bytes = %lld", req.bytes))
return -1;
/* Bad vol_type */
req.alignment = 1;
req.bytes = dev_info.leb_size;
req.vol_type = UBI_DYNAMIC_VOLUME + UBI_STATIC_VOLUME;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EINVAL, "ubi_mkvol", "vol_type = %d",
req.vol_type))
return -1;
req.vol_type = UBI_DYNAMIC_VOLUME;
/* Too long name */
{
char name[UBI_VOL_NAME_MAX + 5];
memset(name, 'x', UBI_VOL_NAME_MAX + 1);
name[UBI_VOL_NAME_MAX + 1] = '\0';
req.name = name;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EINVAL, "ubi_mkvol", "name_len = %d",
UBI_VOL_NAME_MAX + 1))
return -1;
}
/* Try to create 2 volumes with the same ID and name */
req.name = name;
req.vol_id = 0;
if (ubi_mkvol(libubi, node, &req)) {
failed("ubi_mkvol");
return -1;
}
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EEXIST, "ubi_mkvol",
"volume with ID 0 created twice"))
return -1;
req.vol_id = 1;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EEXIST, "ubi_mkvol",
"volume with name \"%s\" created twice", name))
return -1;
if (ubi_rmvol(libubi, node, 0)) {
failed("ubi_rmvol");
return -1;
}
/* Try to use too much space */
req.vol_id = 0;
req.bytes = dev_info.avail_bytes;
if (ubi_mkvol(libubi, node, &req)) {
failed("ubi_mkvol");
return -1;
}
req.bytes = 1;
req.vol_id = 1;
ret = ubi_mkvol(libubi, node, &req);
if (check_failed(ret, EEXIST, "ubi_mkvol",
"created volume of maximum size %lld, but still "
"can create more volumes", dev_info.avail_bytes))
return -1;
if (ubi_rmvol(libubi, node, 0)) {
failed("ubi_rmvol");
return -1;
}
/* Try to create too many volumes */
for (i = 0; i < dev_info.max_vol_count; i++) {
char nm[strlen(name) + 50];
req.vol_id = UBI_VOL_NUM_AUTO;
req.alignment = 1;
req.bytes = 1;
req.vol_type = UBI_STATIC_VOLUME;
sprintf(nm, "%s:%d", name, i);
req.name = nm;
if (ubi_mkvol(libubi, node, &req)) {
/*
* Note, because of gluebi we may be unable to create
* dev_info.max_vol_count devices (MTD restrictions).
*/
if (errno == ENFILE)
break;
failed("ubi_mkvol");
errorm("vol_id %d", i);
goto remove;
}
}
for (i = 0; i < dev_info.max_vol_count + 1; i++)
ubi_rmvol(libubi, node, i);
return 0;
remove:
for (i = 0; i < dev_info.max_vol_count + 1; i++)
ubi_rmvol(libubi, node, i);
return -1;
}
/**
* test_rmvol - test that UBI rmvol ioctl rejects bad input parameters.
*
* This function returns %0 if the test passed and %-1 if not.
*/
static int test_rmvol(void)
{
int ret;
struct ubi_mkvol_request req;
const char *name = PROGRAM_NAME ":test_rmvol()";
/* Bad vol_id */
ret = ubi_rmvol(libubi, node, -1);
if (check_failed(ret, EINVAL, "ubi_rmvol", "vol_id = -1"))
return -1;
ret = ubi_rmvol(libubi, node, dev_info.max_vol_count);
if (check_failed(ret, EINVAL, "ubi_rmvol", "vol_id = %d",
dev_info.max_vol_count))
return -1;
/* Try to remove non-existing volume */
ret = ubi_rmvol(libubi, node, 0);
if (check_failed(ret, ENODEV, "ubi_rmvol",
"removed non-existing volume 0"))
return -1;
/* Try to remove volume twice */
req.vol_id = UBI_VOL_NUM_AUTO;
req.alignment = 1;
req.bytes = dev_info.avail_bytes;
req.vol_type = UBI_DYNAMIC_VOLUME;
req.name = name;
if (ubi_mkvol(libubi, node, &req)) {
failed("ubi_mkvol");
return -1;
}
if (ubi_rmvol(libubi, node, req.vol_id)) {
failed("ubi_rmvol");
return -1;
}
ret = ubi_rmvol(libubi, node, req.vol_id);
if (check_failed(ret, ENODEV, "ubi_rmvol", "volume %d removed twice",
req.vol_id))
return -1;
return 0;
}
int main(int argc, char * const argv[])
{
if (initial_check(argc, argv))
return 1;
node = argv[1];
libubi = libubi_open();
if (libubi == NULL) {
failed("libubi_open");
return 1;
}
if (ubi_get_dev_info(libubi, node, &dev_info)) {
failed("ubi_get_dev_info");
goto close;
}
if (test_mkvol())
goto close;
if (test_rmvol())
goto close;
libubi_close(libubi);
return 0;
close:
libubi_close(libubi);
return 1;
}
|