LCOV - code coverage report
Current view: top level - jvmfwk/plugins/sunmajor/pluginlib - sunversion.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 70 141 49.6 %
Date: 2015-06-13 12:38:46 Functions: 5 8 62.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include "sunversion.hxx"
      22             : #include "osl/diagnose.h"
      23             : #include "osl/thread.h"
      24             : #include "osl/process.h"
      25             : #include "osl/security.hxx"
      26             : #include <string.h>
      27             : #include <ctype.h>
      28             : #include "diagnostics.h"
      29             : using namespace osl;
      30             : 
      31             : namespace jfw_plugin  { //stoc_javadetect
      32             : 
      33             : 
      34             : #if OSL_DEBUG_LEVEL >= 2
      35             : class SelfTest
      36             : {
      37             : public:
      38             :     SelfTest();
      39             : } test;
      40             : #endif
      41             : 
      42         154 : SunVersion::SunVersion(const OUString &usVer):
      43             :     m_nUpdateSpecial(0), m_preRelease(Rel_NONE),
      44         154 :     usVersion(usVer)
      45             : {
      46         154 :     memset(m_arVersionParts, 0, sizeof(m_arVersionParts));
      47         154 :     OString sVersion= OUStringToOString(usVer, osl_getThreadTextEncoding());
      48         154 :     m_bValid = init(sVersion.getStr());
      49         154 : }
      50           0 : SunVersion::SunVersion(const char * szVer):
      51           0 :     m_nUpdateSpecial(0), m_preRelease(Rel_NONE)
      52             : {
      53           0 :     memset(m_arVersionParts, 0, sizeof(m_arVersionParts));
      54           0 :     m_bValid = init(szVer);
      55           0 :     usVersion= OUString(szVer,strlen(szVer),osl_getThreadTextEncoding());
      56           0 : }
      57             : 
      58             : 
      59             : /**Format major.minor.maintenance_update
      60             :  */
      61         154 : bool SunVersion::init(const char *szVersion)
      62             : {
      63         154 :     if (!szVersion || szVersion[0] == '\0')
      64           0 :         return false;
      65             : 
      66             :     //first get the major,minor,maintenance
      67         154 :     const char * pLast = szVersion;
      68         154 :     const char * pCur = szVersion;
      69             :     //pEnd point to the position after the last character
      70         154 :     const char * pEnd = szVersion + strlen(szVersion);
      71             :     // 0 = major, 1 = minor, 2 = maintenance, 3 = update
      72         154 :     int nPart = 0;
      73             :     // position within part beginning with 0
      74         154 :     int nPartPos = 0;
      75             :     char buf[128];
      76             : 
      77             :     //char must me a number 0 - 999 and no leading
      78             :     while (true)
      79             :     {
      80         924 :         if (pCur < pEnd && isdigit(*pCur))
      81             :         {
      82         462 :             if (pCur < pEnd)
      83         462 :                 pCur ++;
      84         462 :             nPartPos ++;
      85             :         }
      86             :         //if  correct separator then form integer
      87         462 :         else if (
      88             :             ! (nPartPos == 0) // prevents: ".4.1", "..1", part must start with digit
      89         462 :             && (
      90             :                 //separators after maintenance (1.4.1_01, 1.4.1-beta, or 1.4.1)
      91         385 :                 ((pCur == pEnd || *pCur == '_' || *pCur == '-') && (nPart == 2 ))
      92         308 :                 ||
      93             :                 //separators between major-minor and minor-maintenance
      94         308 :                 (nPart < 2 && *pCur == '.') )
      95         462 :             && (
      96             :                 //prevent 1.4.0. 1.4.0-
      97         462 :                 pCur + 1 != pEnd || isdigit(*(pCur))) )
      98             :         {
      99         462 :             int len = pCur - pLast;
     100         462 :             if (len >= 127)
     101           0 :                 return false;
     102             : 
     103         462 :             strncpy(buf, pLast, len);
     104         462 :             buf[len] = 0;
     105         462 :             pCur ++;
     106         462 :             pLast = pCur;
     107             : 
     108         462 :             m_arVersionParts[nPart] = atoi(buf);
     109         462 :             nPart ++;
     110         462 :             nPartPos = 0;
     111         462 :             if (nPart == 3)
     112         154 :                 break;
     113             : 
     114             :             //check next character
     115         308 :             if (! ( (pCur < pEnd)
     116         308 :                     && ( (nPart < 3) && isdigit(*pCur))))
     117           0 :                 return false;
     118             :         }
     119             :         else
     120             :         {
     121           0 :             return false;
     122             :         }
     123             :     }
     124         154 :     if (pCur >= pEnd)
     125          77 :         return true;
     126             :     //We have now 1.4.1. This can be followed by _01, -beta, etc.
     127             :     // _01 (update) According to docu must not be followed by any other
     128             :     //characters, but on Solaris 9 we have a 1.4.1_01a!!
     129          77 :     if (* (pCur - 1) == '_')
     130             :     {// _01, _02
     131             :         // update is the last part _01, _01a, part 0 is the digits parts and 1 the trailing alpha
     132             :         while (true)
     133             :         {
     134         231 :             if (pCur <= pEnd)
     135             :             {
     136         231 :                 if ( ! isdigit(*pCur))
     137             :                 {
     138             :                     //1.4.1_01-, 1.4.1_01a, the numerical part may only be 2 chars.
     139          77 :                     int len = pCur - pLast;
     140          77 :                     if (len > 2)
     141           0 :                         return false;
     142             :                     //we've got the update: 01, 02 etc
     143          77 :                     strncpy(buf, pLast, len);
     144          77 :                     buf[len] = 0;
     145          77 :                     m_arVersionParts[nPart] = atoi(buf);
     146          77 :                     if (pCur == pEnd)
     147             :                     {
     148          77 :                         break;
     149             :                     }
     150           0 :                     if (*pCur == 'a' && (pCur + 1) == pEnd)
     151             :                     {
     152             :                         //check if it s followed by a simple "a" (not specified)
     153           0 :                         m_nUpdateSpecial = *pCur;
     154           0 :                         break;
     155             :                     }
     156           0 :                     else if (*pCur == '-' && pCur < pEnd)
     157             :                     {
     158             :                         //check 1.5.0_01-ea
     159           0 :                         PreRelease pr = getPreRelease(++pCur);
     160           0 :                         if (pr == Rel_NONE)
     161           0 :                             return false;
     162             :                         //just ignore -ea because its no official release
     163           0 :                         break;
     164             :                     }
     165             :                     else
     166             :                     {
     167           0 :                         return false;
     168             :                     }
     169             :                 }
     170         154 :                 if (pCur < pEnd)
     171         154 :                     pCur ++;
     172             :                 else
     173           0 :                     break;
     174             :             }
     175         154 :         }
     176             :     }
     177             :     // 1.4.1-ea
     178           0 :     else if (*(pCur - 1) == '-')
     179             :     {
     180           0 :         m_preRelease = getPreRelease(pCur);
     181           0 :         if (m_preRelease == Rel_NONE)
     182           0 :             return false;
     183             : #if defined(FREEBSD)
     184             :       if (m_preRelease == Rel_FreeBSD)
     185             :       {
     186             :           pCur++; //eliminate 'p'
     187             :           if (pCur < pEnd && isdigit(*pCur))
     188             :               pCur ++;
     189             :           int len = pCur - pLast -1; //eliminate 'p'
     190             :           if (len >= 127)
     191             :               return false;
     192             :           strncpy(buf, (pLast+1), len); //eliminate 'p'
     193             :           buf[len] = 0;
     194             :           m_nUpdateSpecial = atoi(buf)+100; //hack for FBSD #i56953#
     195             :           return true;
     196             :       }
     197             : #endif
     198             :     }
     199             :     else
     200             :     {
     201           0 :         return false;
     202             :     }
     203         847 :     return true;
     204             : }
     205             : 
     206           0 : SunVersion::PreRelease SunVersion::getPreRelease(const char *szRelease)
     207             : {
     208           0 :     if (szRelease == NULL)
     209           0 :         return Rel_NONE;
     210           0 :     if( ! strcmp(szRelease,"internal"))
     211           0 :         return  Rel_INTERNAL;
     212           0 :     else if( ! strcmp(szRelease,"ea"))
     213           0 :         return  Rel_EA;
     214           0 :     else if( ! strcmp(szRelease,"ea1"))
     215           0 :         return Rel_EA1;
     216           0 :     else if( ! strcmp(szRelease,"ea2"))
     217           0 :         return Rel_EA2;
     218           0 :     else if( ! strcmp(szRelease,"ea3"))
     219           0 :         return Rel_EA3;
     220           0 :     else if ( ! strcmp(szRelease,"beta"))
     221           0 :         return Rel_BETA;
     222           0 :     else if ( ! strcmp(szRelease,"beta1"))
     223           0 :         return Rel_BETA1;
     224           0 :     else if ( ! strcmp(szRelease,"beta2"))
     225           0 :         return Rel_BETA2;
     226           0 :     else if ( ! strcmp(szRelease,"beta3"))
     227           0 :         return Rel_BETA3;
     228           0 :     else if (! strcmp(szRelease, "rc"))
     229           0 :         return Rel_RC;
     230           0 :     else if (! strcmp(szRelease, "rc1"))
     231           0 :         return Rel_RC1;
     232           0 :     else if (! strcmp(szRelease, "rc2"))
     233           0 :         return Rel_RC2;
     234           0 :     else if (! strcmp(szRelease, "rc3"))
     235           0 :         return Rel_RC3;
     236             : #if defined (FREEBSD)
     237             :     else if (! strncmp(szRelease, "p", 1))
     238             :         return Rel_FreeBSD;
     239             : #endif
     240             :     else
     241           0 :         return Rel_NONE;
     242             : }
     243             : 
     244         154 : SunVersion::~SunVersion()
     245             : {
     246             : 
     247         154 : }
     248             : 
     249             : /* Examples:
     250             :    a) 1.0 < 1.1
     251             :    b) 1.0 < 1.0.0
     252             :    c)  1.0 < 1.0_00
     253             : 
     254             :    returns false if both values are equal
     255             : */
     256          77 : bool SunVersion::operator > (const SunVersion& ver) const
     257             : {
     258          77 :     if( &ver == this)
     259           0 :         return false;
     260             : 
     261             :     //compare major.minor.maintenance
     262         154 :     for( int i= 0; i < 4; i ++)
     263             :     {
     264             :         // 1.4 > 1.3
     265         154 :         if(m_arVersionParts[i] > ver.m_arVersionParts[i])
     266             :         {
     267          77 :             return true;
     268             :         }
     269          77 :         else if (m_arVersionParts[i] < ver.m_arVersionParts[i])
     270             :         {
     271           0 :             return false;
     272             :         }
     273             :     }
     274             :     //major.minor.maintenance_update are equal. Test for a trailing char
     275           0 :     if (m_nUpdateSpecial > ver.m_nUpdateSpecial)
     276             :     {
     277           0 :         return true;
     278             :     }
     279             : 
     280             :     //Until here the versions are equal
     281             :     //compare pre -release values
     282           0 :     if ((m_preRelease == Rel_NONE && ver.m_preRelease == Rel_NONE)
     283           0 :         ||
     284           0 :         (m_preRelease != Rel_NONE && ver.m_preRelease == Rel_NONE))
     285           0 :         return false;
     286           0 :     else if (m_preRelease == Rel_NONE && ver.m_preRelease != Rel_NONE)
     287           0 :         return true;
     288           0 :     else if (m_preRelease > ver.m_preRelease)
     289           0 :         return true;
     290             : 
     291           0 :     return false;
     292             : }
     293             : 
     294           0 : bool SunVersion::operator < (const SunVersion& ver) const
     295             : {
     296           0 :     return (! operator > (ver)) && (! operator == (ver));
     297             : }
     298             : 
     299          77 : bool SunVersion::operator == (const SunVersion& ver) const
     300             : {
     301          77 :     bool bRet= true;
     302         154 :     for(int i= 0; i < 4; i++)
     303             :     {
     304         154 :         if( m_arVersionParts[i] != ver.m_arVersionParts[i])
     305             :         {
     306          77 :             bRet= false;
     307          77 :             break;
     308             :         }
     309             :     }
     310          77 :     bRet = m_nUpdateSpecial == ver.m_nUpdateSpecial && bRet;
     311          77 :     bRet = m_preRelease == ver.m_preRelease && bRet;
     312          77 :     return bRet;
     313             : }
     314             : 
     315             : 
     316             : #if OSL_DEBUG_LEVEL >= 2
     317             : SelfTest::SelfTest()
     318             : {
     319             :     bool bRet = true;
     320             : 
     321             :     char const * versions[] = {"1.4.0", "1.4.1", "1.0.0", "10.0.0", "10.10.0",
     322             :                          "10.2.2", "10.10.0", "10.10.10", "111.0.999",
     323             :                          "1.4.1_01", "9.90.99_09", "1.4.1_99",
     324             :                          "1.4.1_00a",
     325             :                          "1.4.1-ea", "1.4.1-beta", "1.4.1-rc1",
     326             :                          "1.5.0_01-ea", "1.5.0_01-rc2"};
     327             :     char const * badVersions[] = {".4.0", "..1", "", "10.0", "10.10.0.", "10.10.0-", "10.10.0.",
     328             :                             "10.2-2", "10_10.0", "10..10","10.10", "a.0.999",
     329             :                             "1.4b.1_01", "9.90.-99_09", "1.4.1_99-",
     330             :                             "1.4.1_00a2", "1.4.0_z01z", "1.4.1__99A",
     331             :                             "1.4.1-1ea", "1.5.0_010", "1.5.0._01-", "1.5.0_01-eac"};
     332             :     char const * orderedVer[] = { "1.3.1-ea", "1.3.1-beta", "1.3.1-rc1",
     333             :                             "1.3.1", "1.3.1_00a", "1.3.1_01", "1.3.1_01a",
     334             :                             "1.3.2", "1.4.0", "1.5.0_01-ea", "2.0.0"};
     335             : 
     336             :     int num = sizeof (versions) / sizeof(char*);
     337             :     int numBad = sizeof (badVersions) / sizeof(char*);
     338             :     int numOrdered = sizeof (orderedVer) / sizeof(char*);
     339             :     //parsing test (positive)
     340             :     for (int i = 0; i < num; i++)
     341             :     {
     342             :         SunVersion ver(versions[i]);
     343             :         if ( ! ver)
     344             :         {
     345             :             bRet = false;
     346             :             break;
     347             :         }
     348             :     }
     349             :     OSL_ENSURE(bRet, "SunVersion selftest failed");
     350             :     //Parsing test (negative)
     351             :     for ( int i = 0; i < numBad; i++)
     352             :     {
     353             :         SunVersion ver(badVersions[i]);
     354             :         if (ver)
     355             :         {
     356             :             bRet = false;
     357             :             break;
     358             :         }
     359             :     }
     360             :     OSL_ENSURE(bRet, "SunVersion selftest failed");
     361             : 
     362             :     // Ordering test
     363             :     bRet = true;
     364             :     int j = 0;
     365             :     for (int i = 0; i < numOrdered; i ++)
     366             :     {
     367             :         SunVersion curVer(orderedVer[i]);
     368             :         if ( ! curVer)
     369             :         {
     370             :             bRet = false;
     371             :             break;
     372             :         }
     373             :         for (j = 0; j < numOrdered; j++)
     374             :         {
     375             :             SunVersion compVer(orderedVer[j]);
     376             :             if (i < j)
     377             :             {
     378             :                 if ( !(curVer < compVer))
     379             :                 {
     380             :                     bRet = false;
     381             :                     break;
     382             :                 }
     383             :             }
     384             :             else if ( i == j)
     385             :             {
     386             :                 if (! (curVer == compVer
     387             :                        && ! (curVer > compVer)
     388             :                        && ! (curVer < compVer)))
     389             :                 {
     390             :                     bRet = false;
     391             :                     break;
     392             :                 }
     393             :             }
     394             :             else if (i > j)
     395             :             {
     396             :                 if ( !(curVer > compVer))
     397             :                 {
     398             :                     bRet = false;
     399             :                     break;
     400             :                 }
     401             :             }
     402             :         }
     403             :         if ( ! bRet)
     404             :             break;
     405             :     }
     406             :     if (bRet)
     407             :         JFW_TRACE2("Testing class SunVersion succeeded.");
     408             :     else
     409             :         OSL_ENSURE(bRet, "[Java framework] sunjavaplugin: SunVersion self test failed.\n");
     410             : }
     411             : #endif
     412             : 
     413             : }
     414             : 
     415             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11