|
| 1 | +package file_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + geturl = "https://download.microsoft.com/download/8/b/4/8b4addd8-e957-4dea-bdb8-c4e00af5b94b/NDP1.1sp1-KB867460-X86.exe" |
| 12 | + help = "-h" |
| 13 | + fileName = "NDP1.1sp1-KB867460-X86.exe" |
| 14 | + all = "-all " |
| 15 | + multipleAlgs = "-a MD5,SHA1,SHA512 " |
| 16 | + invalidServerConfig = "-sc /tmp/nothingFile " |
| 17 | + invalidAlgo = "-a TestSum " |
| 18 | + invalidFile = "/tmp/nothingFile" |
| 19 | + remoteOnly = "-remoteOnly " |
| 20 | + localOnly = "-localOnly " |
| 21 | + timeout = "-t 1 -a RMD160 " |
| 22 | + invalidLib = "-lib openGo " |
| 23 | + validLibGo = "-a MD5 -lib Go " |
| 24 | + validLibOpenssl = "-a MD5 -lib openssl " |
| 25 | + validLibOs = "-a MD5 -lib os " |
| 26 | + invalidDir = "-dir /tmp/nothingFile " |
| 27 | + validDir = "-dir /tmp " |
| 28 | + keep = "-keep " |
| 29 | + serve = "-s " + validDir |
| 30 | + |
| 31 | + MD4 = "bb137fd4893ab9d85906257ede37dfaf" |
| 32 | + MD5 = "22e38a8a7d90c088064a0bbc882a69e5" |
| 33 | + SHA1 = "74a5b25d65a70b8ecd6a9c301a0aea10d8483a23" |
| 34 | + SHA224 = "18507f80722780ca477d7f10528ae28dd176f8d36cbce05a50cc7be0" |
| 35 | + SHA256 = "2c0a35409ff0873cfa28b70b8224e9aca2362241c1f0ed6f622fef8d4722fd9a" |
| 36 | + SHA384 = "c2372c71f93b5dc2a1c21c804bc74e27d82bfa45ee50fbc9037e713c156f1c591ffbe5e87f94022157906098916403b4" |
| 37 | + SHA512 = "bbe643f447f49636732b12d23a052d02681ad41f6920dc1038b073fa600f7589b378ed8e7de97e811543d93ae89ce52871a85ee58aa3b6aeaddc01bc1617ad85" |
| 38 | + RMD160 = "5bcec7ca2ff3b4b13db72cafb14c311a8fd281dd" |
| 39 | + SHA3224 = "b46b0b864c18029d4b90c4b16be7d9b96f6691d384c6aafb90fec059" |
| 40 | + SHA3256 = "5ea8f6ad9b197018aef9218cc6c372d7bff4e90eec8335cd884eeb50458e482f" |
| 41 | + SHA3384 = "3fee7161da83c2933d3fa6af675d74307bfee8c866a03a6e28775c441376db514359543e482d7fcb4b94ef7faf15dea7" |
| 42 | + SHA3512 = "28f136f06940f1325e4a081652159d9030ab384ec8e3201ff694b987643614f24b9625ad7212189ae706738be34d434629f54bb0a0303f549f7fa7b27160d409" |
| 43 | + SHA512224 = "63b2ffb0c5f1cd68abafba23997482b2087d486dcf60bec6fef7446d" |
| 44 | + SHA512256 = "7b44095feff471dee9366a2153dfe2654d70754c21b7e5204ed950cdf4a3f15a" |
| 45 | + BLAKE2s256 = "699c510d881bf3015dc027afbc32c8ae74b431342520a708fe9bf1760f4b26ef" |
| 46 | + BLAKE2b256 = "9d4d2f9ec65027b070163b3832070effa257f3f57d81ca226774625a90e27e0f" |
| 47 | + BLAKE2b384 = "afcfab976281f0b0a3c30213e9de1715274d811849dcaf155909aebef9d4facf7748b4141cb93133fa8d09587d81c7a8" |
| 48 | + BLAKE2b512 = "ec2de7c2c0c51dc1e016e1a4ef64b23830b088ca9aaf582a5531a6595a5339b61232168780b7e5d40e5bf92281a7272680650f518ae1ba1f33e7b7f8f3077219" |
| 49 | + SM3 = "05c4c5ac91721edec0bc332bcb3a5b5973291f433c8e0f69d23b5d5e354c37dd" |
| 50 | + SHAKE256 = "cfe847bc16528928ca7ab7bb0fb0ccd5443839335a8d5c5613ca777b4ab3859e" |
| 51 | + SHAKE128 = "9aac296a041c51b517223be2c31283d6" |
| 52 | +) |
| 53 | + |
| 54 | +func TestGet(t *testing.T) { |
| 55 | + |
| 56 | + execCommand(localOnly+geturl, fileName, true, t, SHA512) |
| 57 | +} |
| 58 | + |
| 59 | +func TestLibOpenSSL(t *testing.T) { |
| 60 | + |
| 61 | + commandStr := localOnly + validLibOpenssl + geturl |
| 62 | + execCommand(commandStr, fileName, true, t, MD5) |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | +func TestLibOS(t *testing.T) { |
| 67 | + |
| 68 | + commandStr := localOnly + validLibOs + geturl |
| 69 | + execCommand(commandStr, fileName, true, t, MD5) |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | +func TestLibGO(t *testing.T) { |
| 74 | + |
| 75 | + commandStr := localOnly + validLibGo + geturl |
| 76 | + execCommand(commandStr, fileName, true, t, MD5) |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +func TestAllAlgosGoLib(t *testing.T) { |
| 81 | + |
| 82 | + commandStr := all + localOnly + geturl |
| 83 | + execCommand(commandStr, fileName, true, t, MD4, MD5, SHA1, SHA224, SHA384, SHA256, SHA512, SHA3224, SHA3384, SHA3256, SHA3512, SHA512224, SHA512256, RMD160, BLAKE2s256, BLAKE2b256, BLAKE2b384, BLAKE2b512) |
| 84 | + |
| 85 | +} |
| 86 | +func TestAllAlgosOpenSSLLib(t *testing.T) { |
| 87 | + |
| 88 | + commandStr := all + localOnly + validLibOpenssl + geturl |
| 89 | + execCommand(commandStr, fileName, true, t, MD4, MD5, SHA1, SHA224, SHA384, SHA256, SHA512, SHA3224, SHA3384, SHA3256, SHA3512, SHA512224, SHA512256, RMD160, BLAKE2s256, BLAKE2b512, SHAKE128, SHAKE256, SM3) |
| 90 | + |
| 91 | +} |
| 92 | +func TestAllAlgosOSLib(t *testing.T) { |
| 93 | + |
| 94 | + commandStr := all + localOnly + validLibOs + geturl |
| 95 | + execCommand(commandStr, fileName, true, t, MD5, SHA1, SHA224, SHA384, SHA256, SHA512) |
| 96 | + |
| 97 | +} |
| 98 | +func TestMultipleAlgos(t *testing.T) { |
| 99 | + |
| 100 | + commandStr := multipleAlgs + localOnly + geturl |
| 101 | + execCommand(commandStr, fileName, true, t, MD5, SHA1, SHA512) |
| 102 | + |
| 103 | +} |
| 104 | +func TestValidDir(t *testing.T) { |
| 105 | + |
| 106 | + commandStr := localOnly + validDir + geturl |
| 107 | + execCommand(commandStr, "/tmp/"+fileName, true, t, SHA512) |
| 108 | + |
| 109 | +} |
| 110 | + |
| 111 | +func TestInValidDir(t *testing.T) { |
| 112 | + |
| 113 | + commandStr := localOnly + invalidDir + geturl |
| 114 | + execForError(commandStr, fileName, false, t, "Given -dir parameter") |
| 115 | + |
| 116 | +} |
| 117 | + |
| 118 | +func TestInValidAlgo(t *testing.T) { |
| 119 | + |
| 120 | + commandStr := localOnly + invalidAlgo + geturl |
| 121 | + execForError(commandStr, fileName, false, t, "Unrecognized algorithm") |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | +func TestInValidLib(t *testing.T) { |
| 126 | + |
| 127 | + commandStr := localOnly + invalidLib + geturl |
| 128 | + execForError(commandStr, fileName, false, t, "Unrecognized library selection") |
| 129 | +} |
| 130 | + |
| 131 | +func TestInValidFile(t *testing.T) { |
| 132 | + |
| 133 | + commandStr := localOnly + invalidFile |
| 134 | + execForError(commandStr, fileName, false, t, "Given url") |
| 135 | + |
| 136 | +} |
| 137 | +func TestValidationFail(t *testing.T) { |
| 138 | + |
| 139 | + commandStr := localOnly + geturl + " " + MD5 |
| 140 | + execForError(commandStr, fileName, false, t, "MISMATCH") |
| 141 | + |
| 142 | +} |
| 143 | + |
| 144 | +func TestValidation(t *testing.T) { |
| 145 | + |
| 146 | + commandStr := localOnly + geturl + " " + SHA512 |
| 147 | + execCommand(commandStr, fileName, true, t, SHA512) |
| 148 | + |
| 149 | +} |
| 150 | + |
| 151 | +func TestKeep(t *testing.T) { |
| 152 | + commandStr := localOnly + keep + geturl + " " + MD5 |
| 153 | + execForError(commandStr, fileName, true, t, "MISMATCH") |
| 154 | + |
| 155 | +} |
| 156 | + |
| 157 | +func TestRemoteOnly(t *testing.T) { |
| 158 | + commandStr := serve |
| 159 | + cmd := getCommand(commandStr) |
| 160 | + err := cmd.Start() |
| 161 | + defer killServer(cmd, t) |
| 162 | + if err != nil { |
| 163 | + t.Errorf("Can not start server instance! %s", err.Error()) |
| 164 | + } |
| 165 | + commandStr = "-remoteOnly -a MD5 -sc servers.yml " + geturl |
| 166 | + execCommand(commandStr, fileName, false, t, MD5, "server1") |
| 167 | + |
| 168 | +} |
| 169 | + |
| 170 | +func TestServeLocalRemote(t *testing.T) { |
| 171 | + commandStr := serve |
| 172 | + cmd := getCommand(commandStr) |
| 173 | + err := cmd.Start() |
| 174 | + defer killServer(cmd, t) |
| 175 | + if err != nil { |
| 176 | + t.Errorf("Can not start server instance! %s", err.Error()) |
| 177 | + } |
| 178 | + commandStr = "-a MD5 -sc servers.yml " + geturl |
| 179 | + execCommand(commandStr, fileName, true, t, MD5, "server1", "local") |
| 180 | + |
| 181 | +} |
| 182 | + |
| 183 | +func TestServeRemoteValidation(t *testing.T) { |
| 184 | + commandStr := serve |
| 185 | + cmd := getCommand(commandStr) |
| 186 | + err := cmd.Start() |
| 187 | + defer killServer(cmd, t) |
| 188 | + if err != nil { |
| 189 | + t.Errorf("Can not start server instance! %s", err.Error()) |
| 190 | + } |
| 191 | + commandStr = "-a MD5 -sc servers.yml " + geturl + " " + MD5 |
| 192 | + execCommand(commandStr, fileName, true, t, MD5, "server1", "local") |
| 193 | +} |
| 194 | + |
| 195 | +func TestServeRemoteValidationFail(t *testing.T) { |
| 196 | + commandStr := serve |
| 197 | + cmd := getCommand(commandStr) |
| 198 | + err := cmd.Start() |
| 199 | + defer killServer(cmd, t) |
| 200 | + if err != nil { |
| 201 | + t.Errorf("Can not start server instance! %s", err.Error()) |
| 202 | + } |
| 203 | + commandStr = "-a MD5 -sc servers.yml " + geturl + " " + MD4 |
| 204 | + execForError(commandStr, fileName, false, t, "SUSPENDED", "server1", "local") |
| 205 | + |
| 206 | +} |
| 207 | + |
| 208 | +func TestServeAlgoFail(t *testing.T) { |
| 209 | + commandStr := serve |
| 210 | + cmd := getCommand(commandStr) |
| 211 | + err := cmd.Start() |
| 212 | + defer killServer(cmd, t) |
| 213 | + if err != nil { |
| 214 | + t.Errorf("Can not start server instance! %s", err.Error()) |
| 215 | + } |
| 216 | + commandStr = "-a MD5,SHA512 -sc servers.yml " + geturl + " " + MD4 |
| 217 | + execForError(commandStr, fileName, false, t, "you can only run single algorithm") |
| 218 | +} |
| 219 | + |
| 220 | +func killServer(cmd *exec.Cmd, t *testing.T) { |
| 221 | + if cmd != nil && cmd.Process != nil { |
| 222 | + err := cmd.Process.Kill() |
| 223 | + if err != nil { |
| 224 | + t.Errorf("Can not kill process! %s", err.Error()) |
| 225 | + } |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +func execCommand(commandStr string, filename string, keep bool, t *testing.T, contains ...string) { |
| 230 | + cmd := getCommand(commandStr) |
| 231 | + out, err := cmd.CombinedOutput() |
| 232 | + if err != nil { |
| 233 | + t.Errorf("An error occured while calling %s error: %s , \noutput: %s", commandStr, err.Error(), string(out)) |
| 234 | + } else { |
| 235 | + strOut := string(out) |
| 236 | + t.Logf("Calling getsum %s returned success", commandStr) |
| 237 | + t.Logf("\n%s", strOut) |
| 238 | + for _, contain := range contains { |
| 239 | + if !strings.Contains(strOut, contain) { |
| 240 | + t.Errorf("Output doesnt contain sum %s", contain) |
| 241 | + } |
| 242 | + |
| 243 | + } |
| 244 | + |
| 245 | + if keep && !fileExist(filename) { |
| 246 | + t.Errorf("Command successfull but file %s not present!", filename) |
| 247 | + } else if !keep && fileExist(filename) { |
| 248 | + t.Errorf("-keep is false but file is still present!") |
| 249 | + } |
| 250 | + defer deleteFile(filename) |
| 251 | + } |
| 252 | +} |
| 253 | +func execForError(commandStr string, filename string, keep bool, t *testing.T, contains ...string) { |
| 254 | + cmd := getCommand(commandStr) |
| 255 | + out, err := cmd.CombinedOutput() |
| 256 | + if err == nil { |
| 257 | + t.Errorf("An error expected while calling %s , \noutput: %s", commandStr, string(out)) |
| 258 | + } else { |
| 259 | + strOut := string(out) |
| 260 | + t.Logf("Calling getsum %s returned error %s", commandStr, err.Error()) |
| 261 | + t.Logf("\n%s", strOut) |
| 262 | + for _, contain := range contains { |
| 263 | + if !strings.Contains(strOut, contain) { |
| 264 | + t.Errorf("Output doesnt contain error %s", contain) |
| 265 | + } |
| 266 | + |
| 267 | + } |
| 268 | + |
| 269 | + defer deleteFile(filename) |
| 270 | + if !keep && fileExist(filename) { |
| 271 | + t.Errorf("Command returned errpr but file %s is present!", filename) |
| 272 | + } else if keep { |
| 273 | + if !fileExist(filename) { |
| 274 | + t.Errorf("Command returned error and keep param used but file %s is not present!", filename) |
| 275 | + |
| 276 | + } else { |
| 277 | + t.Logf("-keep param success!") |
| 278 | + } |
| 279 | + |
| 280 | + } |
| 281 | + } |
| 282 | +} |
| 283 | + |
| 284 | +func deleteFile(path string) { |
| 285 | + os.Remove(path) |
| 286 | +} |
| 287 | + |
| 288 | +func fileExist(path string) bool { |
| 289 | + _, err := os.Stat(path) |
| 290 | + return !os.IsNotExist(err) |
| 291 | +} |
| 292 | + |
| 293 | +func getCommand(command string) *exec.Cmd { |
| 294 | + fields := strings.Split(command, " ") |
| 295 | + return exec.Command("getsum", fields...) |
| 296 | +} |
0 commit comments