OpenbusTest.lua 9.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
local oil = require "oil"

local Check = require "latt.Check"

local Openbus = require "openbus.Openbus"
local Utils = require "openbus.util.Utils"
local Log = require "openbus.util.Log"

local iConfig = {
  contextID = 1234,
Hugo Roenick's avatar
Hugo Roenick committed
11
  credential_type = "IDL:tecgraf/openbus/core/"..Utils.IDL_VERSION..
12
      "/access_control_service/Credential:1.0",
Hugo Roenick's avatar
Hugo Roenick committed
13
  credential_type_prev = "IDL:openbusidl/acs/Credential:1.0",
14
15
}

16
17
18
19
20
21
22
23
local host = "localhost"
local port = 2089
local user = "tester"
local password = "tester"
local oilLogLevel = 0
local sdkLogLevel = 3

-- alterando as propriedades se preciso
24
local scsutils = require ("scs.core.utils")()
25
local props = {}
26
27
28
scsutils:readProperties(props, "Test.properties")
scsutils = nil

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
if props["host.name"] then
  host = props["host.name"].value
end
if props["host.port"] and tonumber(props["host.port"].value) then
  port = tonumber(props["host.port"].value)
end
if props["user"] then
  user = props["user"].value
end
if props["password"] then
  password = props["password"].value
end
if props["oil.verbose"] and tonumber(props["oil.verbose"].value) then
  oilLogLevel = tonumber(props["oil.verbose"].value)
end
if props["openbus.verbose"] and tonumber(props["openbus.verbose"].value) then
  sdkLogLevel = tonumber(props["openbus.verbose"].value)
end
47
48
49

oil.verbose:level(oilLogLevel)
Log:level(sdkLogLevel)
50
51

Suite = {
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  Test1 = { --Testa o mtodo de inicializao
    testInitNullProps = function(self)
      Check.assertTrue(Openbus:init(host, port, nil, iConfig, iConfig))
      Openbus:destroy()
    end,

    testInitNullHost = function(self)
      Check.assertFalse(Openbus:init(nil, port, props, iConfig, iConfig))
    end,

    testInitNegativePort = function(self)
      Check.assertFalse(Openbus:init(host, -1, props, iConfig, iConfig))
    end,

    testInitInvalidPort = function(self)
      Check.assertFalse(Openbus:init(host, "INVALID", props, iConfig, iConfig))
    end,

    testInitTwice = function(self)
      Check.assertTrue(Openbus:init(host, port, nil, iConfig, iConfig))
      Check.assertFalse(Openbus:init(host, port, nil, iConfig, iConfig))
      Openbus:destroy()
    end,

Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
76
    testDestroyWithoutInit = function(self)
77
78
79
80
81
      Openbus:destroy()
      Check.assertTrue(Openbus:init(host, port, nil, iConfig, iConfig))
      Openbus:destroy()
    end,
  },
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
82

83
  Test2 = {
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
84
    beforeEachTest = function(self)
85
86
87
      Openbus:init(host, port, nil, iConfig)
    end,

88
89
90
91
    afterEachTest = function(self)
      if Openbus:isConnected() then
        Openbus:disconnect()
      end
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
92
      Openbus:destroy()
93
94
    end,

95
    testConnectByPassword = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
96
      Check.assertNotNil(Openbus:connectByLoginPassword(user, password))
97
98
99
100
      Check.assertTrue(Openbus:disconnect())
    end,

    testConnectByPasswordLoginNull = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
101
      Check.assertNil(Openbus:connectByLoginPassword(nil, password))
102
103
104
    end,

    testConnectByPasswordInvalidLogin = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
105
      Check.assertNil(Openbus:connectByLoginPassword("INVALID", password))
106
107
    end,

108
    testConnectByPasswordTwice = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
109
110
      Check.assertNotNil(Openbus:connectByLoginPassword(user, password))
      Check.assertNil(Openbus:connectByLoginPassword(user, password))
111
112
113
      Check.assertTrue(Openbus:disconnect())
    end,

114
    testConnectByCredential = function(self)
115
      Check.assertNil(Openbus:getCredential())
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
116
117
      Check.assertNotNil(Openbus:connectByLoginPassword(user, password))
      local credential = Openbus:getCredential() 
118
      Check.assertNotNil(credential)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
119
120
      Check.assertNotNil(Openbus:connectByCredential(credential))
      Check.assertTrue(Openbus:disconnect())
121
122
123
    end,

    testConnectByCredentialNullCredential = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
124
      Check.assertNil(Openbus:connectByCredential(nil))
125
126
    end,

127
    testConnectByCredentialInvalidCredential = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
128
129
      Check.assertNotNil(Openbus:connectByLoginPassword(user, password))
      local credential = Openbus:getCredential()
130
      Check.assertNotNil(credential)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
131
132
      Check.assertTrue(Openbus:disconnect())
      Check.assertNil(Openbus:connectByCredential(credential))
133
134
    end,

135
136
    testIsConnected = function(self)
      Check.assertFalse(Openbus:isConnected())
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
137
      Check.assertNotNil(Openbus:connectByLoginPassword(user, password))
138
139
140
141
142
143
144
145
146
147
148
149
150
      Check.assertTrue(Openbus:isConnected())
      Check.assertTrue(Openbus:disconnect())
    end,

    testDisconnected = function(self)
      Check.assertFalse(Openbus:disconnect())
    end,

    testGetOrb = function(self)
      Check.assertNotNil(Openbus:getORB())
    end,

    testGetAccessControlService = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
151
152
153
154
      Check.assertNotNil(Openbus:getORB())
      Check.assertNil(Openbus:getAccessControlService())
      Check.assertNil(Openbus:getRegistryService())
      Check.assertNotNil(Openbus:connectByLoginPassword(user, password))
155
156
157
158
159
      Check.assertNotNil(Openbus:getAccessControlService())
      Check.assertTrue(Openbus:disconnect())
    end,

    testGetRegistryService = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
160
161
162
      Check.assertNotNil(Openbus:getORB())
      Check.assertNil(Openbus:getRegistryService())
      Check.assertNotNil(Openbus:connectByLoginPassword(user, password))
163
      Check.assertNotNil(Openbus:getRegistryService())
164
165
166
167
      Check.assertTrue(Openbus:disconnect())
    end,

    testCredentialExpired = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
168
      Check.assertNotNil(Openbus:getORB())
169
170
171
172
173
      local lec = {
        wasExpired = false,
        expired = function(self)
          self.wasExpired = true
        end,
174
        isExpired = function(self)
175
176
177
          return self.wasExpired
        end,
      }
Maíra Gatti's avatar
   
Maíra Gatti committed
178
      Log:info("<< Teste da validade da lease >>")
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
179
180

      Check.assertNotNil(Openbus:connectByLoginPassword(user, password))
181
      Openbus:setLeaseExpiredCallback(lec)
182
183
184
185
      local acs = Openbus:getAccessControlService()
      Check.assertNotNil(acs)
      acs:logout(Openbus:getCredential())
      while (not (lec:isExpired())) do
Maíra Gatti's avatar
   
Maíra Gatti committed
186
        Log:info("Lease no expirou: esperando 30 segundos...")
187
188
        oil.sleep(30)
      end
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
189
      Check.assertFalse(Openbus:disconnect())
190
191
    end,

Maíra Gatti's avatar
   
Maíra Gatti committed
192
    testAddLeaseExpiredCbAfterConnect = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
193
      Check.assertNotNil(Openbus:getORB())
194
195
196
      local lec = {
        wasReconnected = false,
        expired = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
197
          Check.assertNotNil(Openbus:connectByLoginPassword(user, password))
198
199
200
201
202
203
          self.wasReconnected = true
        end,
        isReconnected = function(self)
          return self.wasReconnected
        end,
      }
Maíra Gatti's avatar
   
Maíra Gatti committed
204
      Log:info("<< Teste da reconexo aps a lease no ser mais vlida [Callback cadastrada depois do connect] >>")
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
205
      Check.assertNotNil(Openbus:connectByLoginPassword(user, password))
206
207
208
209
210
      Openbus:setLeaseExpiredCallback(lec)
      local acs = Openbus:getAccessControlService()
      Check.assertNotNil(acs)
      acs:logout(Openbus:getCredential())
      while (not (lec:isReconnected())) do
Maíra Gatti's avatar
   
Maíra Gatti committed
211
        Log:info("Lease no expirou: esperando 30 segundos...")
212
213
        oil.sleep(30)
      end
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
214
      Check.assertTrue(Openbus:disconnect())
215
216
    end,

Maíra Gatti's avatar
   
Maíra Gatti committed
217
    testAddLeaseExpiredCbBeforeConnect = function(self)
218
219
220
      local lec = {
        wasReconnected = false,
        expired = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
221
          Check.assertNotNil(Openbus:connectByLoginPassword(user, password))
222
223
224
225
226
227
          self.wasReconnected = true
        end,
        isReconnected = function(self)
          return self.wasReconnected
        end,
      }
Maíra Gatti's avatar
   
Maíra Gatti committed
228
      Log:info("<< Teste da reconexo aps a lease no ser mais vlida [Callback cadastrada antes do connect] >>")
229
      Openbus:setLeaseExpiredCallback(lec)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
230
      Check.assertNotNil(Openbus:connectByLoginPassword(user, password))
231
232
233
234
      local acs = Openbus:getAccessControlService()
      Check.assertNotNil(acs)
      acs:logout(Openbus:getCredential())
      while (not (lec:isReconnected())) do
Maíra Gatti's avatar
   
Maíra Gatti committed
235
        Log:info("Lease no expirou: esperando 30 segundos...")
236
237
        oil.sleep(30)
      end
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
238
      Check.assertTrue(Openbus:disconnect())
239
240
    end,
  },
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
241

242
243
  Test3 = { -- Testa connectByCertificate
    beforeTestCase = function(self)
244
      self.systemId     = "TesteBarramento"
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
      self.deploymentId = self.systemId
      self.testKeyFile  = self.systemId .. ".key"
      self.acsCertFile  = "AccessControlService.crt"
      local testACSCertFile = assert(io.open(self.acsCertFile,"r"))
      testACSCertFile:close()
      Openbus:init(host, port, nil, iConfig)
    end,

    afterTestCase = function(self)
      Openbus:destroy()
    end,

    afterEachTest = function(self)
      if Openbus:isConnected() then
        Openbus:disconnect()
      end
    end,

    testConnectByCertificate = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
264
      Check.assertNotNil(Openbus:connectByCertificate(self.deploymentId,
265
266
267
          self.testKeyFile, self.acsCertFile))
      Check.assertTrue(Openbus:disconnect())
    end,
Hugo Roenick's avatar
Hugo Roenick committed
268

269
    testConnectByCertificateNullKey = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
270
      Check.assertNil(Openbus:connectByCertificate(self.deploymentId, nil,
271
272
          self.acsCertFile))
    end,
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
273

274
    testConnectByCertificateNullACSCertificate = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
275
      Check.assertNil(Openbus:connectByCertificate(self.deploymentId,
276
277
278
279
          self.testKeyFile, nil))
    end,

    testConnectByCertificateInvalidEntityName = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
280
      Check.assertNil(Openbus:connectByCertificate(nil, self.testKeyFile,
281
282
283
284
          self.acsCertFile))
    end,

    testConnectByCertificateTwice = function(self)
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
285
      Check.assertNotNil(Openbus:connectByCertificate(self.deploymentId,
286
          self.testKeyFile, self.acsCertFile))
Carlos Eduardo Lara Augusto's avatar
Carlos Eduardo Lara Augusto committed
287
      Check.assertNil(Openbus:connectByCertificate(self.deploymentId,
288
289
290
291
          self.testKeyFile, self.acsCertFile))
      Check.assertTrue(Openbus:disconnect())
    end,
  },
292
}