summaryrefslogtreecommitdiffstats
path: root/test/bio_memleak_test.c
diff options
context:
space:
mode:
authorTomas Mraz <tmraz@fedoraproject.org>2019-04-04 09:49:36 +0200
committerMatt Caswell <matt@openssl.org>2019-04-16 10:55:36 +0100
commit2456ae5763dc4b036b3b4cdb9b98de5d46dd221f (patch)
treec44b56adbe612d98b9c349e679151cb9853dba63 /test/bio_memleak_test.c
parent693f98aae8a33f2e0f91264ca7383438bae93d47 (diff)
Add test for the BIO_s_mem rdwr->rdonly->rdwr use-case
Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de> Reviewed-by: Matt Caswell <matt@openssl.org> (Merged from https://github.com/openssl/openssl/pull/8649) (cherry picked from commit 06add280d90de9625e9c18985f376ef8d0419a46)
Diffstat (limited to 'test/bio_memleak_test.c')
-rw-r--r--test/bio_memleak_test.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/test/bio_memleak_test.c b/test/bio_memleak_test.c
index bde66e0812..9724148fae 100644
--- a/test/bio_memleak_test.c
+++ b/test/bio_memleak_test.c
@@ -145,6 +145,43 @@ finish:
return ok;
}
+static int test_bio_rdwr_rdonly(void)
+{
+ int ok = 0;
+ BIO *bio = NULL;
+ char data[16];
+
+ bio = BIO_new(BIO_s_mem());
+ if (!TEST_ptr(bio))
+ goto finish;
+ if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
+ goto finish;
+
+ BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY);
+ if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
+ goto finish;
+ if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
+ goto finish;
+ if (!TEST_int_gt(BIO_reset(bio), 0))
+ goto finish;
+
+ BIO_clear_flags(bio, BIO_FLAGS_MEM_RDONLY);
+ if (!TEST_int_eq(BIO_puts(bio, "Hi!\n"), 4))
+ goto finish;
+ if (!TEST_int_eq(BIO_read(bio, data, 16), 16))
+ goto finish;
+
+ if (!TEST_mem_eq(data, 16, "Hello World\nHi!\n", 16))
+ goto finish;
+
+ ok = 1;
+
+finish:
+ BIO_free(bio);
+ return ok;
+}
+
+
int global_init(void)
{
CRYPTO_set_mem_debug(1);
@@ -158,5 +195,6 @@ int setup_tests(void)
ADD_TEST(test_bio_get_mem);
ADD_TEST(test_bio_new_mem_buf);
ADD_TEST(test_bio_rdonly_mem_buf);
+ ADD_TEST(test_bio_rdwr_rdonly);
return 1;
}
ble.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
use {
    crate::{
        app_context::AppContext,
        errors::ProgramError,
        io::W,
        mad_skin::{
            self,
            StatusMadSkinSet,
        },
        skin::Skin,
    },
    crossterm::{
        cursor,
        terminal::{Clear, ClearType},
        QueueableCommand,
    },
    termimad::{Area, CompoundStyle, InputField, MadSkin},
};

pub static FLAGS_AREA_WIDTH: u16 = 10;

pub struct Screen {
    pub width: u16,
    pub height: u16,
    pub skin: Skin,
    pub input_field: InputField,
    pub status_skin: StatusMadSkinSet,
    pub help_skin: MadSkin,
}

impl Screen {
    pub fn new(con: &AppContext, skin: Skin) -> Result<Screen, ProgramError> {
        let mut input_field = InputField::new(Area::new(0, 0, 10, 1));
        input_field.set_normal_style(CompoundStyle::from(skin.input.clone()));
        let status_skin = StatusMadSkinSet::from_skin(&skin);
        let help_skin = mad_skin::make_help_mad_skin(&skin);
        let mut screen = Screen {
            width: 0,
            height: 0,
            skin,
            input_field,
            status_skin,
            help_skin,
        };
        screen.read_size(con)?;
        Ok(screen)
    }
    pub fn set_terminal_size(&mut self, w: u16, h: u16, con: &AppContext) {
        self.width = w;
        self.height = h;
        if let Some(h) = con.launch_args.height {
            self.height = h;
        }
        self.input_field.change_area(0, h-1, w - FLAGS_AREA_WIDTH);
    }
    pub fn read_size(&mut self, con: &AppContext) -> Result<(), ProgramError> {
        let (w, h) = termimad::terminal_size();
        self.set_terminal_size(w, h, con);
        Ok(())
    }
    /// move the cursor to x,y and clears the line.
    pub fn goto_clear(&self, w: &mut W, x: u16, y: u16)
    -> Result<(), ProgramError> {
        self.goto(w, x, y)?;
        self.clear_line(w)
    }
    /// move the cursor to x,y
    pub fn goto(
        &self,
        w: &mut W,
        x: u16,
        y: u16
    ) -> Result<(), ProgramError> {
        w.queue(cursor::MoveTo(x, y))?;
        Ok(())
    }
    /// clear the whole screen
    pub fn clear(&self, w: &mut W) -> Result<(), ProgramError> {
        w.queue(Clear(ClearType::All))?;
        Ok(())
    }
    /// clear from the cursor to the end of line
    pub fn clear_line(&self, w: &mut W) -> Result<(), ProgramError> {
        w.queue(Clear(ClearType::UntilNewLine))?;
        Ok(())
    }
}