FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
fossologyTest.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
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 along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
34 require_once ('TestEnvironment.php');
35 require_once ('commonTestFuncs.php');
38 global $URL;
39 global $USER;
40 global $PASSWORD;
41 
51 class fossologyTest extends WebTestCase
52 {
53  public $mybrowser;
54  public $cookie;
55  public $debug;
56  private $Url;
57  protected $User = NULL;
58  private $Password = NULL;
59 
60  /* Accesor methods */
61  public function getBrowser() {
62  return ($this->mybrowser);
63  }
64  public function getCookie() {
65  return ($this->cookie);
66  }
67  public function getPassword() {
68  return ($this->Password);
69  }
70  public function getUser() {
71  return ($this->User);
72  }
73  public function setBrowser($browser) {
74  return ($this->mybrowser = $browser);
75  }
76  public function setmyCookie($cookie) {
77  return ($this->cookie = $cookie);
78  }
79  public function setPassword($password) {
80  return ($this->Password = $password);
81  }
82  public function setUser($user) {
83  return ($this->User = $user);
84  }
85 
86  /* Factory methods, still need to change methods */
87  function pmm($test)
88  {
89  return(new parseMiniMenu($this));
90  }
91  function plt($test)
92  {
93  return(new parseLicenseTbl($this));
94  }
95 
96  /* Methods */
97 
98 
109  protected function _browser() {
110 
111  if(is_object($this->mybrowser)) {
112  return($this->mybrowser);
113  }
114  else {
115  $browser = new SimpleBrowser();
116  if(is_object($browser)) {
117  $this->setBrowser($browser);
118  }
119  else {
120  $this->fail("FAIL! Login() internal failure did not get a browser object\n");
121  }
122  }
123  return($this->mybrowser);
124  } //_browser
125 
137  public function createTestFolder($name, $parent='root')
138  {
139  global $URL;
140  if ($parent == 'root') { $parent = null; }
141  if (empty($name)){
142  $pid = getmypid();
143  $name = 'Testing-' . $pid;
144  }
145  $page = $this->mybrowser->get($URL);
146  $this->createFolder($parent, $name, null);
147  }// createTestingFolder
161  public function getFolderId($folderName, $page, $selectName) {
162  if(empty($folderName)) {
163  return(NULL);
164  }
165  else if (empty($page)) {
166  return(NULL);
167  }
168  else if (empty($selectName)) {
169  return(NULL);
170  }
171  /*
172  * special case the folder called root, it's always folder id 1.
173  * This way we still don't have to query for the name.
174  *
175  * This will probably break when users are implimented.
176  */
177  if(($folderName == 'root') || ($folderName == 1)) {
178  return(1);
179  }
180  $FolderId = $this->parseSelectStmnt($page, $selectName, $folderName);
181  if(empty($FolderId)) {
182  return(NULL);
183  }
184  return($FolderId);
185  }
199  public function getUploadId($uploadName, $page, $selectName) {
200  if(empty($uploadName)) {
201  return(NULL);
202  }
203  else if (empty($page)) {
204  return(NULL);
205  }
206  else if (empty($selectName)) {
207  return(NULL);
208  }
209  $UploadId = $this->parseSelectStmnt($page, $selectName, $uploadName);
210  if(empty($UploadId)) {
211  return(NULL);
212  }
213  return ($UploadId);
214  }
215 
216 
225  public function Login($User=NULL, $Password=NULL)
226  {
227  global $URL;
228  global $USER;
229  global $PASSWORD;
230 
231  // user name passed in, use what is supplied, (can have blank password)
232  if(!empty($User)) {
233  $this->setUser($User);
234  $this->setPassword($Password);
235  }
236  else // no valid paramaters, use user in TestEnvironment.php
237  {
238  $this->setUser($USER);
239  $this->setPassword($PASSWORD);
240  }
241 
242  $browser = $this->_browser();
243  $page = $this->mybrowser->get($URL);
244  $this->assertTrue($page,"Login FAILED! did not fetch a web page, can't login\n'");
245  $cookie = $this->_repoDBlogin($this->mybrowser);
246  $this->setmyCookie($cookie);
247  $host = getHost($URL);
248  $this->mybrowser->setCookie('Login', $cookie, $host);
249 
250  $url = $this->mybrowser->getUrl();
251  $page = $this->mybrowser->getContent($url);
252  //$c = $this->__chopPage($page);
253  //print "*********************page at end of LOGIN is:*******\n$c\n";
254  }
255 
263  public function Logout($User=NULL)
264  {
265  global $URL;
266  global $USER;
267 
268  if(strlen($User)) {
269  $this->setUser($User);
270  }
271  /*
272  else {
273  $this->setUser($USER);
274  }
275  */
276  $url = $this->mybrowser->getUrl();
277  $loggedIn = $this->mybrowser->get($URL);
278  //$this->assertTrue($this->myassertText($loggedIn, "/User:<\/small> $User/"),
279  // "Did not find User:<\/small> $User, is $User logged in?\n");
280  // must do 2 calls. For some reason the ?mod=auth does not logout, but
281  // gets to a page where the logout link works.
282 
283 
284  $page = $this->mybrowser->get("$URL?mod=auth");
285  $page = $this->mybrowser->clickLink('logout');
286  $host = getHost($URL);
287  $clearCookie = $this->mybrowser->setCookie('Login', '', $host);
288  $page = $this->mybrowser->get("$URL?mod=Default");
289  //$p = $this->__chopPage($page);
290  //print "page after logout sequence is:$p\n";
291 
292  if($this->myassertText($page,"/This login uses HTTP/") !== TRUE) {
293  //if($this->myassertText($page,"/Where to Begin\.\.\./") !== TRUE) {
294  $this->fail("FAIL! Did not find string 'This login uses HTTP', Is user logged out?\n");
295  //$this->fail("FAIL! Did not find string 'Where to Begin...', Is user logged out?\n");
296  $this->setUser(NULL);
297  $this->setPassword(NULL);
298  return(FALSE);
299  }
300  return(TRUE);
301  }
302 
303  private function _repoDBlogin($browser = NULL) {
304 
305  if (is_null($browser))
306  {
307  $this->_browser();
308  }
309 
310  global $URL;
311  global $USER;
312  global $PASSWORD;
313 
314  $page = NULL;
315  $cookieValue = NULL;
316 
317  $host = getHost($URL);
318  $this->assertTrue(is_object($this->mybrowser));
319  $this->mybrowser->useCookies();
320  $cookieValue = $this->mybrowser->getCookieValue($host, '/', 'Login');
321  $this->mybrowser->setCookie('Login', $cookieValue, $host);
322  $page = $this->mybrowser->get("$URL?mod=auth&nopopup=1");
323  $this->assertTrue($page);
324 
325  /* Use the test configured user if none specified */
326  if(!strlen($this->User)) {
327  $this->setUser($USER);
328  }
329  // no check on the password, as it could be blank, just use it...It should
330  // have been set (if there was one) in Login
331  //echo "FTDB: user is:$this->User and Password:$this->Password\n";
332  $this->assertTrue($this->mybrowser->setField('username', $this->User),
333  "Fatal! could not set username field in login form for $this->User\n");
334  $this->assertTrue($this->mybrowser->setField('password', $this->Password),
335  "Fatal! could not set password field in login form for $this->User\n");
336  $this->assertTrue($this->mybrowser->isSubmit('Login'));
337  $page = $this->mybrowser->clickSubmit('Login');
338  $this->assertTrue($page,"FATAL! did not get a valid page back from Login\n");
339  //print "DB: _RDBL: After Login ****page is:$page\n";
340  $page = $this->mybrowser->get("$URL?mod=Default");
341  //$p = $this->__chopPage($page);
342  //print "DB: _RDBL: After mod=Default ****page is:$page\n";
343  $this->assertTrue($this->myassertText($page, "/User:<\/small>\s$this->User/"),
344  "Did not find User:<\/small> $this->User\nThe User may not be logged in\n");
345  $this->mybrowser->setCookie('Login', $cookieValue, $host);
346  $page = $this->mybrowser->getContent();
347  $NumMatches = preg_match('/User Logged Out/', $page, $matches);
348  $this->assertFalse($NumMatches, "User Logged out!, Login Failed! %s");
349  return ($cookieValue);
350  } // _repoDBlogin
351 
352  public function myassertText($page, $pattern) {
353  $NumMatches = preg_match($pattern, $page, $matches);
354  if ($NumMatches) {
355  return (TRUE);
356  }
357  return (FALSE);
358  }
359 
375  public function parseSelectStmnt($page,$selectName,$optionText=NULL) {
376  if(empty($page)) {
377  return(NULL);
378  }
379  if(empty($selectName)) {
380  return(NULL);
381  }
382  $hpage = new DOMDocument();
383  @$hpage->loadHTML($page);
384  /* get select and options */
385  $selectList = $hpage->getElementsByTagName('select');
386  $optionList = $hpage->getElementsByTagName('option');
387  /*
388  * gather the section names and group the options with each section
389  * collect the data at the same time. Assemble into the data structure.
390  */
391  for($i=0; $i < $selectList->length; $i++) {
392  $ChildList = $selectList->item($i)->childNodes;
393  foreach($ChildList as $child) {
394  $optionValue = $child->getAttribute('value');
395  $orig = $child->nodeValue;
396  /*
397  * need to clean up the string, to get rid of &nbsp codes, or the keys
398  * will not match.
399  */
400  $he = htmlentities($orig);
401  $htmlGone = preg_replace('/&.*?;/','',$he);
402  $cleanText = trim($htmlGone);
403  if(!empty($optionText)) {
404  $noDotOptText = escapeDots($optionText);
405  $match = preg_match("/^$noDotOptText$/", $cleanText, $matches);
406  if($match) {
407  /* Use the matched optionText instead of the whole string */
408  //print "Adding matches[0] to select array\n";
409  $Selects[$selectList->item($i)->getAttribute('name')][$matches[0]] = $optionValue;
410  }
411  }
412  else {
413  /*
414  * Add the complete string contained in the <option>, any
415  * html & values should have been removed.
416  */
417  //print "Adding cleanText to select array\n";
418  $Selects[$selectList->item($i)->getAttribute('name')][$cleanText] = $optionValue;
419  $foo = $selectList->item($i)->getAttribute('onload');
420  }
421  }
422  } //for
423 
424  /*
425  * if there were no selects found, then we were passed something that
426  * doesn't exist.
427  */
428  if (empty($Selects)) {
429  return(NULL);
430  }
431  //print "FTPSS: selects array is:\n";print_r($Selects) . "\n";
432  /* Return either an int or an array */
433  if (!is_null($optionText)) {
434  if(array_key_exists($optionText,$Selects[$selectName])){
435  return($Selects[$selectName][$optionText]); // int
436  }
437  else {
438  return(NULL);
439  }
440  }
441  else {
442  if(array_key_exists($selectName,$Selects)){
443  return($Selects[$selectName]); // array
444  }
445  else {
446  return(NULL); // didn't find any...
447  }
448  }
449  } // parseSelectStmnt
450 
451 
471  public function parseFossjobs($all=NULL) {
472  /*
473  use fossjobs to get the upload ids
474  */
475  $last = exec('fossjobs -u',$uploadList, $rtn);
476  foreach ($uploadList as $upload) {
477  if(empty($upload)) {
478  continue;
479  }
480 
481  list($upId, $file) = split(' ', $upload);
482  if($upId == '#') {
483  continue;
484  }
485 
486  $uploadId = rtrim($upId, ':');
487  $Uploads[$uploadId] = $file;
488  /*
489  gather up the last uploads done on each file (file is not unique)
490  */
491  $LastUploads[$file] = $uploadId;
492  }
493  $lastUp = &$LastUploads;
494  $sorted = arsort($lastUp);
495  //$sorted = arsort(&$LastUploads);
496  if(!empty($all)) {
497  //print "uploads is:\n";print_r($Uploads) . "\n";
498  return($Uploads); // return all uploads
499  }
500  else {
501  //print "LastUploads is:\n";print_r($LastUploads) . "\n";
502  return($LastUploads); // default return
503  }
504  }
505 
520  public function setAgents($agents = NULL) {
521  $agentList = array (
522  'buckets' => 'Check_agent_bucket',
523  'copyright' => 'Check_agent_copyright',
524  'mimetype' => 'Check_agent_mimetype',
525  'nomos' => 'Check_agent_nomos',
526  'package' => 'Check_agent_pkgagent',
527  );
528  /* check parameters and parse */
529  if (is_null($agents)) {
530  return NULL; // No agents to set
531  }
532  /* set them all if 'all' */
533  if (0 === strcasecmp($agents, 'all')) {
534  foreach ($agentList as $agent => $name) {
535  if ($this->debug) {
536  print "SA: setting agents for 'all', agent name is:$name\n";
537  }
538  $this->assertTrue($this->mybrowser->setField($name, 1));
539  }
540  return (NULL);
541  }
542  /*
543  * what is left is 0 or more numbers, comma seperated
544  * parse them then use them to set a list of agents.
545  */
546  $numberList = explode(',', $agents);
547  $numAgents = count($numberList);
548 
549  if ($numAgents = 0) {
550  return NULL; // no agents to schedule
551  }
552  else {
553  foreach ($numberList as $number) {
554  switch ($number) {
555  case 1 :
556  $checklist[] = $agentList['buckets'];
557  break;
558  case 2 :
559  $checklist[] = $agentList['copyright'];
560  break;
561  case 3 :
562  $checklist[] = $agentList['mimetype'];
563  break;
564  case 4 :
565  $checklist[] = $agentList['nomos'];
566  break;
567  case 5 :
568  $checklist[] = $agentList['package'];
569  break;
570  case 6:
571  $checklist[] = $agentList['specagent'];
572  break;
573  case 7:
574  $checklist[] = $agentList['license'];
575  break;
576  }
577  } // foreach
578 
579  if ($this->debug == 1) {
580  print "the agent list is:\n";
581  }
582 
583  foreach ($checklist as $agent) {
584  if ($this->debug) {
585  print "DEBUG: $agent\n";
586  }
587  $this->assertTrue($this->mybrowser->setField($agent, 1));
588  }
589  }
590  return (NULL);
591  } //setAgents
592 
605  protected function getSelectAttr($page, $selectName){
606 
607  if(empty($page)) {
608  return(NULL);
609  }
610  if(empty($selectName)) {
611  return(NULL);
612  }
613  $hpage = new DOMDocument();
614  @$hpage->loadHTML($page);
615  /* get select */
616  $selectList = $hpage->getElementsByTagName('select');
617  //print "number of selects on this page:$selectList->length\n";
618  /*
619  * gather the section names and group the attributes with each section
620  * collect the data at the same time. Assemble into the data structure.
621  */
622  $select = array();
623  for($i=0; $i < $selectList->length; $i++) {
624  $sname = $selectList->item($i)->getAttribute('name');
625  if($sname == $selectName) {
626  /* get some common interesting attributes needed */
627  $onload = $selectList->item($i)->getAttribute('onload');
628  $onchange = $selectList->item($i)->getAttribute('onchange');
629  $id = $selectList->item($i)->getAttribute('id');
630  $select[$sname] = array ('onload' => $onload,
631  'onchange' => $onchange,
632  'id' => $id
633  );
634  break; // all done
635  }
636  }
637  return($select);
638  }
639 
654  protected function setSelectAttr($page, $selectName, $attribute, $value=NULL){
655 
656  if(empty($page)) {
657  return(NULL);
658  }
659  if(empty($selectName)) {
660  return(NULL);
661  }
662  if(empty($attribute)) {
663  return(NULL);
664  }
665  $hpage = new DOMDocument();
666  @$hpage->loadHTML($page);
667  /* get select */
668  $selectList = $hpage->getElementsByTagName('select');
669  /*
670  * gather the section names and group the attributes with each section
671  * collect the data at the same time. Assemble into the data structure.
672  */
673  $select = array();
674  for($i=0; $i < $selectList->length; $i++) {
675  $sname = $selectList->item($i)->getAttribute('name');
676  if($sname == $selectName) {
677  $oldValue= $selectList->item($i)->getAttribute($attribute);
678  if(!empty($value)) {
679  //$node = $selectList->item($i)->set_attribute($attribute,$value);
680  $node = $selectList->item($i)->setAttribute($attribute,$value);
681  }
682  break; // all done
683  }
684  }
685  $setValue= $selectList->item($i)->getAttribute($attribute);
686  if($setValue != $value) {
687  return(NULL);
688  }
689  return(TRUE);
690  } // set SelectAttr
691 
701  private function __chopPage($page) {
702 
703  if(!strlen($page)) {
704  return(FALSE);
705  }
706  return(substr($page,-1536));
707  } // chopPage
708 
709 } // fossolgyTest
710 ?>
int debug
Definition: buckets.c:68
getUploadId($uploadName, $page, $selectName)
Logout($User=NULL)
parseSelectStmnt($page, $selectName, $optionText=NULL)
parseFossjobs($all=NULL)
getSelectAttr($page, $selectName)
getFolderId($folderName, $page, $selectName)
setAgents($agents=NULL)
createTestFolder($name, $parent='root')
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:321
setSelectAttr($page, $selectName, $attribute, $value=NULL)
Login($User=NULL, $Password=NULL)
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695