Skip to content

Commit

Permalink
fix(push): verify push file OKAY response
Browse files Browse the repository at this point in the history
  • Loading branch information
Malinskiy committed May 9, 2020
1 parent 66c45e2 commit bcc7ee3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import com.malinskiy.adam.request.sync.StatFileRequest
import com.malinskiy.adam.rule.AdbDeviceRule
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.receiveOrNull
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -72,13 +71,8 @@ class FileE2ETest {
percentage = newPercentage
}
}
println()

for (i in 1..10) {
val stats = adbRule.adb.execute(StatFileRequest("/data/local/tmp/app-debug.apk"), adbRule.deviceSerial)
if (stats.size == testFile.length().toInt()) break
delay(100)
}
val stats = adbRule.adb.execute(StatFileRequest("/data/local/tmp/app-debug.apk"), adbRule.deviceSerial)
assertThat(stats.size).isEqualTo(testFile.length().toInt())

val sizeString = adbRule.adb.execute(ShellCommandRequest("${md5()} /data/local/tmp/app-debug.apk"), adbRule.deviceSerial)
val split = sizeString.split(" ").filter { it != "" }
Expand All @@ -89,9 +83,6 @@ class FileE2ETest {
*/
assertThat(split[0]).isEqualTo(testFile.md5())

val stats = adbRule.adb.execute(StatFileRequest("/data/local/tmp/app-debug.apk"), adbRule.deviceSerial)

assertThat(stats.size).isEqualTo(testFile.length().toInt())
//TODO figure out why 644 is actually pushed as 666
assertThat(stats.mode).isEqualTo("100666".toInt(radix = 8))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (C) 2019 Anton Malinskiy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.malinskiy.adam.exception

class PushFailedException(message: String) : RuntimeException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.malinskiy.adam.request.sync

import com.malinskiy.adam.Const
import com.malinskiy.adam.exception.PushFailedException
import com.malinskiy.adam.extension.toByteArray
import com.malinskiy.adam.request.async.AsyncChannelRequest
import com.malinskiy.adam.transport.AndroidReadChannel
Expand Down Expand Up @@ -63,13 +64,18 @@ class PushFileRequest(
val available = channel.readAvailable(buffer, 8, Const.MAX_FILE_PACKET_LENGTH)
return when {
available < 0 -> {
channel.cancel(null)
Const.Message.DONE.copyInto(buffer)
(local.lastModified() / 1000).toInt().toByteArray().copyInto(buffer, destinationOffset = 4)
writeChannel.write(request = buffer, length = 8)
val transportResponse = readChannel.read()
readChannel.cancel(null)
writeChannel.close(null)
1.0
channel.cancel(null)
return if (transportResponse.okay) {
1.0
} else {
throw PushFailedException("adb didn't aknowledge the file transfer: ${transportResponse.message ?: ""}")
}
}
available > 0 -> {
Const.Message.DATA.copyInto(buffer)
Expand Down

0 comments on commit bcc7ee3

Please sign in to comment.