FOSSology  4.4.0
Open Source License Compliance by Open Source Software
dashR.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2007 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
17 $archive_path = '/tmp/fossology';
18 
19 class TestCP2fossRecursion extends UnitTestCase
20 {
21  /*
22  * This function will use illegal archives, that is we have a file for
23  * input, but it is zero length.
24  */
25 
26  public $command = '/usr/local/bin/test.cp2foss';
27 
28  function TestDashRNoArchive()
29  {
30 
31  $error = exec("$this->command -p devnull -n fail -a /dev/null -d \"test should fail\" ",
32  $output, $retval);
33  //print_r($output);
34  $this->assertPattern('/Error, .* not greater than zero/', $output[0]);
35  //print_r($output);
36  $output = array();
37  $error = exec("$this->command -p stdin -n fail -a /dev/stdin -d 'stdin should fail'",
38  $output, $retval);
39  //print_r($output);
40  $this->assertPattern('/Stopping, can\'t process archive/', $output[1]);
41  }
42 
43  /*
44  * This test needs setup: need some dir tree to process.
45  *
46  * Tyically the fossology sources are used. Check them out to
47  * /tmp/fossology for this test to work.
48  *
49  * Consider adding setup and teardown methods. For now the install
50  * test script sets this up.
51  *
52  */
53 
54  function TestNoDashR()
55  {
56  /*
57  * Method: run a real cp2foss run, then examine the tar file created
58  * by it and compare to what was tar'ed up. If no differences,
59  * at least cp2foss worked as far as creating the archive to upload
60  * correctly. This test DOES NOT test if the upload worked.
61  */
62 
63  $last = exec(
64  "$this->command -p CP2fossTest/fldr1 -n fossology -a $archive_path -d 'no -R, only files saved' ", $output, $retval
65  );
66  echo "\$output Only files is:\n"; dump($output); echo "\n";
67 
68  /*
69  * $output[1] will always have the archive we are loading... in this
70  * case it will be a tar file....get only the files under fossology
71  *
72  * NOTE: this seems to be brittle.... sometimes it's 2?! and then
73  * this test breaks....
74  *
75  */
76 
77  $find = "find /tmp/fossology -maxdepth 1 -type f -print";
78  $last = exec($find, $findoutput, $retval);
79  $last = exec("tar -tf $output[1]", $tarout, $retval);
80  // get leaf names from find output
81  $basenames = array();
82  foreach ($findoutput as $path) {
83  $basenames[] = basename($path);
84  }
85  sort($tarout);
86  sort($basenames);
87  $diffs = array_diff($tarout, $basenames);
88  if (empty($diffs)) {
89  $this->pass();
90  } else {
91  $this->fail();
92  }
93  }
94 
95  function TestDashR()
96  {
97  /*
98  * Method: run a real cp2foss run, then examine the tar file created
99  * by it and compare to what was tar'ed up. If no differences,
100  * at least cp2foss worked as far as creating the archive to upload
101  * correctly. This test DOES NOT test if the upload worked.
102  */
103  $last = exec(
104  "$this->command -p CP2fossTest -n fossology -a $archive_path -R -d '-R all contents saved' ",
105  $output, $retval);
106  /*
107  * $output[1] will always have the archive we are loading... in this
108  * case it will be a tar file....get only the files under fossology
109  *
110  */
111 
112  echo "output All contents is:\n"; dump($output); echo "\n";
113  /*
114  * Tried using find and combos of ls etc... issue was getting the
115  * trailing / on directories without changing the format.
116  * Gave up, and will just retar the archive and then compare to the
117  * orginal tar.
118  */
119  $temp_tar = "/tmp/test.tar.bz2";
120  chdir($apath) or die("Can't cd to $apath, $php_errormsg\n");
121 
122  $tcmd = "tar -cjf $temp_tar --exclude='.svn' --exclude='.cvs' *";
123  $last = exec($tcmd, $Rtoutput, $retval);
124  $last = exec("tar -tf $output[1]", $tarout, $retval);
125  $last = exec("tar -tf $temp_tar", $Rtout, $retval);
126  foreach ($tarout as $p) {
127  //echo "tar path is:$p\n";
128  $tpaths[] = rtrim($p);
129  }
130  foreach ($Rtout as $path) {
131  $Rtpaths[] = rtrim($path);
132  }
133  sort($tpaths);
134  sort($Rtpaths);
135  $diffs = array_diff($tpaths, $Rtpaths);
136  if (empty($diffs)) {
137  $this->pass();
138  } else {
139  echo "diffs are:\n";
140  $this->dump($diffs);
141  $this->fail();
142  }
143  }
144 }
145