FOSSology  4.4.0
Open Source License Compliance by Open Source Software
fossologyTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
23 require_once ('TestEnvironment.php');
24 require_once ('commonTestFuncs.php');
27 global $URL;
28 global $USER;
29 global $PASSWORD;
30 
40 class fossologyTest extends WebTestCase
41 {
42  public $mybrowser;
43  public $cookie;
44  public $debug;
45  private $Url;
46  protected $User = NULL;
47  private $Password = NULL;
48 
49  /* Accesor methods */
50  public function getBrowser() {
51  return ($this->mybrowser);
52  }
53  public function getCookie() {
54  return ($this->cookie);
55  }
56  public function getPassword() {
57  return ($this->Password);
58  }
59  public function getUser() {
60  return ($this->User);
61  }
62  public function setBrowser($browser) {
63  return ($this->mybrowser = $browser);
64  }
65  public function setmyCookie($cookie) {
66  return ($this->cookie = $cookie);
67  }
68  public function setPassword($password) {
69  return ($this->Password = $password);
70  }
71  public function setUser($user) {
72  return ($this->User = $user);
73  }
74 
75  /* Factory methods, still need to change methods */
76  function pmm($test)
77  {
78  return(new parseMiniMenu($this));
79  }
80  function plt($test)
81  {
82  return(new parseLicenseTbl($this));
83  }
84 
85  /* Methods */
86 
87 
98  protected function _browser() {
99 
100  if(is_object($this->mybrowser)) {
101  return($this->mybrowser);
102  }
103  else {
104  $browser = new SimpleBrowser();
105  if(is_object($browser)) {
106  $this->setBrowser($browser);
107  }
108  else {
109  $this->fail("FAIL! Login() internal failure did not get a browser object\n");
110  }
111  }
112  return($this->mybrowser);
113  } //_browser
114 
126  public function createTestFolder($name, $parent='root')
127  {
128  global $URL;
129  if ($parent == 'root') { $parent = null; }
130  if (empty($name)){
131  $pid = getmypid();
132  $name = 'Testing-' . $pid;
133  }
134  $page = $this->mybrowser->get($URL);
135  $this->createFolder($parent, $name, null);
136  }// createTestingFolder
150  public function getFolderId($folderName, $page, $selectName) {
151  if(empty($folderName)) {
152  return(NULL);
153  }
154  else if (empty($page)) {
155  return(NULL);
156  }
157  else if (empty($selectName)) {
158  return(NULL);
159  }
160  /*
161  * special case the folder called root, it's always folder id 1.
162  * This way we still don't have to query for the name.
163  *
164  * This will probably break when users are implimented.
165  */
166  if(($folderName == 'root') || ($folderName == 1)) {
167  return(1);
168  }
169  $FolderId = $this->parseSelectStmnt($page, $selectName, $folderName);
170  if(empty($FolderId)) {
171  return(NULL);
172  }
173  return($FolderId);
174  }
188  public function getUploadId($uploadName, $page, $selectName) {
189  if(empty($uploadName)) {
190  return(NULL);
191  }
192  else if (empty($page)) {
193  return(NULL);
194  }
195  else if (empty($selectName)) {
196  return(NULL);
197  }
198  $UploadId = $this->parseSelectStmnt($page, $selectName, $uploadName);
199  if(empty($UploadId)) {
200  return(NULL);
201  }
202  return ($UploadId);
203  }
204 
205 
214  public function Login($User=NULL, $Password=NULL)
215  {
216  global $URL;
217  global $USER;
218  global $PASSWORD;
219 
220  // user name passed in, use what is supplied, (can have blank password)
221  if(!empty($User)) {
222  $this->setUser($User);
223  $this->setPassword($Password);
224  }
225  else // no valid paramaters, use user in TestEnvironment.php
226  {
227  $this->setUser($USER);
228  $this->setPassword($PASSWORD);
229  }
230 
231  $browser = $this->_browser();
232  $page = $this->mybrowser->get($URL);
233  $this->assertTrue($page,"Login FAILED! did not fetch a web page, can't login\n'");
234  $cookie = $this->_repoDBlogin($this->mybrowser);
235  $this->setmyCookie($cookie);
236  $host = getHost($URL);
237  $this->mybrowser->setCookie('Login', $cookie, $host);
238 
239  $url = $this->mybrowser->getUrl();
240  $page = $this->mybrowser->getContent($url);
241  //$c = $this->__chopPage($page);
242  //print "*********************page at end of LOGIN is:*******\n$c\n";
243  }
244 
252  public function Logout($User=NULL)
253  {
254  global $URL;
255  global $USER;
256 
257  if(strlen($User)) {
258  $this->setUser($User);
259  }
260  /*
261  else {
262  $this->setUser($USER);
263  }
264  */
265  $url = $this->mybrowser->getUrl();
266  $loggedIn = $this->mybrowser->get($URL);
267  //$this->assertTrue($this->myassertText($loggedIn, "/User:<\/small> $User/"),
268  // "Did not find User:<\/small> $User, is $User logged in?\n");
269  // must do 2 calls. For some reason the ?mod=auth does not logout, but
270  // gets to a page where the logout link works.
271 
272 
273  $page = $this->mybrowser->get("$URL?mod=auth");
274  $page = $this->mybrowser->clickLink('logout');
275  $host = getHost($URL);
276  $clearCookie = $this->mybrowser->setCookie('Login', '', $host);
277  $page = $this->mybrowser->get("$URL?mod=Default");
278  //$p = $this->__chopPage($page);
279  //print "page after logout sequence is:$p\n";
280 
281  if($this->myassertText($page,"/This login uses HTTP/") !== TRUE) {
282  //if($this->myassertText($page,"/Where to Begin\.\.\./") !== TRUE) {
283  $this->fail("FAIL! Did not find string 'This login uses HTTP', Is user logged out?\n");
284  //$this->fail("FAIL! Did not find string 'Where to Begin...', Is user logged out?\n");
285  $this->setUser(NULL);
286  $this->setPassword(NULL);
287  return(FALSE);
288  }
289  return(TRUE);
290  }
291 
292  private function _repoDBlogin($browser = NULL) {
293 
294  if (is_null($browser))
295  {
296  $this->_browser();
297  }
298 
299  global $URL;
300  global $USER;
301  global $PASSWORD;
302 
303  $page = NULL;
304  $cookieValue = NULL;
305 
306  $host = getHost($URL);
307  $this->assertTrue(is_object($this->mybrowser));
308  $this->mybrowser->useCookies();
309  $cookieValue = $this->mybrowser->getCookieValue($host, '/', 'Login');
310  $this->mybrowser->setCookie('Login', $cookieValue, $host);
311  $page = $this->mybrowser->get("$URL?mod=auth&nopopup=1");
312  $this->assertTrue($page);
313 
314  /* Use the test configured user if none specified */
315  if(!strlen($this->User)) {
316  $this->setUser($USER);
317  }
318  // no check on the password, as it could be blank, just use it...It should
319  // have been set (if there was one) in Login
320  //echo "FTDB: user is:$this->User and Password:$this->Password\n";
321  $this->assertTrue($this->mybrowser->setField('username', $this->User),
322  "Fatal! could not set username field in login form for $this->User\n");
323  $this->assertTrue($this->mybrowser->setField('password', $this->Password),
324  "Fatal! could not set password field in login form for $this->User\n");
325  $this->assertTrue($this->mybrowser->isSubmit('Login'));
326  $page = $this->mybrowser->clickSubmit('Login');
327  $this->assertTrue($page,"FATAL! did not get a valid page back from Login\n");
328  //print "DB: _RDBL: After Login ****page is:$page\n";
329  $page = $this->mybrowser->get("$URL?mod=Default");
330  //$p = $this->__chopPage($page);
331  //print "DB: _RDBL: After mod=Default ****page is:$page\n";
332  $this->assertTrue($this->myassertText($page, "/User:<\/small>\s$this->User/"),
333  "Did not find User:<\/small> $this->User\nThe User may not be logged in\n");
334  $this->mybrowser->setCookie('Login', $cookieValue, $host);
335  $page = $this->mybrowser->getContent();
336  $NumMatches = preg_match('/User Logged Out/', $page, $matches);
337  $this->assertFalse($NumMatches, "User Logged out!, Login Failed! %s");
338  return ($cookieValue);
339  } // _repoDBlogin
340 
341  public function myassertText($page, $pattern) {
342  $NumMatches = preg_match($pattern, $page, $matches);
343  if ($NumMatches) {
344  return (TRUE);
345  }
346  return (FALSE);
347  }
348 
364  public function parseSelectStmnt($page,$selectName,$optionText=NULL) {
365  if(empty($page)) {
366  return(NULL);
367  }
368  if(empty($selectName)) {
369  return(NULL);
370  }
371  $hpage = new DOMDocument();
372  @$hpage->loadHTML($page);
373  /* get select and options */
374  $selectList = $hpage->getElementsByTagName('select');
375  $optionList = $hpage->getElementsByTagName('option');
376  /*
377  * gather the section names and group the options with each section
378  * collect the data at the same time. Assemble into the data structure.
379  */
380  for($i=0; $i < $selectList->length; $i++) {
381  $ChildList = $selectList->item($i)->childNodes;
382  foreach($ChildList as $child) {
383  $optionValue = $child->getAttribute('value');
384  $orig = $child->nodeValue;
385  /*
386  * need to clean up the string, to get rid of &nbsp codes, or the keys
387  * will not match.
388  */
389  $he = htmlentities($orig);
390  $htmlGone = preg_replace('/&.*?;/','',$he);
391  $cleanText = trim($htmlGone);
392  if(!empty($optionText)) {
393  $noDotOptText = escapeDots($optionText);
394  $match = preg_match("/^$noDotOptText$/", $cleanText, $matches);
395  if($match) {
396  /* Use the matched optionText instead of the whole string */
397  //print "Adding matches[0] to select array\n";
398  $Selects[$selectList->item($i)->getAttribute('name')][$matches[0]] = $optionValue;
399  }
400  }
401  else {
402  /*
403  * Add the complete string contained in the <option>, any
404  * html & values should have been removed.
405  */
406  //print "Adding cleanText to select array\n";
407  $Selects[$selectList->item($i)->getAttribute('name')][$cleanText] = $optionValue;
408  $foo = $selectList->item($i)->getAttribute('onload');
409  }
410  }
411  } //for
412 
413  /*
414  * if there were no selects found, then we were passed something that
415  * doesn't exist.
416  */
417  if (empty($Selects)) {
418  return(NULL);
419  }
420  //print "FTPSS: selects array is:\n";print_r($Selects) . "\n";
421  /* Return either an int or an array */
422  if (!is_null($optionText)) {
423  if(array_key_exists($optionText,$Selects[$selectName])){
424  return($Selects[$selectName][$optionText]); // int
425  }
426  else {
427  return(NULL);
428  }
429  }
430  else {
431  if(array_key_exists($selectName,$Selects)){
432  return($Selects[$selectName]); // array
433  }
434  else {
435  return(NULL); // didn't find any...
436  }
437  }
438  } // parseSelectStmnt
439 
440 
460  public function parseFossjobs($all=NULL) {
461  /*
462  use fossjobs to get the upload ids
463  */
464  $last = exec('fossjobs -u',$uploadList, $rtn);
465  foreach ($uploadList as $upload) {
466  if(empty($upload)) {
467  continue;
468  }
469 
470  list($upId, $file) = explode(' ', $upload);
471  if($upId == '#') {
472  continue;
473  }
474 
475  $uploadId = rtrim($upId, ':');
476  $Uploads[$uploadId] = $file;
477  /*
478  gather up the last uploads done on each file (file is not unique)
479  */
480  $LastUploads[$file] = $uploadId;
481  }
482  $lastUp = &$LastUploads;
483  $sorted = arsort($lastUp);
484  //$sorted = arsort(&$LastUploads);
485  if(!empty($all)) {
486  //print "uploads is:\n";print_r($Uploads) . "\n";
487  return($Uploads); // return all uploads
488  }
489  else {
490  //print "LastUploads is:\n";print_r($LastUploads) . "\n";
491  return($LastUploads); // default return
492  }
493  }
494 
509  public function setAgents($agents = NULL) {
510  $agentList = array (
511  'buckets' => 'Check_agent_bucket',
512  'copyright' => 'Check_agent_copyright',
513  'mimetype' => 'Check_agent_mimetype',
514  'nomos' => 'Check_agent_nomos',
515  'package' => 'Check_agent_pkgagent',
516  );
517  /* check parameters and parse */
518  if (is_null($agents)) {
519  return NULL; // No agents to set
520  }
521  /* set them all if 'all' */
522  if (0 === strcasecmp($agents, 'all')) {
523  foreach ($agentList as $agent => $name) {
524  if ($this->debug) {
525  print "SA: setting agents for 'all', agent name is:$name\n";
526  }
527  $this->assertTrue($this->mybrowser->setField($name, 1));
528  }
529  return (NULL);
530  }
531  /*
532  * what is left is 0 or more numbers, comma seperated
533  * parse them then use them to set a list of agents.
534  */
535  $numberList = explode(',', $agents);
536  $numAgents = count($numberList);
537 
538  if ($numAgents = 0) {
539  return NULL; // no agents to schedule
540  }
541  else {
542  foreach ($numberList as $number) {
543  switch ($number) {
544  case 1 :
545  $checklist[] = $agentList['buckets'];
546  break;
547  case 2 :
548  $checklist[] = $agentList['copyright'];
549  break;
550  case 3 :
551  $checklist[] = $agentList['mimetype'];
552  break;
553  case 4 :
554  $checklist[] = $agentList['nomos'];
555  break;
556  case 5 :
557  $checklist[] = $agentList['package'];
558  break;
559  case 6:
560  $checklist[] = $agentList['specagent'];
561  break;
562  case 7:
563  $checklist[] = $agentList['license'];
564  break;
565  }
566  } // foreach
567 
568  if ($this->debug == 1) {
569  print "the agent list is:\n";
570  }
571 
572  foreach ($checklist as $agent) {
573  if ($this->debug) {
574  print "DEBUG: $agent\n";
575  }
576  $this->assertTrue($this->mybrowser->setField($agent, 1));
577  }
578  }
579  return (NULL);
580  } //setAgents
581 
594  protected function getSelectAttr($page, $selectName){
595 
596  if(empty($page)) {
597  return(NULL);
598  }
599  if(empty($selectName)) {
600  return(NULL);
601  }
602  $hpage = new DOMDocument();
603  @$hpage->loadHTML($page);
604  /* get select */
605  $selectList = $hpage->getElementsByTagName('select');
606  //print "number of selects on this page:$selectList->length\n";
607  /*
608  * gather the section names and group the attributes with each section
609  * collect the data at the same time. Assemble into the data structure.
610  */
611  $select = array();
612  for($i=0; $i < $selectList->length; $i++) {
613  $sname = $selectList->item($i)->getAttribute('name');
614  if($sname == $selectName) {
615  /* get some common interesting attributes needed */
616  $onload = $selectList->item($i)->getAttribute('onload');
617  $onchange = $selectList->item($i)->getAttribute('onchange');
618  $id = $selectList->item($i)->getAttribute('id');
619  $select[$sname] = array ('onload' => $onload,
620  'onchange' => $onchange,
621  'id' => $id
622  );
623  break; // all done
624  }
625  }
626  return($select);
627  }
628 
643  protected function setSelectAttr($page, $selectName, $attribute, $value=NULL){
644 
645  if(empty($page)) {
646  return(NULL);
647  }
648  if(empty($selectName)) {
649  return(NULL);
650  }
651  if(empty($attribute)) {
652  return(NULL);
653  }
654  $hpage = new DOMDocument();
655  @$hpage->loadHTML($page);
656  /* get select */
657  $selectList = $hpage->getElementsByTagName('select');
658  /*
659  * gather the section names and group the attributes with each section
660  * collect the data at the same time. Assemble into the data structure.
661  */
662  $select = array();
663  for($i=0; $i < $selectList->length; $i++) {
664  $sname = $selectList->item($i)->getAttribute('name');
665  if($sname == $selectName) {
666  $oldValue= $selectList->item($i)->getAttribute($attribute);
667  if(!empty($value)) {
668  //$node = $selectList->item($i)->set_attribute($attribute,$value);
669  $node = $selectList->item($i)->setAttribute($attribute,$value);
670  }
671  break; // all done
672  }
673  }
674  $setValue= $selectList->item($i)->getAttribute($attribute);
675  if($setValue != $value) {
676  return(NULL);
677  }
678  return(TRUE);
679  } // set SelectAttr
680 
690  private function __chopPage($page) {
691 
692  if(!strlen($page)) {
693  return(FALSE);
694  }
695  return(substr($page,-1536));
696  } // chopPage
697 
698 } // fossolgyTest
int debug
Definition: buckets.c:57
getUploadId($uploadName, $page, $selectName)
setSelectAttr($page, $selectName, $attribute, $value=NULL)
createTestFolder($name, $parent='root')
getFolderId($folderName, $page, $selectName)
parseSelectStmnt($page, $selectName, $optionText=NULL)
parseFossjobs($all=NULL)
Login($User=NULL, $Password=NULL)
getSelectAttr($page, $selectName)
Logout($User=NULL)
setAgents($agents=NULL)
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308