MountCmdLineTest.cpp 6.41 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
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
/******************************************************************************
 *
 * 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/exception/Exception.hpp"
#include "castor/mediachanger/MountCmdLine.hpp"

#include <gtest/gtest.h>
#include <list>
#include <memory>

namespace unitTests {

class castor_mediachanger_MountCmdLineTest : public ::testing::Test {
protected:

  struct Argcv {
    int argc;
    char **argv;
    Argcv(): argc(0), argv(NULL) {
    }
  };
  typedef std::list<Argcv*> ArgcvList;
  ArgcvList m_argsList;

  /**
   * Creates a duplicate string using the new operator.
   */
  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;
  }

  virtual void SetUp() {
  }

  virtual void TearDown() {
    // 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;
    }
  }
};

TEST_F(castor_mediachanger_MountCmdLineTest, acs) {
  using namespace castor::mediachanger;

  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 3;
  args->argv = new char *[4];
  args->argv[0] = dupString("castor-tape-mediachanger-mount");
  args->argv[1] = dupString("vid");
  args->argv[2] = dupString("acs1,2,3,4");
  args->argv[3] = NULL;

  std::unique_ptr<MountCmdLine> cmdLine;
  ASSERT_NO_THROW(cmdLine.reset(new MountCmdLine(args->argc, args->argv)));

Steven Murray's avatar
Steven Murray committed
89
90
91
  ASSERT_FALSE(cmdLine->getHelp());
  ASSERT_FALSE(cmdLine->getDebug());
  ASSERT_FALSE(cmdLine->getReadOnly());
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  ASSERT_EQ(TAPE_LIBRARY_TYPE_ACS,
    cmdLine->getDriveLibrarySlot().getLibraryType());
  ASSERT_EQ(std::string("acs1,2,3,4"),
    cmdLine->getDriveLibrarySlot().str());
}

TEST_F(castor_mediachanger_MountCmdLineTest, copy_constructor) {
  using namespace castor::mediachanger;

  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 3;
  args->argv = new char *[4];
  args->argv[0] = dupString("castor-tape-mediachanger-mount");
  args->argv[1] = dupString("vid");
  args->argv[2] = dupString("acs1,2,3,4");
  args->argv[3] = NULL;

  MountCmdLine cmdLine1(args->argc, args->argv);
Steven Murray's avatar
Steven Murray committed
111
112
113
  ASSERT_FALSE(cmdLine1.getHelp());
  ASSERT_FALSE(cmdLine1.getDebug());
  ASSERT_FALSE(cmdLine1.getReadOnly());
114
115
116
117
118
119
  ASSERT_EQ(TAPE_LIBRARY_TYPE_ACS,
    cmdLine1.getDriveLibrarySlot().getLibraryType());
  ASSERT_EQ(std::string("acs1,2,3,4"),
    cmdLine1.getDriveLibrarySlot().str());

  MountCmdLine cmdLine2(cmdLine1);
Steven Murray's avatar
Steven Murray committed
120
121
122
  ASSERT_FALSE(cmdLine2.getHelp());
  ASSERT_FALSE(cmdLine2.getDebug());
  ASSERT_FALSE(cmdLine2.getReadOnly());
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  ASSERT_EQ(TAPE_LIBRARY_TYPE_ACS,
    cmdLine2.getDriveLibrarySlot().getLibraryType());
  ASSERT_EQ(std::string("acs1,2,3,4"),
    cmdLine2.getDriveLibrarySlot().str());
}

TEST_F(castor_mediachanger_MountCmdLineTest, assignment) {
  using namespace castor::mediachanger;

  Argcv *args1 = new Argcv();
  m_argsList.push_back(args1);
  args1->argc = 3;
  args1->argv = new char *[4];
  args1->argv[0] = dupString("castor-tape-mediachanger-mount");
  args1->argv[1] = dupString("vid");
  args1->argv[2] = dupString("acs1,2,3,4");
  args1->argv[3] = NULL;

  MountCmdLine cmdLine1(args1->argc, args1->argv);
Steven Murray's avatar
Steven Murray committed
142
143
144
  ASSERT_FALSE(cmdLine1.getHelp());
  ASSERT_FALSE(cmdLine1.getDebug());
  ASSERT_FALSE(cmdLine1.getReadOnly());
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  ASSERT_EQ(TAPE_LIBRARY_TYPE_ACS,
    cmdLine1.getDriveLibrarySlot().getLibraryType());
  ASSERT_EQ(std::string("acs1,2,3,4"),
    cmdLine1.getDriveLibrarySlot().str());

  Argcv *args2 = new Argcv();
  m_argsList.push_back(args2);
  args2->argc = 3;
  args2->argv = new char *[4];
  args2->argv[0] = dupString("castor-tape-mediachanger-mount");
  args2->argv[1] = dupString("vid");
  args2->argv[2] = dupString("acs5,6,7,8");
  args2->argv[3] = NULL;
  
  MountCmdLine cmdLine2(args2->argc, args2->argv);
Steven Murray's avatar
Steven Murray committed
160
161
162
  ASSERT_FALSE(cmdLine2.getHelp());
  ASSERT_FALSE(cmdLine2.getDebug());
  ASSERT_FALSE(cmdLine2.getReadOnly());
163
164
165
166
167
168
169
  ASSERT_EQ(TAPE_LIBRARY_TYPE_ACS,
    cmdLine2.getDriveLibrarySlot().getLibraryType());
  ASSERT_EQ(std::string("acs5,6,7,8"),
    cmdLine2.getDriveLibrarySlot().str());

  cmdLine1 = cmdLine2;

Steven Murray's avatar
Steven Murray committed
170
171
172
  ASSERT_FALSE(cmdLine1.getHelp());
  ASSERT_FALSE(cmdLine1.getDebug());
  ASSERT_FALSE(cmdLine1.getReadOnly());
173
174
175
176
177
  ASSERT_EQ(TAPE_LIBRARY_TYPE_ACS,
    cmdLine1.getDriveLibrarySlot().getLibraryType());
  ASSERT_EQ(std::string("acs5,6,7,8"),
    cmdLine1.getDriveLibrarySlot().str());

Steven Murray's avatar
Steven Murray committed
178
179
180
  ASSERT_FALSE(cmdLine2.getHelp());
  ASSERT_FALSE(cmdLine2.getDebug());
  ASSERT_FALSE(cmdLine2.getReadOnly());
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  ASSERT_EQ(TAPE_LIBRARY_TYPE_ACS,
    cmdLine2.getDriveLibrarySlot().getLibraryType());
  ASSERT_EQ(std::string("acs5,6,7,8"),
    cmdLine2.getDriveLibrarySlot().str());
}

TEST_F(castor_mediachanger_MountCmdLineTest, scsi) {
  using namespace castor::mediachanger;

  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 3;
  args->argv = new char *[4];
  args->argv[0] = dupString("castor-tape-mediachanger-mount");
  args->argv[1] = dupString("vid");
  args->argv[2] = dupString("smc@rmcHostname,1");
  args->argv[3] = NULL;

  std::unique_ptr<MountCmdLine> cmdLine;
  ASSERT_NO_THROW(cmdLine.reset(new MountCmdLine(args->argc, args->argv)));

Steven Murray's avatar
Steven Murray committed
202
203
204
  ASSERT_FALSE(cmdLine->getHelp());
  ASSERT_FALSE(cmdLine->getDebug());
  ASSERT_FALSE(cmdLine->getReadOnly());
205
206
207
208
209
  ASSERT_EQ(TAPE_LIBRARY_TYPE_SCSI,
    cmdLine->getDriveLibrarySlot().getLibraryType());
}

} // namespace unitTests