AcsMountCmdLineTest.cpp 15.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/******************************************************************************
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/

#include "castor/acs/AcsMountCmdLine.hpp"
25
#include "castor/acs/Constants.hpp"
26
27
#include "castor/exception/InvalidArgument.hpp"
#include "castor/exception/MissingOperand.hpp"
28
29

#include <gtest/gtest.h>
30
31
32
33
#include <list>
#include <memory>
#include <sstream>
#include <string.h>
34
35
36
37
38
39

namespace unitTests {

class castor_acs_AcsMountCmdLineTest: public ::testing::Test {
protected:

40
41
42
43
44
45
46
47
48
49
  struct Argcv {
    int argc;
    char **argv;
    Argcv(): argc(0), argv(NULL) {
    }
  };
  typedef std::list<Argcv*> ArgcvList;
  ArgcvList m_argsList;

  /**
50
   * Creates a duplicate string using the new operator.
51
52
53
54
55
56
57
58
59
   */
  char *dupString(const char *str) {
    const size_t len = strlen(str);
    char *duplicate = new char[len+1];
    strncpy(duplicate, str, len);
    duplicate[len] = '\0';
    return duplicate;
  }

60
61
62
63
  virtual void SetUp() {
  }

  virtual void TearDown() {
64
65
66
67
68
69
70
71
72
73
74
    // Allow getopt_long to be called again
    optind = 0;

    for(ArgcvList::const_iterator itor = m_argsList.begin();
      itor != m_argsList.end(); itor++) {
      for(int i=0; i < (*itor)->argc; i++) {
        delete[] (*itor)->argv[i];
      }
      delete[] (*itor)->argv;
      delete *itor;
    }
75
  }
76
}; // class castor_acs_AcsMountCmdLineTest
77
78
79
80
81
82
83
84

TEST_F(castor_acs_AcsMountCmdLineTest, constructor) {
  const castor::acs::AcsMountCmdLine cmdLine;
  ASSERT_EQ(false, cmdLine.debug);
  ASSERT_EQ(false, cmdLine.help);
  ASSERT_EQ((BOOLEAN)FALSE, cmdLine.readOnly);
  ASSERT_EQ(0, cmdLine.timeout);
  ASSERT_EQ(0, cmdLine.queryInterval);
85
86
87
88
  ASSERT_EQ(0, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.acs);
  ASSERT_EQ(0, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.lsm);
  ASSERT_EQ(0, (int)cmdLine.libraryDriveSlot.panel_id.panel);
  ASSERT_EQ(0, (int)cmdLine.libraryDriveSlot.drive);
89
90
91
  ASSERT_EQ('\0', cmdLine.volId.external_label[0]);
}

92
93
94
95
96
97
98
99
100
101
102
103
TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithNoArgs) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 1;
  args->argv = new char *[2];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
104
105
  ASSERT_THROW(AcsMountCmdLine(args->argc, args->argv),
    castor::exception::MissingOperand);
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithOnlyVolId) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 2;
  args->argv = new char *[3];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("VIDVID");
  args->argv[2] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
121
122
  ASSERT_THROW(AcsMountCmdLine(args->argc, args->argv),
    castor::exception::MissingOperand);
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithShortReadOnly) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 4;
  args->argv = new char *[5];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("-r");
  args->argv[2] = dupString("VIDVID");
  args->argv[3] = dupString("111:112:113:114");
  args->argv[4] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
  AcsMountCmdLine cmdLine;
141
  ASSERT_NO_THROW(cmdLine = AcsMountCmdLine(args->argc, args->argv));
142
143
  ASSERT_EQ((BOOLEAN)TRUE, cmdLine.readOnly);
  ASSERT_EQ(std::string("VIDVID"), std::string(cmdLine.volId.external_label));
144
145
146
147
  ASSERT_EQ(111, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.acs);
  ASSERT_EQ(112, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.lsm);
  ASSERT_EQ(113, (int)cmdLine.libraryDriveSlot.panel_id.panel);
  ASSERT_EQ(114, (int)cmdLine.libraryDriveSlot.drive);
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithLongReadOnly) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 4;
  args->argv = new char *[5];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("--readonly");
  args->argv[2] = dupString("VIDVID");
  args->argv[3] = dupString("111:112:113:114");
  args->argv[4] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
  AcsMountCmdLine cmdLine;
166
  ASSERT_NO_THROW(cmdLine = AcsMountCmdLine(args->argc, args->argv));
167
168
  ASSERT_EQ((BOOLEAN)TRUE, cmdLine.readOnly);
  ASSERT_EQ(std::string("VIDVID"), std::string(cmdLine.volId.external_label));
169
170
171
172
  ASSERT_EQ(111, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.acs);
  ASSERT_EQ(112, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.lsm);
  ASSERT_EQ(113, (int)cmdLine.libraryDriveSlot.panel_id.panel);
  ASSERT_EQ(114, (int)cmdLine.libraryDriveSlot.drive);
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithShortHelp) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 2;
  args->argv = new char *[3];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("-h");
  args->argv[2] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
  AcsMountCmdLine cmdLine;
189
  ASSERT_NO_THROW(cmdLine = AcsMountCmdLine(args->argc, args->argv));
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  ASSERT_EQ(true, cmdLine.help);
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithLongHelp) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 2;
  args->argv = new char *[3];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("--help");
  args->argv[2] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
  AcsMountCmdLine cmdLine;
207
  ASSERT_NO_THROW(cmdLine = AcsMountCmdLine(args->argc, args->argv));
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  ASSERT_EQ(true, cmdLine.help);
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithVolIdAndDrive) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 3;
  args->argv = new char *[4];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("VIDVID");
  args->argv[2] = dupString("111:112:113:114");
  args->argv[3] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
  AcsMountCmdLine cmdLine;
226
  ASSERT_NO_THROW(cmdLine = AcsMountCmdLine(args->argc, args->argv));
227
228
  ASSERT_EQ(false, cmdLine.debug);
  ASSERT_EQ(false, cmdLine.help);
229
230
  ASSERT_EQ(ACS_QUERY_INTERVAL, cmdLine.queryInterval);
  ASSERT_EQ(ACS_CMD_TIMEOUT, cmdLine.timeout);
231
  ASSERT_EQ(std::string("VIDVID"), std::string(cmdLine.volId.external_label));
232
233
234
235
  ASSERT_EQ(111, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.acs);
  ASSERT_EQ(112, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.lsm);
  ASSERT_EQ(113, (int)cmdLine.libraryDriveSlot.panel_id.panel);
  ASSERT_EQ(114, (int)cmdLine.libraryDriveSlot.drive);
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithShortDebug) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 4;
  args->argv = new char *[5];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("-d");
  args->argv[2] = dupString("VIDVID");
  args->argv[3] = dupString("111:112:113:114");
  args->argv[4] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
  AcsMountCmdLine cmdLine;
254
  ASSERT_NO_THROW(cmdLine = AcsMountCmdLine(args->argc, args->argv));
255
256
  ASSERT_EQ(true, cmdLine.debug);
  ASSERT_EQ(std::string("VIDVID"), std::string(cmdLine.volId.external_label));
257
258
259
260
  ASSERT_EQ(111, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.acs);
  ASSERT_EQ(112, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.lsm);
  ASSERT_EQ(113, (int)cmdLine.libraryDriveSlot.panel_id.panel);
  ASSERT_EQ(114, (int)cmdLine.libraryDriveSlot.drive);
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithLongDebug) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 4;
  args->argv = new char *[5];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("--debug");
  args->argv[2] = dupString("VIDVID");
  args->argv[3] = dupString("111:112:113:114");
  args->argv[4] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
  AcsMountCmdLine cmdLine;
279
  ASSERT_NO_THROW(cmdLine = AcsMountCmdLine(args->argc, args->argv));
280
281
  ASSERT_EQ(true, cmdLine.debug);
  ASSERT_EQ(std::string("VIDVID"), std::string(cmdLine.volId.external_label));
282
283
284
285
  ASSERT_EQ(111, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.acs);
  ASSERT_EQ(112, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.lsm);
  ASSERT_EQ(113, (int)cmdLine.libraryDriveSlot.panel_id.panel);
  ASSERT_EQ(114, (int)cmdLine.libraryDriveSlot.drive);
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithTooLongVolIdAndDrive) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 3;
  args->argv = new char *[4];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("VIDVID7");
  args->argv[2] = dupString("111:112:113:114");
  args->argv[3] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
302
  ASSERT_THROW(AcsMountCmdLine(args->argc, args->argv),
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
    castor::exception::InvalidArgument);
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithValidVolIdAndInvalidDrive) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 3;
  args->argv = new char *[4];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("VIDVID");
  args->argv[2] = dupString("INVALID_DRIVE");
  args->argv[3] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
320
  ASSERT_THROW(AcsMountCmdLine(args->argc, args->argv),
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
    castor::exception::InvalidArgument);
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithShortTimeout) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 5;
  args->argv = new char *[6];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("-t");
  args->argv[2] = dupString("2");
  args->argv[3] = dupString("VIDVID");
  args->argv[4] = dupString("111:112:113:114");
  args->argv[5] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
  AcsMountCmdLine cmdLine;
341
  ASSERT_NO_THROW(cmdLine = AcsMountCmdLine(args->argc, args->argv));
342
343
  ASSERT_EQ(2, cmdLine.timeout);
  ASSERT_EQ(std::string("VIDVID"), std::string(cmdLine.volId.external_label));
344
345
346
347
  ASSERT_EQ(111, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.acs);
  ASSERT_EQ(112, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.lsm);
  ASSERT_EQ(113, (int)cmdLine.libraryDriveSlot.panel_id.panel);
  ASSERT_EQ(114, (int)cmdLine.libraryDriveSlot.drive);
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithLongTimeout) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 5;
  args->argv = new char *[6];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("--timeout");
  args->argv[2] = dupString("2");
  args->argv[3] = dupString("VIDVID");
  args->argv[4] = dupString("111:112:113:114");
  args->argv[5] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
  AcsMountCmdLine cmdLine;
367
  ASSERT_NO_THROW(cmdLine = AcsMountCmdLine(args->argc, args->argv));
368
369
  ASSERT_EQ(2, cmdLine.timeout);
  ASSERT_EQ(std::string("VIDVID"), std::string(cmdLine.volId.external_label));
370
371
372
373
  ASSERT_EQ(111, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.acs);
  ASSERT_EQ(112, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.lsm);
  ASSERT_EQ(113, (int)cmdLine.libraryDriveSlot.panel_id.panel);
  ASSERT_EQ(114, (int)cmdLine.libraryDriveSlot.drive);
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWith0Timeout) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 5;
  args->argv = new char *[6];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("--timeout");
  args->argv[2] = dupString("0");
  args->argv[3] = dupString("VIDVID");
  args->argv[4] = dupString("111:112:113:114");
  args->argv[5] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
392
  ASSERT_THROW(AcsMountCmdLine(args->argc, args->argv),
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
    castor::exception::InvalidArgument);
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithShortQuery) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 5;
  args->argv = new char *[6];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("-q");
  args->argv[2] = dupString("1");
  args->argv[3] = dupString("VIDVID");
  args->argv[4] = dupString("111:112:113:114");
  args->argv[5] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
  AcsMountCmdLine cmdLine;
413
  ASSERT_NO_THROW(cmdLine = AcsMountCmdLine(args->argc, args->argv));
414
415
  ASSERT_EQ(1, cmdLine.queryInterval);
  ASSERT_EQ(std::string("VIDVID"), std::string(cmdLine.volId.external_label));
416
417
418
419
  ASSERT_EQ(111, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.acs);
  ASSERT_EQ(112, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.lsm);
  ASSERT_EQ(113, (int)cmdLine.libraryDriveSlot.panel_id.panel);
  ASSERT_EQ(114, (int)cmdLine.libraryDriveSlot.drive);
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWithLongQuery) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 5;
  args->argv = new char *[6];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("--query");
  args->argv[2] = dupString("1");
  args->argv[3] = dupString("VIDVID");
  args->argv[4] = dupString("111:112:113:114");
  args->argv[5] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
  AcsMountCmdLine cmdLine;
439
  ASSERT_NO_THROW(cmdLine = AcsMountCmdLine(args->argc, args->argv));
440
441
  ASSERT_EQ(1, cmdLine.queryInterval);
  ASSERT_EQ(std::string("VIDVID"), std::string(cmdLine.volId.external_label));
442
443
444
445
  ASSERT_EQ(111, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.acs);
  ASSERT_EQ(112, (int)cmdLine.libraryDriveSlot.panel_id.lsm_id.lsm);
  ASSERT_EQ(113, (int)cmdLine.libraryDriveSlot.panel_id.panel);
  ASSERT_EQ(114, (int)cmdLine.libraryDriveSlot.drive);
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
}

TEST_F(castor_acs_AcsMountCmdLineTest, parseCmdLineWith0Query) {
  using namespace castor::acs;
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 5;
  args->argv = new char *[6];
  args->argv[0] = dupString("mountacs");
  args->argv[1] = dupString("--query");
  args->argv[2] = dupString("0");
  args->argv[3] = dupString("VIDVID");
  args->argv[4] = dupString("111:112:113:114");
  args->argv[5] = NULL;

  std::istringstream inStream;
  std::ostringstream outStream;
  std::ostringstream errStream;
464
  ASSERT_THROW(AcsMountCmdLine(args->argc, args->argv),
465
466
467
    castor::exception::InvalidArgument);
}

468
} // namespace unitTests