Skip to content

Commit

Permalink
Set properly encrypted file size when uploading (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
kleewho authored Feb 2, 2023
1 parent 8f8fd00 commit d257769
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 89 deletions.
13 changes: 9 additions & 4 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: kotlin
version: 7.4.0
version: 7.4.1
schema: 1
scm: github.com/pubnub/kotlin
files:
- build/libs/pubnub-kotlin-7.4.0-all.jar
- build/libs/pubnub-kotlin-7.4.1-all.jar
sdks:
-
type: library
Expand All @@ -23,8 +23,8 @@ sdks:
-
distribution-type: library
distribution-repository: maven
package-name: pubnub-kotlin-7.4.0
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-kotlin/7.4.0/pubnub-kotlin-7.4.0.jar
package-name: pubnub-kotlin-7.4.1
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-kotlin/7.4.1/pubnub-kotlin-7.4.1.jar
supported-platforms:
supported-operating-systems:
Android:
Expand Down Expand Up @@ -121,6 +121,11 @@ sdks:
license-url: https://github.com/stleary/JSON-java/blob/20210307/LICENSE
is-required: Required
changelog:
- date: 2023-02-02
version: v7.4.1
changes:
- type: bug
text: "Uploading an encrypted file correctly sets the content size in the multipart body."
- date: 2022-09-20
version: v7.4.0
changes:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v7.4.1
February 02 2023

#### Fixed
- Uploading an encrypted file correctly sets the content size in the multipart body.

## v7.4.0
September 20 2022

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ You will need the publish and subscribe keys to authenticate your app. Get your
<dependency>
<groupId>com.pubnub</groupId>
<artifactId>pubnub-kotlin</artifactId>
<version>7.4.0</version>
<version>7.4.1</version>
</dependency>
```

* for Gradle, add the following dependency in your `gradle.build`:
```groovy
implementation 'com.pubnub:pubnub-kotlin:7.4.0'
implementation 'com.pubnub:pubnub-kotlin:7.4.1'
```

2. Configure your keys:
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SONATYPE_HOST=DEFAULT
SONATYPE_AUTOMATIC_RELEASE=true
GROUP=com.pubnub
POM_ARTIFACT_ID=pubnub-kotlin
VERSION_NAME=7.4.0
VERSION_NAME=7.4.1
POM_PACKAGING=jar

POM_NAME=PubNub Android Chat Components
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/pubnub/api/PubNub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class PubNub(val configuration: PNConfiguration) {

companion object {
private const val TIMESTAMP_DIVIDER = 1000
private const val SDK_VERSION = "7.4.0"
private const val SDK_VERSION = "7.4.1"
private const val MAX_SEQUENCE = 65535

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class UploadFile(
val mediaType = getMediaType(formParams.findContentType())

val bytes = prepareBytes(content, cipherKey)
builder.addFormDataPart(FILE_PART_MULTIPART, fileName, bytes.toRequestBody(mediaType, 0, content.size))
builder.addFormDataPart(FILE_PART_MULTIPART, fileName, bytes.toRequestBody(mediaType, 0, bytes.size))
return s3Service.upload(baseUrl, builder.build())
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/com/pubnub/api/legacy/PubNubTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class PubNubTest : BaseTest() {
fun getVersionAndTimeStamp() {
val version = pubnub.version
val timeStamp = pubnub.timestamp()
assertEquals("7.4.0", version)
assertEquals("7.4.1", version)
assertTrue(timeStamp > 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.pubnub.api.endpoints.files.SendFile
import com.pubnub.api.endpoints.files.UploadFile
import com.pubnub.api.enums.PNOperationType
import com.pubnub.api.enums.PNStatusCategory
import com.pubnub.api.legacy.endpoints.files.TestsWithFiles.Companion.folder
import com.pubnub.api.legacy.endpoints.remoteaction.TestRemoteAction
import com.pubnub.api.models.consumer.PNStatus
import com.pubnub.api.models.consumer.files.PNBaseFile
Expand All @@ -20,11 +19,7 @@ import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.FileInputStream
import java.io.IOException
import org.junit.jupiter.api.Test
import java.io.InputStream
import java.time.Instant
import java.util.concurrent.CountDownLatch
Expand All @@ -34,20 +29,13 @@ import java.util.concurrent.atomic.AtomicInteger

class SendFileTest : TestsWithFiles {
private val channel = "channel"
private val filename = "test.txt"
private val generateUploadUrlFactory: GenerateUploadUrl.Factory = mockk {}
private val publishFileMessageFactory: PublishFileMessage.Factory = mockk {}
private val sendFileToS3Factory: UploadFile.Factory = mockk {}

@get:Rule
override val temporaryFolder: TemporaryFolder
get() = folder

@Test
@Throws(PubNubException::class, IOException::class)
fun sync_happyPath() {
// given
val file = getTemporaryFile(filename)
val fileUploadRequestDetails = generateUploadUrlProperResponse()
val expectedResponse = pnFileUploadResult()
val publishFileMessageResult = PNPublishFileMessageResult(expectedResponse.timetoken)
Expand All @@ -63,20 +51,18 @@ class SendFileTest : TestsWithFiles {
every { publishFileMessageFactory.create(any(), any(), any()) } returns publishFileMessage

// when
val result: PNFileUploadResult? = FileInputStream(file).use { fileInputStream ->
sendFile(channel, file.name, fileInputStream).sync()
val result: PNFileUploadResult? = inputStream().use { inputStream ->
sendFile(channel, fileName(), inputStream).sync()
}

// then
Assert.assertEquals(expectedResponse, result)
}

@Test
@Throws(InterruptedException::class, IOException::class)
fun async_happyPath() {
// given
val countDownLatch = CountDownLatch(1)
val file = getTemporaryFile(filename)
val fileUploadRequestDetails = generateUploadUrlProperResponse()
val expectedResponse = pnFileUploadResult()
val publishFileMessageResult = PNPublishFileMessageResult(expectedResponse.timetoken)
Expand All @@ -90,11 +76,11 @@ class SendFileTest : TestsWithFiles {
publishFileMessageResult
)
every { publishFileMessageFactory.create(any(), any(), any()) } returns publishFileMessage
FileInputStream(file).use { fileInputStream ->
inputStream().use { inputStream ->
sendFile(
channel,
file.name,
fileInputStream
fileName(),
inputStream
).async { result: PNFileUploadResult?, _: PNStatus? ->
Assert.assertEquals(expectedResponse, result)
countDownLatch.countDown()
Expand All @@ -104,11 +90,9 @@ class SendFileTest : TestsWithFiles {
}

@Test
@Throws(InterruptedException::class, IOException::class)
fun async_publishFileMessageRetry() {
// given
val countDownLatch = CountDownLatch(1)
val file = getTemporaryFile(filename)
val fileUploadRequestDetails = generateUploadUrlProperResponse()
val expectedResponse = pnFileUploadResult()
val publishFileMessageResult = PNPublishFileMessageResult(expectedResponse.timetoken)
Expand All @@ -125,11 +109,11 @@ class SendFileTest : TestsWithFiles {
)
)
every { publishFileMessageFactory.create(any(), any(), any()) } returns publishFileMessage
FileInputStream(file).use { fileInputStream ->
inputStream().use { inputStream ->
sendFile(
channel,
file.name,
fileInputStream,
fileName(),
inputStream,
numberOfRetries
).async { result: PNFileUploadResult?, _: PNStatus? ->
Assert.assertEquals(expectedResponse, result)
Expand All @@ -143,10 +127,8 @@ class SendFileTest : TestsWithFiles {
}

@Test
@Throws(InterruptedException::class, IOException::class, PubNubException::class)
fun sync_publishFileMessageRetry() {
// given
val file = getTemporaryFile(filename)
val fileUploadRequestDetails = generateUploadUrlProperResponse()
val expectedResponse = pnFileUploadResult()
val publishFileMessageResult = PNPublishFileMessageResult(expectedResponse.timetoken)
Expand All @@ -166,8 +148,8 @@ class SendFileTest : TestsWithFiles {
every { publishFileMessageFactory.create(any(), any(), any()) } returns publishFileMessage

// when
val result: PNFileUploadResult? = FileInputStream(file).use { fileInputStream ->
sendFile(channel, file.name, fileInputStream, numberOfRetries).sync()
val result: PNFileUploadResult? = inputStream().use { inputStream ->
sendFile(channel, fileName(), inputStream, numberOfRetries).sync()
}

// then
Expand All @@ -194,7 +176,7 @@ class SendFileTest : TestsWithFiles {
channel: String,
fileName: String,
inputStream: InputStream,
numberOfRetries: Int = 1
numberOfRetries: Int = 1,
): SendFile {
return SendFile(
channel = channel,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
package com.pubnub.api.legacy.endpoints.files

import org.junit.rules.TemporaryFolder
import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.util.ArrayList
import java.util.Collections
import org.junit.jupiter.api.BeforeEach
import java.util.UUID

interface TestsWithFiles {
val temporaryFolder: TemporaryFolder

fun getTemporaryFile(filename: String, vararg content: String?): File {
return try {
val file = temporaryFolder.newFile(filename)
val lines = ArrayList<String>()
Collections.addAll<String>(lines, *content)
Files.write(file.toPath(), lines)
file
} catch (ex: IOException) {
// fail
throw RuntimeException(ex.message, ex)
}
fun fileName() = _fileName

fun inputStream(content: String = "content") = content.byteInputStream()

@BeforeEach
fun beforeEach() {
_fileName = "file_${UUID.randomUUID()}"
}

companion object {
val folder = TemporaryFolder()
private lateinit var _fileName: String
}
}
Loading

0 comments on commit d257769

Please sign in to comment.